当前位置: 代码迷 >> Web前端 >> 前端优化,自定义逾期缓存
  详细解决方案

前端优化,自定义逾期缓存

热度:230   发布时间:2013-08-13 16:43:28.0
前端优化,自定义过期缓存
  var customCache = {};
  /**
   * 自定义缓存
   * @param key 缓存key
   * @param value 内容
   * @param minutes 失效时间(默认1分钟过期)
   */
  setCache = function(key,value,minutes){
      if(!!key && !!value){
          var expDt = new Date();
          expDt.setMinutes(expDt.getMinutes() + (minutes || 1));  
          customCache[key] = JSON.stringify({
              value: value,
              expires: expDt.getTime()
          });
      }
  }
  getCache = function(key){
      if(!customCache[key])return null;
      var item = $.parseJSON(customCache[key]);  
      if(new Date().getTime() > item.expires){
          delete customCache[key];
      }
      return !customCache[key]? null : item.value;
  }
  相关解决方案