- 是否需要兼容,是否可扩展--原生js
- 遵循开闭原则
- 扩展性
- 应用简单
- 面向接口的设计
// 1、是否需要兼容,是否可扩展--原生js
// 2、遵循开闭原则
// 3、扩展性
// 4、应用简单
// 5、面向接口的设计
(function (global, factory, name) {
return global[name] = factory.call(global);
})(this, function () {
var __TYPES__ = {
MEMORY: "MEMORY"
};
// 缓存引擎
var __ENGINE__ = {
[__TYPES__.MEMORY]: { // 这个是什么用法?
init: function () {
this.pools = this.pools || {};
},
set: function (ops) {
console.log("ops>>", ops)
this.pools || this.init();
console.log("set 默认引擎", this.pools)
this.pools[ops.key] = ops.data;
},
get: function (ops) {
console.log("ops>>", ops)
this.pools || this.init();
console.log("get 默认引擎", this.pools)
return this.pools[ops.key];
},
}
};
var __CACHE__ = {
install: function (cacheType, cacheImpl) {
// 安装某一个缓存机制, 用于扩展不同的缓存实现
__TYPES__[cacheType] = cacheType;
__ENGINE__[cacheType] = cacheImpl;
},
uninstall: function () {
// 制裁某一个缓存机制
},
set: function (type, ops) {
__ENGINE__[type || __TYPES__.MEMORY].set(ops);
},
get: function (type, ops) {
return __ENGINE__[type || __TYPES__.MEMORY].get(ops);
},
};
__CACHE__.install(__ENGINE__.MEMORY);
return {
TYPES: __TYPES__,
getInstance: function () {
return __CACHE__;
}
};
}, "EasyCache");
let cache = EasyCache.getInstance();
// console.log(cache);
// console.log(EasyCache.TYPES);
//
//
//
//
// cache.install("LOCAL_STORAGE", {
// init: function () {
//
// },
// set: function (ops) {
// console.log("自定义的缓存")
//
// },
// get: function (ops) {
//
// },
// });
cache.set(EasyCache.TYPES.LOCAL_STORAGE, {})
cache.set(EasyCache.TYPES.MEMORY, {key:"test1", data:{
name: "张三",
age: 29
}});
var result = cache.get(EasyCache.TYPES.MEMORY, {key:"test1"});
console.log("缓存》》", result);
转载于:https://www.jianshu.com/p/85ba74949376