Common points between sessionStorage , localStorage and cookies:
They are all saved on the browser and have the same origin.
Differences between sessionStorage , localStorage and cookies:
Cookie data is always carried in the same-origin http request (even if not required), i.e. cookies are passed back and forth between the browser and the server. SessionStorage and localStorage will not automatically send data to the server and will only be saved locally. Cookie data also has the concept of path (path), which can restrict cookies from belonging to a certain path.
The storage size limit is also different. The cookie data cannot exceed 4k. At the same time, because cookies are carried every http request, cookies are only suitable for storing very small data, such as session identifiers. SessionStorage and localStorage also have storage size limitations, but are much larger than cookies and can reach 5M or larger.
The data validity period is different. SessionStorage: only valid before the current browser window is closed, so it is naturally impossible to persist; localStorage: always valid, and the window or browser closes, so it is used as persistent data; cookies are only valid until the set cookie expiration time, even if the window or browser closes.
Different scopes, sessionStorage is not shared in different browser windows, even on the same page; localStorage is shared in all homologous windows; cookies are shared in all homologous windows.
Web Storage supports event notification mechanisms, which can send notifications of data updates to listeners.
Web Storage's API interface is more convenient to use.
The encapsulated localStorage method can control the number of data stored and the time
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 //Second of seconds in a day 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; //Start doing LRU algorithm. this.LRU(key); //The LRU algorithm ends. var isFresh = true; var nowTime = (new Date()).getTime() / 1000; if(key in this.memQueue.keys){ var cacheTime = this.memQueue.keys[key].life / 1000; //If the expiration time exceeds the configured lifeTime, //The current cache is cleared 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; } } //If isFresh is false, it is expired, then return null, otherwise return from localStorage (false == isFresh) ? null : this.localStorage[key]; } }, set: function(appid, url, value) { if (true == support()) { var key = appid + ':' + url; var lruKey = this.getLRU(); //Eliminate the least used recently. //Or another method will read the most suitable for elimination //The premise is that the current key is not in localStorage. if (lruKey) { this.localStorage.removeItem(lruKey); } //Start set this value//For compatibility, use the following method to set 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(); //Eliminate the least used recently. //Get another method to read the most elimination 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; //The current key must also be lruled this.LRU(key); //lru ends this.localStorage.setItem('LRUConfig', JSON.stringify(this.memQueue)); } }, /* * Algorithm is least used recently*/ LRU: function(key) { var memQueue = this.memQueue; if (typeof memQueue.objs != 'undefined') { var _o = memQueue.objs; //Start calculate the key to be eliminated, //It is the largest time. If there are several largest times//, the var with the smallest time 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) { //Sort in descending order of times. if (f.times < s.times) { return 1; } else if (f.times > s.times) { 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; } }, /* * Read an item that needs to be eliminated*/ getLRU: function() { var _o = this.memQueue.objs; if (_o) { return (_o.length >= this.config.size) ? _o.shift().key : null; } return null; } }); return { 'cache': Cache };});How to use
var cache = require('cache');// set value cache.Cache.set('ip', 'your own url', value);// get value cache.Cache.get('ip')