/** * 判断key是否是字符串,如果不是,则转换成字符串 */ function_keyAsString(key) { if (typeof key !== 'string') { console.warn(key + ' used as a key, but it is not a string.'); key = String(key); } return key; }
/** * exp 失效时间:默认为0(单位:分钟) */ s.setExp = function(key, val, exp) { key = _keyAsString(key); if (exp === undefined) { exp = 0; } s.set(key, new VConstructor(val, exp)); }; s.getExp = function(key) { key = _keyAsString(key); var item = null; try { item = s.get(key); } catch (e) { returnnull; } if (_hasEffective(item)) { if (_isEffective(item)) { return item.v; } else { s.remove(key); returnnull; } } else { return item; } }; s.getAllExp = function() { var ret = {}; s.forEachExp(function(key, val) { ret[key] = val; }) return ret; }; s.forEachExp = function(callback) { var storage = s.getAll(); for ( var key in storage) { callback(key, s.getExp(key)); } }; })(store);