js中加入數據緩存


因為我們的系統設計 所有的數據查詢全部是采用參數化json 后台解析后進行數據返回

由於使用統一的數據查詢入口 所有可以很方便的為數據設置緩存

var ModelDataCache = new Array();
//根據搜索模型獲取緩存
function GetDataCache(whereStr) {

   var data= $.grep(ModelDataCache, function (value) {
       return value.whereStr == whereStr && new Date() - value.addTime < 60000;
   })
   if (NoNull(data)) {
       return data[0].Data;
   } else {
       return null;
   }

}
//根據搜索模型設置緩存
function SetDataCache(whereStr, Data) {


    for (var i = 0; i < ModelDataCache.length; i++) {
        if (ModelDataCache[i].whereStr == whereStr) {
            ModelDataCache[i].Data = Data;

      ModelDataCache[i].addTime = new Data();

            return;
        }

    }
    ModelDataCache.push({
        "whereStr": whereStr,
        "Data": Data,
        "addTime": new Date()
    });


}

  //統一的異步請求接口 每當請求該接口時 清空緩存

function agAjax(url, type, data) {
    //	url='/Agent.ashx?urlstr=http://192.168.31.99:99/'+url;
    var ajaxData = null;
    $.ajax({
        type: type,
        async: false,
        url: url,
        data: data,
        dataType: 'json',
        success: function (data) {
            if (data.state != 'success') {
                tips(data.content);
            }
            ajaxData = data;

        },
        error: function () {

        }
    });
    ModelDataCache = new Array();
    return ajaxData;
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM