javascript存储的三种要领
sessionStorage 、localStorage 和 cookie
sessionStorage 、localStorage 和 cookie 之间的区分
共同点:
都是保留在浏览器端,且同源的。
区分:
cookie数据一向在同源的http要求中照顾(纵然不须要),即cookie在浏览器和服务器间往返通报。而sessionStorage和localStorage不会自动把数据发给服务器,仅在当地保留。cookie数据另有途径(path)的观点,能够限定cookie只属于某个途径下。
存储大小限定也差别,cookie数据不能凌驾4k,同时由于每次http要求都邑照顾cookie,所以cookie只合适保留很小的数据,如会话标识。sessionStorage和localStorage 虽然也有存储大小的限定,但比cookie大得多,能够到达5M或更大。
数据有用期差别,sessionStorage:仅在当前浏览器窗口封闭前有用,天然也就不可能耐久坚持;localStorage:一向有用,窗口或浏览器封闭也一向保留,因而用作耐久数据;cookie只在设置的cookie逾期时候之前一向有用,纵然窗口或浏览器封闭。
作用域差别,sessionStorage不在差别的浏览器窗口中同享,纵然是同一个页面;localStorage 在所有同源窗口中都是同享的;cookie也是在所有同源窗口中都是同享的。
Web Storage 支撑事宜关照机制,能够将数据更新的关照发送给监听者。
Web Storage 的 api 接口运用更轻易。
封装的localStorage的要领,能够掌握存储数据的条数,以及时候
define(function (require) {
var $ = require('jquery');
var Cache = {};
function support() {
var _t = !(typeof window.localStorage === 'undefined');
return _t;
}
$.extend(Cache, {
config: {
size: 5,
// lifeTime: 86400 //一天的秒数
lifeTime: 1*60
},
localStorage: window.localStorage,
memQueue: (function () {
if (support()) {
var jsonStr = window.localStorage.getItem('LRUConfig');
return jsonStr ? JSON.parse(jsonStr) : {
keys: {},
objs: []
};
} else {
return {};
}
})(),
get: function(appid, url) {
if (true == support()) {
var key = appid + ':' + url;
//最先做LRU算法。
this.LRU(key);
//LRU算法完毕。
var isFresh = true;
var nowTime = (new Date()).getTime() / 1000;
if(key in this.memQueue.keys){
var cacheTime = this.memQueue.keys[key].life / 1000;
//假如逾期时候凌驾 设置的lifeTime,
//则清撤除当前缓存
if(nowTime - cacheTime >= this.config.lifeTime){
delete this.memQueue.keys[key];
for (var i=0, len = this.memQueue.objs.length; i < len; i++) {
var _o = this.memQueue.objs[i];
if(_o.key == key){
this.memQueue.objs.splice(i,1);
break;
}
}
isFresh = false;
}
}
//假如isFresh为假,就是已逾期,则返回null,否则从localStorage中取
return (false == isFresh) ? null : this.localStorage[key];
}
},
set: function(appid, url, value) {
if (true == support()) {
var key = appid + ':' + url;
var lruKey = this.getLRU();
//镌汰近来起码运用的这个。
//别的起一个要领读取最相符镌汰的这个
//条件是当前这个key,不在localStorage内里。
if (lruKey) {
this.localStorage.removeItem(lruKey);
}
//最先设置一下这个值
//为了兼容性,用以下要领设置
if (typeof this.memQueue.objs != 'undefined' &&
this.memQueue.objs.length <= this.config.size) {
this.localStorage.removeItem(key);
} else {
while (this.memQueue.objs.length >= this.config.size) {
var lruKey = this.getLRU();
//镌汰近来起码运用的这个。
//别的起一个要领读取最相符镌汰的这个
if (lruKey) {
this.localStorage.removeItem(lruKey);
delete this.memQueue.keys[lruKey];
for (var i = 0; i < this.memQueue.objs.length; i++) {
var _o = this.memQueue.objs[i];
if(_o.key == lruKey){
this.memQueue.objs.splice(i,1);
break;
}
}
}
}
}
this.localStorage[key] = value;
//当前的key,也必需lru一下
this.LRU(key);
//lru完毕
this.localStorage.setItem('LRUConfig', JSON.stringify(this.memQueue));
}
},
/*
* 近期起码运用算法
*/
LRU: function(key) {
var memQueue = this.memQueue;
if (typeof memQueue.objs != 'undefined') {
var _o = memQueue.objs;
//最先盘算谁人要镌汰的key,
//就是谁人times最大的,假如times最大的有几个
//则返回谁人time最小的
var isIn = false;
for (var i = 0, len = _o.length; i < len; i++) {
_o[i].times = (key == _o[i].key) ? 0 : _o[i].times + 1;
_o[i].time = (key == _o[i].key) ? (new Date()).getTime() : _o[i].time;
if(key == _o[i].key && false == isIn){
isIn = true;
}
}
// 假如
if(false == isIn){
var _to = {
'key': key,
'times': 0,
'time': (new Date()).getTime(),
'life': (new Date()).getTime()
};
this.memQueue.keys[key] = _to;
this.memQueue.objs.push(_to);
}
_o.sort(function(f, s) {
//按times降序分列。
if (f.times < s.times) {
return 1;
} else if (f.times > s.times) {
return -1;
} else {
//最先比较time
//按time,时候升序分列
if (f.time < s.time) {
return -1;
} else {
return 1;
}
}
});
} else {
this.memQueue.objs = [];
this.memQueue.keys = {};
var _to = {
'key': key,
'times': 0,
'time': (new Date()).getTime(),
'life': (new Date()).getTime()
};
this.memQueue.keys[key] = _to;
this.memQueue.objs.push(_to);
return null;
}
},
/*
* 读取须要镌汰的一项
*/
getLRU: function() {
var _o = this.memQueue.objs;
if (_o) {
return (_o.length >= this.config.size) ? _o.shift().key : null;
}
return null;
}
});
return {
'cache': Cache
};
});
运用要领
var cache = require('cache');
// set 值
cache.Cache.set('ip', '你本身的一个url', value);
// get值
cache.Cache.get('ip')