Javascript写一个缓存代理(AOP)

自己写了一个缓存代理,使用代理模式,支持指定函数、支持按参数缓存,原创

后续会支持缓存异步数据

CacheProxy:

复制代码
function CacheProxy(obj,proxyMethods){
        var _cache={};
        var _getType=Object.prototype.toString;  
        var _this = this;
        var _getCacheName=function(methodName,args){
            var caches = [];
            caches.push(methodName);
            for (var i = 0,len=args.length; i <len; i++) {
                caches.push(args[i]);
            }
            return caches.join('_');
        };
        if (_getType.apply(obj)!=="[object Object]") {
            console.info("parameter obj must be object");
            return;
        }
        if (_getType.apply(proxyMethods)!=="[object Array]") {
            if (proxyMethods===undefined ) {
                proxyMethods=[];
            }
            else{
                console.info("parameter proxyMethods must be Array");
                return;                
            }
        }

        for(attr in obj){
            if(_getType.apply(obj[attr])==="[object Function]"){
                (function(attr){
                   _this[attr]=function(){
                    if (proxyMethods.length===0 || proxyMethods.indexOf(attr)>-1) {
                        var cacheName = _getCacheName(attr,_this[attr].arguments);
                        if (_cache[cacheName]===undefined) {
                            return _cache[cacheName];
                        }
                        _cache[cacheName] = obj[attr].apply(_this,arguments);
                        return _cache[cacheName];
                    }
                    
                    return obj[attr].apply(_this,arguments);
                }; 
            })(attr);
                
            }
        }
复制代码

客户端:

复制代码
    function Person(){
        this.sayName=function(a,b,c){
            return "fan";
        };
        this.sayHello=function(a,b){
            return "Hello";
        };
    }

var p = new Person();
var cacheProxy = new CacheProxy(p,["sayName","sayHello"]);//第二个参数可以不写
console.info(cacheProxy.sayName());
console.info(cacheProxy.sayName("1"));
console.info(cacheProxy.sayName("1","2"));
console.info(cacheProxy.sayName("1","2"));//取缓存
console.info(cacheProxy.sayName("1"));//取缓存
 
console.info(cacheProxy.sayHello());
console.info(cacheProxy.sayHello("1"));
console.info(cacheProxy.sayHello("1","2"));
console.info(cacheProxy.sayHello("1","2"));//取缓存
复制代码

 

案例2、缓存函数:

复制代码
    Function.prototype.beforeAsync = function (func) {
        var _self = this;
        return function () {
            func.apply(this, [_self]);
        };
    };
 cacheHandle.CACHE={};
function cacheHandle(func){
        var funcID = func.toString().hash();
        if(cacheHandle.CACHE[funcID] === undefined){
            cacheHandle.CACHE[funcID] = func(); 
        }
        return cacheHandle.CACHE[funcID];
}

//--------------客户端------------ function a(){
return "a"; }; var data = a.beforeAsync(cacheHandle)(); data = a.beforeAsync(cacheHandle)();//取缓存
复制代码

 

posted @   .Neterr  阅读(217)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示