storage.js
2.73 KB
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
import devCache from "../libs/devCache";
export default {
/**
* 挂载缓存监听
*/
install() {
try {
// #ifdef MP
let that = this;
let _setStorage = uni.setStorage;
uni.setStorage = setStorage;
function setStorage() {
try {
if (
arguments[0]
&& arguments[0].key
&& arguments[0].key.indexOf("devTools_") != 0
) {
that.addCacheKey(arguments[0].key)
}
} catch (error) { }
return _setStorage(...arguments)
}
let _setStorageSync = uni.setStorageSync;
uni.setStorageSync = setStorageSync;
function setStorageSync() {
try {
if (
arguments[0]
&& arguments[0].indexOf("devTools_") != 0
) {
that.addCacheKey(arguments[0])
}
} catch (error) { }
return _setStorageSync(...arguments)
}
let _removeStorage = uni.removeStorage;
uni.removeStorage = removeStorage;
function removeStorage() {
try {
if (
arguments[0]
&& arguments[0].key
&& arguments[0].key.indexOf("devTools_") != 0
) {
that.delCacheKey(arguments[0].key)
}
} catch (error) { }
return _removeStorage(...arguments)
}
let _removeStorageSync = uni.removeStorageSync;
uni.removeStorageSync = removeStorageSync;
function removeStorageSync() {
try {
if (
arguments[0]
&& arguments[0].indexOf("devTools_") != 0
) {
that.delCacheKey(arguments[0])
}
} catch (error) { }
return _removeStorageSync(...arguments)
}
// #endif
} catch (error) {
console.log("devTools storage.install error", error);
}
},
/**
* 添加缓存key
*/
addCacheKey(key) {
try {
if (key && typeof key == "string") {
let storageList = devCache.get("storage")
if (!storageList) storageList = [];
if (storageList.indexOf(key) == -1) {
storageList.push(key)
devCache.set("storage", storageList)
}
}
} catch (error) {
console.log("devTools storage.addCacheKey error", error);
}
},
/**
* 删除指定缓存key
*/
delCacheKey(key) {
try {
if (key && typeof key == "string") {
let storageList = devCache.get("storage")
if (!storageList) storageList = [];
let index = storageList.indexOf(key);
if (index > -1) {
storageList.splice(index, 1)
devCache.set("storage", storageList)
}
}
} catch (error) {
console.log("devTools storage.delCacheKey error", error);
}
},
}