[i];
? ? ? ? if (tmpObj.timeout < now) {
? ? ? ? ? this.sProxy.removeItem(tmpObj.key);
? ? ? ? } else {
? ? ? ? ? newMap.push(tmpObj);
? ? ? ? }
? ? ? }
? ? ? this.sProxy.setItem(this.keyCache, JSON.stringify(newMap));
? ? },
? ? removeLastCache: function () {
? ? ? var i, len;
? ? ? var num = this.removeNum || 5;
? ? ? //取出键值对
? ? ? var cacheStr = this.sProxy.getItem(this.keyCache);
? ? ? var cacheMap = [];
? ? ? var delMap = [];
? ? ? //说明本次存储过大
? ? ? if (!cacheStr) return false;
? ? ? cacheMap.sort(function (a, b) {
? ? ? ? return a.timeout - b.timeout;
? ? ? });
? ? ? //删除了哪些数据
? ? ? delMap = cacheMap.splice(0, num);
? ? ? for (i = 0, len = delMap.length; i < len; i++) {
? ? ? ? this.sProxy.removeItem(delMap[i].key);
? ? ? }
? ? ? this.sProxy.setItem(this.keyCache, JSON.stringify(cacheMap));
? ? ? return true;
? ? },
? ? setKeyCache: function (key, timeout) {
? ? ? if (!key || !timeout || timeout < new Date().getTime()) return;
? ? ? var i, len, tmpObj;
? ? ? //获取当前已经缓存的键值字符串
? ? ? var oldstr = this.sProxy.getItem(this.keyCache);
? ? ? var oldMap = [];
? ? ? //当前key是否已经存在
? ? ? var flag = false;
? ? ? var obj = {};
? ? ? obj.key = key;
? ? ? obj.timeout = timeout;
? ? ? if (oldstr) {
? ? ? ? oldMap = JSON.parse(oldstr);
? ? ? ? if (!_.isArray(oldMap)) oldMap = [];
? ? ? }
? ? ? for (i = 0, len = oldMap.length; i < len; i++) {
? ? ? ? tmpObj = oldMap[i];
? ? ? ? if (tmpObj.key == key) {
? ? ? ? ? oldMap[i] = obj;
? ? ? ? ? flag = true;
? ? ? ? ? break;
? ? ? ? }
? ? ? }
? ? ? if (!flag) oldMap.push(obj);
? ? ? //最后将新数组放到缓存中
? ? ? this.sProxy.setItem(this.keyCache, JSON.stringify(oldMap));
? ? },
? ? buildStorageObj: function (value, indate, timeout, sign) {
? ? ? var obj = {
? ? ? ? value: value,
? ? ? ? timeout: timeout,
? ? ? ? sign: sign,
? ? ? ? indate: indate
? ? ? };
? ? ? return obj;
? ? },
? ? get: function (key, sign) {
? ? ? var result, now = new Date().getTime();
? ? ? try {
? ? ? ? result = this.sProxy.getItem(key);
? ? ? ? if (!result) return null;
? ? ? ? result = JSON.parse(result);
? ? ? ? //数据过期
? ? ? ? if (result.timeout < now) return null;
? ? ? ? //需要验证签名
? ? ? ? if (sign) {
? ? ? ? ? if (sign === result.sign)
? ? ? ? ? ? return result.value;
? ? ? ? ? return null;
? ? ? ? } else {
? ? ? ? ? return result.value;
? ? ? ? }
? ? ? } catch (e) {
? ? ? ? console && console.log(e);
? ? ? }
? ? ? return null;
? ? },
? ? //获取签名
? ? getSign: function (key) {
? ? ? var result, sign = null;
? ? ? try {
? ? ? ? result = this.sProxy.getItem(key);
? ? ? ? if (result) {
? ? ? ? ? result = JSON.parse(result);
? ? ? ? ? sign = result && result.sign
? ? ? ? }
? ? ? } catch (e) {
? ? ? ? console && console.log(e);
? ? ? }
? ? ? return sign;
? ? },
? ? remove: function (key) {
? ? ? return this.sProxy.removeItem(key);
? ? },
? ? clear: function () {
? ? ? this.sProxy.clear();
? ? }
? });
? Storage.getInstance = function () {
? ? if (this.instance) {
? ? ? return this.instance;
? ? } else {
? ? ? return this.instance = new this();
? ? }
? };
? return Storage;
});
这段代码包含了localstorage的基本操作,并且对以上问题做了处理,而真实的使用还要再抽象:
define(['AbstractStorage'], function (AbstractStorage) {
? var Store = _.inherit({
? ? //默认属性
? ? propertys: function () {
? ? ? //每个对象一定要具有存储键,并且不能重复
? ? ? this.key = null;
? ? ? //默认一条数据的生命周期,S为秒,M为分,D为天
? ? ? this.lifeTime = '30M';
? ? ? //默认返回数据
? ? ? //? ? ? this.defaultData = null;
? ? ? //代理对象,localstorage对象
? ? ? this.sProxy = new AbstractStorage();
? ? },
? ? setOption: function (options) {
? ? ? _.extend(this, options);
? ? },
? ? assert: function () {
? ? ? if (this.key === null) {
? ? ? ? throw 'not override key property';
? ? ? }
? ? ? if (this.sProxy === null) {
? ? ? ? throw 'not override sProxy property';
? ? ? }
? ? },
? ? initialize: function (opts) {
? ? ? this.propertys();
? ? ? this.setOption(opts);
? ? ? this.assert();
? ? },
? ? _getLifeTime: function () {
? ? ? var timeout = 0;
? ? ? var str = this.lifeTime;
? ? ? var unit = str.charAt(