/*
    js缓存类
    调用方法。
    var obj=new CacheHelper();
    obj.init();
    obj.addCache(1,"a");
    obj.addCache(2,"b");
    obj.addCache(3,"c");
    obj.addCache(4,"d");
    obj.addCache(5,"e");
    alert(obj.getCacheByKey(3))
*/
function CacheHelper()
{
    this.cache=new Array();
}
           
CacheHelper.prototype={
    init:function() {
      this.cache=null;
        this.cache=new Array();;
    },
    //判断缓存是否存在
    isExist:function(id)
    {
        for(var i=0;i<this.cache.length;i++)
        {
            if(this.cache[i].id==id)
                return true;
        }
        return false;
    },
    //添加缓存
    addCache:function(id,value)
    {
        if(!this.isExist(id))
        {
          var obj={id:id,value:value};
          this.cache.push(obj);
        }
    },
    //取得缓存大小
    getCacheSize:function()
    {
     return this.cache.length; 
    },
    //根据缓存键值取得缓存值
    getCacheByKey:function(id)
    {
     for(var i=0;i<this.cache.length;i++)
        {
            if(this.cache[i].id==id)
                return this.cache[i].value;
        }
        return "";
    }
   
}

相关文章:

  • 2022-02-13
  • 2021-08-08
  • 2021-06-14
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-09
  • 2021-12-21
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
相关资源
相似解决方案