|
@@ -0,0 +1,217 @@
|
|
1
|
+import {
|
|
2
|
+ EXPIRE
|
|
3
|
+} from '../config/app';
|
|
4
|
+
|
|
5
|
+class Cache {
|
|
6
|
+
|
|
7
|
+ constructor(handler) {
|
|
8
|
+ this.cacheSetHandler = uni.setStorageSync;
|
|
9
|
+ this.cacheGetHandler = uni.getStorageSync;
|
|
10
|
+ this.cacheClearHandler = uni.removeStorageSync;
|
|
11
|
+ this.cacheExpire = 'UNI-APP-CRMEB:TAG';
|
|
12
|
+ this.clearOverdue();
|
|
13
|
+ }
|
|
14
|
+
|
|
15
|
+ /**
|
|
16
|
+ * 获取当前时间戳
|
|
17
|
+ */
|
|
18
|
+ time() {
|
|
19
|
+ return Math.round(new Date() / 1000);
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ /**
|
|
23
|
+ * 字符串转时间戳
|
|
24
|
+ * @param {Object} expiresTime
|
|
25
|
+ */
|
|
26
|
+ strTotime(expiresTime){
|
|
27
|
+ let expires_time = expiresTime.substring(0, 19);
|
|
28
|
+ expires_time = expires_time.replace(/-/g, '/');
|
|
29
|
+ return Math.round(new Date(expires_time).getTime() / 1000);
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+ /**
|
|
34
|
+ * 设置过期时间缓存
|
|
35
|
+ * @param {Object} key
|
|
36
|
+ * @param {Object} expire
|
|
37
|
+ */
|
|
38
|
+ setExpireCaheTag(key, expire) {
|
|
39
|
+ expire = expire !== undefined ? expire : EXPIRE;
|
|
40
|
+ if (typeof expire === 'number') {
|
|
41
|
+ let tag = this.cacheGetHandler(this.cacheExpire), newTag = [],newKeys = [];
|
|
42
|
+ if (typeof tag === 'object' && tag.length) {
|
|
43
|
+ newTag = tag.map(item => {
|
|
44
|
+ newKeys.push(item.key);
|
|
45
|
+ if (item.key === key) {
|
|
46
|
+ item.expire = expire === 0 ? 0 : this.time() + expire;
|
|
47
|
+ }
|
|
48
|
+ return item;
|
|
49
|
+ });
|
|
50
|
+ }
|
|
51
|
+ if (!newKeys.length || newKeys.indexOf(key) === -1) {
|
|
52
|
+ newTag.push({
|
|
53
|
+ key: key,
|
|
54
|
+ expire: expire === 0 ? 0 : this.time() + expire
|
|
55
|
+ });
|
|
56
|
+ }
|
|
57
|
+ this.cacheSetHandler(this.cacheExpire, newTag);
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ /**
|
|
62
|
+ * 缓存是否过期,过期自动删除
|
|
63
|
+ * @param {Object} key
|
|
64
|
+ * @param {Object} $bool true = 删除,false = 不删除
|
|
65
|
+ */
|
|
66
|
+ getExpireCahe(key, $bool) {
|
|
67
|
+ try {
|
|
68
|
+ let tag = this.cacheGetHandler(this.cacheExpire),time = 0,index = false;
|
|
69
|
+ if (typeof tag === 'object' && tag.length) {
|
|
70
|
+ tag.map((item,i) => {
|
|
71
|
+ if(item.key === key){
|
|
72
|
+ time = item.expire
|
|
73
|
+ index = i
|
|
74
|
+ }
|
|
75
|
+ });
|
|
76
|
+ if (time) {
|
|
77
|
+ let newTime = parseInt(time);
|
|
78
|
+ if (time && time < this.time() && !Number.isNaN(newTime)) {
|
|
79
|
+ if ($bool === undefined || $bool === true) {
|
|
80
|
+ this.cacheClearHandler(key);
|
|
81
|
+ if(index !== false){
|
|
82
|
+ tag.splice(index,1)
|
|
83
|
+ this.cacheClearHandler(this.cacheExpire,tag);
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+ return false;
|
|
87
|
+ } else
|
|
88
|
+ return true;
|
|
89
|
+ } else {
|
|
90
|
+ return !!this.cacheGetHandler(key);
|
|
91
|
+ }
|
|
92
|
+ }
|
|
93
|
+ return false;
|
|
94
|
+ } catch (e) {
|
|
95
|
+ return false;
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ /**
|
|
100
|
+ * 设置缓存
|
|
101
|
+ * @param {Object} key
|
|
102
|
+ * @param {Object} data
|
|
103
|
+ */
|
|
104
|
+ set(key, data, expire) {
|
|
105
|
+ if (data === undefined) {
|
|
106
|
+ return true;
|
|
107
|
+ }
|
|
108
|
+ if (typeof data === 'object')
|
|
109
|
+ data = JSON.stringify(data);
|
|
110
|
+ try {
|
|
111
|
+ this.setExpireCaheTag(key, expire);
|
|
112
|
+ return this.cacheSetHandler(key, data);
|
|
113
|
+ } catch (e) {
|
|
114
|
+ console.log(e);
|
|
115
|
+ return false;
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ /**
|
|
120
|
+ * 检测缓存是否存在
|
|
121
|
+ * @param {Object} key
|
|
122
|
+ */
|
|
123
|
+ has(key) {
|
|
124
|
+ this.clearOverdue();
|
|
125
|
+ return this.getExpireCahe(key);
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ /**
|
|
129
|
+ * 获取缓存
|
|
130
|
+ * @param {Object} key
|
|
131
|
+ * @param {Object} $default
|
|
132
|
+ * @param {Object} expire
|
|
133
|
+ */
|
|
134
|
+ get(key, $default, expire) {
|
|
135
|
+ this.clearOverdue();
|
|
136
|
+ try {
|
|
137
|
+ let isBe = this.getExpireCahe(key);
|
|
138
|
+ let data = this.cacheGetHandler(key);
|
|
139
|
+ if (data && isBe) {
|
|
140
|
+ if (typeof $default === 'boolean')
|
|
141
|
+ return JSON.parse(data);
|
|
142
|
+ else
|
|
143
|
+ return data;
|
|
144
|
+ } else {
|
|
145
|
+ if (typeof $default === 'function') {
|
|
146
|
+ let value = $default();
|
|
147
|
+ this.set(key, value, expire);
|
|
148
|
+ return value;
|
|
149
|
+ } else {
|
|
150
|
+ this.set(key, $default, expire);
|
|
151
|
+ return $default;
|
|
152
|
+ }
|
|
153
|
+ }
|
|
154
|
+ } catch (e) {
|
|
155
|
+ return null;
|
|
156
|
+ }
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ /**
|
|
160
|
+ * 删除缓存
|
|
161
|
+ * @param {Object} key
|
|
162
|
+ */
|
|
163
|
+ clear(key) {
|
|
164
|
+ try {
|
|
165
|
+ let cahceValue = this.cacheGetHandler(this.cacheExpire),
|
|
166
|
+ index = false;
|
|
167
|
+ if (cahceValue && typeof cahceValue === 'object' && cahceValue.length) {
|
|
168
|
+ cahceValue.map((item, i) => {
|
|
169
|
+ if (item.key === key) {
|
|
170
|
+ index = i;
|
|
171
|
+ }
|
|
172
|
+ });
|
|
173
|
+
|
|
174
|
+ if (index !== false) {
|
|
175
|
+ cahceValue.splice(index, 1);
|
|
176
|
+ }
|
|
177
|
+ this.cacheSetHandler(this.cacheExpire, cahceValue);
|
|
178
|
+ }
|
|
179
|
+ return this.cacheClearHandler(key);
|
|
180
|
+ } catch (e) {
|
|
181
|
+ return false;
|
|
182
|
+ }
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ /**
|
|
186
|
+ * 清除过期缓存
|
|
187
|
+ */
|
|
188
|
+ clearOverdue() {
|
|
189
|
+ let cahceValue = this.cacheGetHandler(this.cacheExpire),
|
|
190
|
+ time = this.time(),
|
|
191
|
+ newBeOverdueValue = [],
|
|
192
|
+ newTagValue = [];
|
|
193
|
+
|
|
194
|
+ if (cahceValue && typeof cahceValue === 'object' && cahceValue.length) {
|
|
195
|
+ cahceValue.map(item => {
|
|
196
|
+ if (item) {
|
|
197
|
+ if ((item.expire !== undefined && item.expire > time) || item.expire === 0) {
|
|
198
|
+ newTagValue.push(item);
|
|
199
|
+ } else {
|
|
200
|
+ newBeOverdueValue.push(item.key);
|
|
201
|
+ }
|
|
202
|
+ }
|
|
203
|
+ });
|
|
204
|
+ }
|
|
205
|
+ //保存没有过期的缓存标签
|
|
206
|
+ if (newTagValue.length !== cahceValue.length) {
|
|
207
|
+ this.cacheSetHandler(this.cacheExpire, newTagValue);
|
|
208
|
+ }
|
|
209
|
+ //删除过期缓存
|
|
210
|
+ newBeOverdueValue.forEach(k => {
|
|
211
|
+ this.cacheClearHandler(k);
|
|
212
|
+ })
|
|
213
|
+ }
|
|
214
|
+}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+export default new Cache;
|