DCS.Common.suggestCookieStore = function(userId,time){
//------------for cookie initialization
var _rsProvider = DCS.Common.getCookieStore(userId,time);
this.userId = userId;
this.KeySeperator = '-.';
this.ItemSeperator = '/.';
var obj = _rsProvider.get(NSConst.Cookie.DCS) || {};
var tmp = obj[this.userId];
obj = {};
obj[this.userId] = tmp; //delete other users cookie
this.dcsObj = obj;
this.store = function(key,obj){
this.clear(key);
var allStr = this.dcsObj[this.userId] || '';
var str = '';
if(allStr && allStr != '')
str += this.ItemSeperator;
str += key+this.KeySeperator+_rsProvider.encodeValue(obj);
var length = str.length;
if((length+DCS.Common.getCookieLength([NSConst.Cookie.DCS])) > 3300)
this.clearLRU();
if((length+DCS.Common.getCookieLength([NSConst.Cookie.DCS])) > 3300)
this.clearLRU();
var allStr = this.dcsObj[this.userId] || '';
allStr += str;
this.dcsObj[this.userId] = allStr;
_rsProvider.set(NSConst.Cookie.DCS,this.dcsObj);
};
this.clearLRU = function(){
var allStr = this.dcsObj[this.userId];
if(allStr && allStr.length > 2){
var start = allStr.indexOf(this.ItemSeperator) ;
if(start > 0 && (start+2) < allStr.length){
allStr = allStr.substring(start+2);
this.dcsObj[this.userId] = allStr;
_rsProvider.set(NSConst.Cookie.DCS,this.dcsObj);
}
}
};
this.get = function(key){
var allStr = this.dcsObj[this.userId];
if(allStr){
if(allStr.length > (key.length+2) && allStr.indexOf(key+this.KeySeperator) > -1){
var keyStr = key+this.KeySeperator;
var start = allStr.indexOf(keyStr);
var keyLength = keyStr.length;
var end = allStr.indexOf(this.ItemSeperator,start);
if(end < 0){
end = allStr.length;
}
var result = allStr.substring(start+keyLength,end);
return _rsProvider.decodeValue(result);
}
}
return null;
};
this.clear = function(key){
var allStr = this.dcsObj[this.userId];
if(allStr){
if(allStr.length > (key.length+2) && allStr.indexOf(key+this.KeySeperator) > -1){
var keyStr = key+this.KeySeperator;
var start = allStr.indexOf(keyStr);
var end = allStr.indexOf(this.ItemSeperator,start);
if(end < 0){
if(start > 2)
start -= 2; //if not the first item, need clear the sepeartor
allStr = allStr.substring(0,start);
}
else{
allStr = allStr.substring(0,start)+allStr.substring(end+2,allStr.length);
}

this.dcsObj[this.userId] = allStr;
_rsProvider.set(NSConst.Cookie.DCS,this.dcsObj);
}
}
};
this.clearAll=function(){
_rsProvider.clear(NSConst.Cookie.DCS);
}
};

 

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()