增加注释

This commit is contained in:
kuangthree 2024-02-03 12:48:20 +08:00
parent 217a1137da
commit 0006a38c54
1 changed files with 50 additions and 17 deletions

View File

@ -9,29 +9,49 @@ import { hex_md5 } from "../crypt/md5.js";
export class CacheContext{
constructor(){
this.lib = this.createCacheProxy(Library);
this.game = this.createCacheProxy(Game);
this.get = this.createCacheProxy(Get);
this.lib = this._createCacheProxy(Library);
this.game = this._createCacheProxy(Game);
this.get = this._createCacheProxy(Get);
this.sourceMap = new Map();
this.storageMap = new Map();
}
/**
* 设置当前是否处于缓存环境当使用inject对类进行注入时只在缓存环境下会返回缓存值
* @param {boolean} cache
*/
static setInCacheEnvironment(cache){
_status.cacheEnvironment = cache;
}
/**
* 设置一个公有的缓存上下文缓存上下文持有期间假设所缓存的函数在参数相同时绝对不会注意是绝对不会返回不同的返回值
* 使用inject对类进行注入时将应用公有的缓存上下文
* @param {CacheContext} context
*/
static setCacheContext(context){
_status.cacheContext = context;
}
/**
* 返回当前公有的缓存上下文
* @returns {CacheContext} 缓存上下文
*/
static getCacheContext(){
return _status.cacheContext;
}
/**
* 移除当前公有的缓存上下文
*/
static removeCacheContext(){
delete _status.cacheContext;
}
/**
* 返回公有的缓存上下文没有就创建一个新的返回不会设置为新的公有缓存上下文
* @returns {CacheContext} 缓存上下文
*/
static requireCacheContext(){
let cache = CacheContext.getCacheContext();
if(!cache){
@ -40,6 +60,13 @@ export class CacheContext{
return cache;
}
/**
* 对一个对象进行代理对象的所有函数都将按条件返回缓存结果
* 注意以cache开头的方法依然保持原来的调用
* 如果所代理的对象拥有cacheSupportFunction方法返回一个方法名数组只有允许的方法才会返回缓存结果剩余方法依然保持原来的调用
* @param {any} source 需要代理的对象
* @returns
*/
delegate(source){
if(source === null || source === undefined)return source;
if(source._cacheDelegateSource)return source;
@ -47,11 +74,17 @@ export class CacheContext{
if(proxy){
return proxy;
}
proxy = this.createCacheProxy(source);
proxy = this._createCacheProxy(source);
this.sourceMap.set(source,proxy);
return proxy;
}
/**
* 对一个类进行注入methods为可以返回缓存的所有方法注入后此类的相关方法会在公有缓存上下文下返回缓存值
* @param {any} source
* @param {Array<string>} methods
* @returns
*/
static inject(source,methods){
if(source == null || source === undefined)return null;
for(let method of methods){
@ -63,7 +96,7 @@ export class CacheContext{
return func.call(this,...arguments);
}
return CacheContext
.getCacheValueFromObject(CacheContext.requireCacheContext().requireStorage(this),method,arguments,this,func);
._getCacheValueFromObject(CacheContext.requireCacheContext()._requireStorage(this),method,arguments,this,func);
}catch(e){
return func.call(this,...arguments);
}
@ -71,7 +104,7 @@ export class CacheContext{
}
}
requireStorage(obj){
_requireStorage(obj){
let storage = this.storageMap.get(obj);
if(!storage){
storage = {};
@ -85,7 +118,7 @@ export class CacheContext{
* @param {T} delegateObject
* @returns {T}
*/
createCacheProxy(delegateObject){
_createCacheProxy(delegateObject){
const cacheFuncObj = {};
const cacheStorage = {};
return new Proxy(delegateObject,{
@ -106,7 +139,7 @@ export class CacheContext{
wrapFunc = function(){
try{
return CacheContext
.getCacheValueFromObject(cacheStorage,key,arguments,target);
._getCacheValueFromObject(cacheStorage,key,arguments,target);
}catch(e){
return value.call(target,...arguments);
}
@ -120,10 +153,10 @@ export class CacheContext{
});
}
static getCacheValueFromObject(storage,key,params,source,func){
static _getCacheValueFromObject(storage,key,params,source,func){
let cache = storage;
let funcCache = CacheContext.ensureMember(cache,key);
let cacheKey = CacheContext.wrapParametersToCacheKey(params);
let funcCache = CacheContext._ensureMember(cache,key);
let cacheKey = CacheContext._wrapParametersToCacheKey(params);
let ret = funcCache[cacheKey];
if(ret === undefined){
ret = ((typeof func == 'function')?func:source[key]).call(source,...params);
@ -135,7 +168,7 @@ export class CacheContext{
return ret;
}
static ensureMember(obj,key){
static _ensureMember(obj,key){
let mem = obj[key];
if(!mem){
mem = {};
@ -144,14 +177,14 @@ export class CacheContext{
return mem;
}
static wrapParametersToCacheKey(params){
static _wrapParametersToCacheKey(params){
return Array.from(params)
.filter(p=>!(p instanceof CacheContext))
.map(param=>CacheContext.wrapParameterToCacheKey(param))
.map(param=>CacheContext._wrapParameterToCacheKey(param))
.join('-');
}
static wrapParameterToCacheKey(param){
static _wrapParameterToCacheKey(param){
if(param === null)return 'null';
if(param === undefined)return 'undefined';
if(typeof param === 'string')return `[str:${param}]`;
@ -159,11 +192,11 @@ export class CacheContext{
if(typeof param === 'boolean')return `[bl:${param}]`;
if(typeof param.getCacheKey == 'function')return param.getCacheKey();
if(Array.isArray(param)){
return `[arr:[${param.filter(p=>!(p instanceof CacheContext)).map(p=>CacheContext.wrapParameterToCacheKey(p)).join('-')}]]`;
return `[arr:[${param.filter(p=>!(p instanceof CacheContext)).map(p=>CacheContext._wrapParameterToCacheKey(p)).join('-')}]]`;
}
if(typeof param === 'function')return `[f:${hex_md5(param.toString())}]`;
let entries = Object.entries(param);
entries.sort((a,b)=>a[0]<b[0]?-1:1);
return `[obj:{${entries.map(e=>e[0]+":"+CacheContext.wrapParameterToCacheKey(e[1])).join(',')}}]`;
return `[obj:{${entries.map(e=>e[0]+":"+CacheContext._wrapParameterToCacheKey(e[1])).join(',')}}]`;
}
}