-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore_encode.js
311 lines (287 loc) · 11.8 KB
/
store_encode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.store = factory();
}
}(this, function () {
"use strict";
var _maxExpireDate = new Date('Fri, 31 Dec 9999 23:59:59 UTC'),
_serialize=function (item) {
return JSON.stringify(item);
},
_deserialize=function (data) {
return data && JSON.parse(data);
},
_isSupported=function(storage) {
var supported = false;
if (storage && storage.setItem ) {
supported = true;
var key = '__' + Math.round(Math.random() * 1e7);
try {
storage.setItem(key, key);
storage.removeItem(key);
} catch (err) {
supported = false;
}
}
return supported;
},
_getStorage=function(storage) {
if (typeof(storage) === 'string') {
return window[storage];
}
return storage;
},
_isDate=function(date) {
return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
},
_getExpDate=function(expires, now) {
now = now || new Date();
if (typeof expires === 'number') {
expires = expires === Infinity ?
_maxExpireDate : new Date(now.getTime() + expires * 1000);
} else if (typeof expires === 'string') {
expires = new Date(expires);
}
if (expires && !_isDate(expires)) {
throw new Error('`expires` parameter cannot be converted to a valid Date instance');
}
return expires;
},
_cacheItem=function(value, exp) {
exp = exp || _maxExpireDate;
return {
c : new Date().getTime(),
e : _getExpDate(exp).getTime(),
v: compress(value)
}
};
function Store() {
this.storage = _getStorage('localStorage');
}
//默认不支持localStorage的不报错,包含隐私模式下同样的不报错,让页面正常运行
Store.prototype={
constructor:Store,
set: function () {},
get: function () {},
del: function () {},
clearExp:function(){},
clearAll:function(){},
add:function(){},
setExp:function(){},
getExp:function(){}
};
if (_isSupported(_getStorage('localStorage'))) {
Store.prototype={
constructor:Store,
/**
* 设置缓存,可以设置过期时间,单位:秒
* @param key 缓存名称
* @param val 缓存的值,如果未定义或为null,则删除该缓存
* @param exp 缓存的过期时间
* @returns {*}
*/
set: function(key, val, exp) {
if (typeof(key) !== 'string') {
console.warn(key + ' used as a key, but it is not a string.');
key = ''+key;
}
if (val === undefined || val===null) {
return this.del(key);
}
try {
this.storage.setItem(key, _serialize(_cacheItem(_serialize(val), exp)));
} catch (e) {
console.error(e);
}
return val;
},
/**
* 获取缓存,如果已经过期,会主动删除缓存,并返回null
* @param key 缓存名称
* @returns {*} 缓存的值,默认已经做好序列化
*/
get: function (key) {
var item = _deserialize(this.storage.getItem(key));
if(item !== null && item !== undefined) {
var timeNow = new Date().getTime();
if(timeNow < item.e) {
return _deserialize(deCompress(item.v));
} else {
this.del(key);
}
}
return null;
},
/**
* 删除指定的缓存
* @param key 要删除缓存的主键
* @returns {*}
*/
del: function (key) {
this.storage.removeItem(key);
return key;
},
/**
* 删除所有过期的缓存
* @returns {Array}
*/
clearExp: function() {
var length = this.storage.length,
caches = [],
_this = this;
for (var i = 0; i < length; i++) {
var key = this.storage.key(i),
item = _deserialize(this.get(key));
if(item && !item.e) {
if(new Date().getTime() >= item.e) {
caches.push(key);
}
}
}
caches.forEach(function(key) {
_this.del(key);
});
return caches;
},
/**
* 清空缓存
*/
clearAll: function () {
this.storage.clear();
},
/**
* 添加一条缓存,如果已经存在同样名称的缓存,则不做任何处理
* @param key 缓存的名称
* @param value 缓存的值
* @param exp 过期时间 单位:秒
*/
add: function (key, value, exp) {
if(this.get(key) === null || this.get(key) === undefined) {
this.set(key, value, exp);
}
},
/**
* 设置修改缓存的过期时间
* @param key 缓存的名称
* @param exp 新的过期时间 单位:秒
*/
setExp: function (key, exp) {
var item = this.get(key);
if(item !== null && item !== undefined) {
item.e = _getExpDate(exp,item.c);
this.set(key,_serialize(item),exp);
}
},
/**
* 获取缓存的过期时间
* @param key 缓存的名称
* @returns {*} 单位:秒
*/
getExp: function(key){
var item =this.get(key);
return item && item.e;
}
}
}
return new Store();
}));
(function(root){
function toUnicode(str){
if(!str) return '';
return str.toString().replace(/[^\x00-\xff]/g,function(s){
return '\\u'+s.charCodeAt(0).toString(16);
});
}
function unicode2Char(str){
if(!str) return '';
return str.replace(/\\u(\w{1,4})/g,function(a,s){
return String.fromCharCode(parseInt(s,16));
});
}
root.compress=function(strNormalString) {
var strCompressedString = "",
intLeftOver = 0,
intOutputCode = 0,
pcode = 0,
ccode = 0,
k = 0,
strNormalString=toUnicode(strNormalString),
len = strNormalString.length,
getCode=String.fromCharCode;
for (var i = 0; i < len; i++) {
ccode = strNormalString.charCodeAt(i);
k = (pcode << 8) | ccode;
intLeftOver += 12;
intOutputCode <<= 12;
intOutputCode |= pcode;
pcode = ccode;
if (intLeftOver >= 16) {
strCompressedString += getCode(intOutputCode >> (intLeftOver - 16));
intOutputCode &= (Math.pow(2, (intLeftOver - 16)) - 1);
intLeftOver -= 16;
}
}
if (pcode != 0) {
intLeftOver += 12;
intOutputCode <<= 12;
intOutputCode |= pcode;
}
if (intLeftOver >= 16) {
strCompressedString += getCode(intOutputCode >> (intLeftOver - 16));
intOutputCode &= (Math.pow(2, (intLeftOver - 16)) - 1);
intLeftOver -= 16;
}
if (intLeftOver > 0) {
intOutputCode <<= (16 - intLeftOver);
strCompressedString += getCode(intOutputCode);
}
return strCompressedString;
};
root.deCompress=function(strCompressedString){
var strNormalString = "",
ht = [],
used = 128,
intLeftOver = 0,
intOutputCode = 0,
ccode = 0,
pcode = 0,
key = 0,
len = strCompressedString.length,
getCode=String.fromCharCode;
for (var i = 0; i < used; i++) {
ht[i] = getCode(i);
}
for (i = 0; i < len; i++) {
intLeftOver += 16;
intOutputCode <<= 16;
intOutputCode |= strCompressedString.charCodeAt(i);
while (1) {
if (intLeftOver >= 12) {
ccode = intOutputCode >> (intLeftOver - 12);
if (typeof (key = ht[ccode]) != "undefined") {
strNormalString += key;
if (used > 128) {
ht[ht.length] = ht[pcode] + key.substr(0, 1);
}
pcode = ccode;
}else {
key = ht[pcode] + ht[pcode].substr(0, 1);
strNormalString += key;
ht[ht.length] = ht[pcode] + key.substr(0, 1);
pcode = ht.length - 1;
}
used++;
intLeftOver -= 12;
intOutputCode &= (Math.pow(2, intLeftOver) - 1);
}else {
break;
}
}
}
return unicode2Char(strNormalString.substr(1,strNormalString.length-2));
}
}(this));