global.localStorage.js 3.13 KB
/**
 * 20180304 yxt 添加本地缓存机制
 */

/*
 * 定义一个缓存类
 * modelctls URL
 * param 		参数
 * result 		结果
 * */
function CacehKey(url,data,type,result)
{
	this.url = url;
	this.data = data;
	this.type = type;
	
	this.KeyString = function()
	{
		var keyString = JSON.stringify(this.url) + ";" +  JSON.stringify(this.data) + ";" +  JSON.stringify(this.type) ;
		return keyString;
	}
}
//系统级请求本地缓存
function GlobalLocalCache()
{
	this.flag = true;
	//添加缓存
	this.AddCache = function(url,data,type,result)
	{
		if(!window.localStorage){
            console.log("浏览器不支持localstorage");
            return ;
        }else{
        	var key = new CacehKey(url,data,type);
    		var keyString = key.KeyString();
    		var valueString = JSON.stringify(result);
    		if(this.flag){
    			keyString = encodeURI(keyString);
    			valueString = encodeURI(valueString);
    		}
            window.localStorage.setItem(keyString,valueString);
        }
	}
	
	//获取缓存
	this.GetCacheResult = function (url,data,type,result)
	{
		if(!window.localStorage){
            console.log("浏览器不支持localstorage");
            result = null;
            return null;
        }else{
        	var key = new CacehKey(url,data,type);
    		var keyString = key.KeyString();
    		if(this.flag){
    			keyString = encodeURI(keyString);
    		}
            var value = window.localStorage.getItem(keyString);
            if(this.flag){
            	value = decodeURI(value);
            }
            result = JSON.parse(value);
            return result;            
        }
	}
	
	//单个清除缓存
	this.removeLocalCache = function(url,data,type,result)
	{
		if(!window.localStorage){
            console.log("浏览器不支持localstorage");
            return ;
        }else{
        	var key = new CacehKey(url,data,type);
    		var keyString = key.KeyString();
    		if(this.flag){
    			keyString = encodeURI(keyString);
    		}
            window.localStorage.removeItem(keyString);
        }
	}
	
	//清理缓存
	this.clearLocalCache = function ()
	{
		if(!window.localStorage){
            console.log("浏览器不支持localstorage"); 
        }else{
        	
        	window.localStorage.clear();     
        }
	}
	
	//检查缓存版本
	this.checkCacheVersion=function()
	{
		var pdata = {name : "后台配置版本"};
		var localCache = this;
		var curVersion = this.GetCacheResult(CONF_BACK_SERVERURL + global.modelctls.kvtree.all,
				pdata,"");
		$.ajax({
			type : "POST",
			url : CONF_BACK_SERVERURL + global.modelctls.kvtree.all,
			headers : {
				"token" : $.cookie('ftoken')
			},
			dataType : "json",
			async : true,
			data : pdata,
			success : function(data) {
				var curVersionStr = JSON.stringify(curVersion);
				var dataStr = JSON.stringify(data);
				
				if(dataStr != curVersionStr)
				{
					localCache.clearLocalCache();
					localCache.AddCache(CONF_BACK_SERVERURL + global.modelctls.kvtree.all,
							pdata,"",data);
				}
			},
			error : function(data, textStatus, errorThrown) {
				
			}
		});
	}
}
var globalLocalCache = null;

$(function(){
	globalLocalCache = new GlobalLocalCache();
});