UNPKG

h5-cli

Version:

hello

144 lines (125 loc) 3.17 kB
"use strict"; /** lib.cache.js \u8d44\u6e90\u7f13\u5b58 */ define("libs/cache", function (require, exports, module) { var IS_LOCAL_STORGEl = window.localStorage; /** cache.set(key, value, expire); cache.get(key) */ var cache = module.exports = { cacheMap: {}, set: function set(key, value, expire) { if (IS_LOCAL_STORGEl) { return window.localStorage.setItem(key, value); } return null; }, get: function get(key) { if (IS_LOCAL_STORGEl) { return window.localStorage.getItem(key); } return null; }, remove: function remove(key) { if (IS_LOCAL_STORGEl) { return window.localStorage.removeItem(key); } return null; }, getCordId: function getCordId() { this.get("corpId"); }, getCacheModule: function getCacheModule(name, keepTime) { name = name || "yuantu-cache"; // \u4e0d\u8981\u7f13\u5b58 // if( !this.cacheMap[ name ] ){ // this.cacheMap[name] = new CacheModule( name ); // } return new CacheModule(name, keepTime); }, removeCacheModule: function removeCacheModule(name) { name = name || "yuantu-cache"; this.cacheMap[name] = null; this.remove(name); } }; /** \u6784\u9020\u4e00\u4e2a\u591a\u9875\u9762\u4f20\u503c\u7684module var abcCache = new cache.CacheModule("abc"); abcCache.set("key", "value", "name"); abcCache.get("key") // {value:"value", name:"name"} var t = Date.now(); md5("abdslfjalkdjfla"); console.log(Date.now() - t) */ function CacheModule(name, keepTime) { this.name = name; //10\u5206\u949f\u5185\u4f7f\u7528\u7f13\u5b58 this.cacheTime = keepTime || 1000 * 60 * 100000; this.module = {}; this.init(); } CacheModule.prototype = { init: function init() { try { //console.log(JSON.stringify(cache.get(this.name))) this.module = JSON.parse(cache.get(this.name)) || {}; //\u5220\u9664\u5df2\u7ecf\u8fc7\u671f\u7684\u7f13\u5b58 for (var key in this.module) { // console.log(Date.now() - this.module[key].time > this.cacheTime ) if (this.module[key].time && Date.now() - this.module[key].time > this.cacheTime) { console.log("\u5220\u9664", key); delete this.module[key]; } } } catch (e) { this.module = {}; } }, refresh: function refresh() { try { this.module = JSON.parse(cache.get(this.name)) || this.module; } catch (e) {} }, set: function set(key, value, name) { var json = null; // try{ // json = cache.get(this.name); // if( json ){ // json = JSON.parse(json); // } // }catch(e){} this.module[key] = { value: value, name: name, time: Date.now() }; this.save(); }, get: function get(key) { return this.module[key] || {}; }, remove: function remove(key) { if (this.module[key]) { delete this.module[key]; this.save(); } }, save: function save() { //\u5f02\u6b65\u4fdd\u5b58\u6570\u636e var name = this.name; var module = this.module; // setTimeout(function(){ cache.set(name, JSON.stringify(module)); // },0); }, clear: function clear() { this.module = {}; this.save(); } }; window.cache = cache; });