Cache(缓存)架构 性能优化

缓存的概念以及用途

缓存的目的是为了尽量降低获取资源或者数据的时间成本

客户端缓存内容分类
  • 资源缓存 (图片、脚本、样式等)
  • 数据缓存 (dom数据、对象数据等)

解决: 内存缓存 --> 文件缓存 --> 数据库缓存

缓存简单架构实现
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        //缓存架构应该满足
            //1.兼容性
            //2.开闭原则
            //3.扩展性(尽量满足多种存储)
            //4.应用起来方便
        (function(global,factory,name){
            //函数体
            return global[name] = factory.call(global)
        })(this,function(){         //this 动态指针引用 // this window //node.js modules //sea.js define   
            var __E_TYPES__ = {
                MEMORY:'MEMORY'
            };

            //缓存引擎
            var __ENGINES__ = {
                [__E_TYPES__.MEMORY]:{
                    init:function(){
                        this.pools = this.pools||{}
                    },
                    set:function(options){
                        this.pools || this.init();
                        this.pools[options.key]=options.data
                    },
                    get:function(options){
                        this.pools || this.init();
                        return this.pools[options.key]
                    },
                    remove:function(){
                        delete this.pools[options.key]
                    }
                },
            };
            
            
            
            //保护架构的核心功能
            var __CACHE__ = {
                install:function(type,object){
                    __E_TYPES__[type]=type;
                    __ENGINES__[type]=object;
                    __ENGINES__[type].init();
                },
                uninstall:function(){},
                set:function(type,ops){
                    __ENGINES__[type].set(ops)        
                },
                get:function(type,ops){
                    return __ENGINES__[type].get(ops)
                },
                remove:function(){
                    return __ENGINES__[type||__E_TYPES__.MEMORY].get(ops)
                }
            };

            //__CACHE__.install(__ENGINES__.MEMORY);
            return {
                TYPES:__E_TYPES__,
                getInstance:function(){
                    return __CACHE__
                }
            }

        },'MyCache'); 
        var NEC = MyCache.getInstance();
        // NEC.install('LOCAL_STORAGE',{
        //     init:function(){},
        //     set:function(){},
        //     get:function(){
        //         console.log("自调get")
        //     }

        // });
        // console.log(MyCache.TYPES,)
        NEC.set(MyCache.TYPES.MEMORY,{
            key:'jack',
            data:{height:144,wight:100}
        })
        console.log(NEC.get(MyCache.TYPES.MEMORY,{key:'jack'}))
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值