-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_js_cache.js
145 lines (119 loc) · 4.22 KB
/
simple_js_cache.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
/*
2021, N. Frick
(THIS IS NOT THE RECOMMENDED VERSION)
Purpose: a dual-layer cache
Layer 1. in memory (as global var)
Layer 2. or browser localStorage (as JSON string)
Details: if not found in Map "gAppCacheNF", it fetches from localStorage
Usage. This is based on promises, so call using the "consuming code" mode (using .then):
setCacheStorageP(myKey,myObject).then(function() { console.log('saved');});
getCacheStorageP(myKey).then(function(theObj) { myItem = theObj; });
Note - since this is async: set,upsert, and delete may happen before you expect.
( This is the slower, localStorage version.)
*/
function getLocalStorage4P(keyName) { var rs = {};
var objStr = localStorage.getItem(keyName);
if ((objStr != null) && (objStr.length > 0))
{ try {
rs = JSON.parse(objStr);
} catch (ex_var) {
rs = {};
}
}
return rs;
}
var gAppCacheNF = new Map(); // Large Global var to hold cache
/* SET */
var setCacheStorageP = function(keyName,theObject) {
return new Promise(function(resolve,reject) {
gAppCacheNF.set(keyName,theObject);
localStorage.setItem(keyName, JSON.stringify(theObject));
resolve(keyName);
});
}
/* GET */
var getCacheStorageP = function(keyName) {
return new Promise(function(resolve,reject) {
let rs = {};
if (gAppCacheNF.has(keyName))
rs = gAppCacheNF.get(keyName);
else
{ rs = getLocalStorage4P(keyName);
if (Object.keys(rs).length > 0) {
gAppCacheNF.set(keyName,rs); // push to faster (global) var
}
}
resolve(rs,keyName);
});
}
/* UPSERT (UPDATE) */
// combines(overwrites) existing cache object props with new object props
// --> may be used in lieu of setCacheStorageP()
var upsertCacheStorageP = function(keyName,theMixObject) {
return new Promise(function(resolve,reject) {
var rs = {};
if (gAppCacheNF.has(keyName))
rs = gAppCacheNF.get(keyName);
else
rs = getLocalStorage4P(keyName);
var theObject = Object.assign(rs,theMixObject); // combine
gAppCacheNF.set(keyName,theObject); //save
localStorage.setItem(keyName, JSON.stringify(theObject));
resolve(keyName);
});
}
/* DELETE */
// removes item from cache by keyName
var deleteCacheStorageP = function(keyName) {
return new Promise(function(resolve,reject) {
gAppCacheNF.delete(keyName);
localStorage.removeItem(keyName);
resolve(keyName);
});
}
/* DELETE PROPS BY OBJ CONTENTS */
// deletes from existing cache item props that are in specObject
var deletePropObjCacheStorageP = function(keyName,specObject) {
return new Promise(function(resolve,reject) {
var rs = {};
if (gAppCacheNF.has(keyName))
rs = gAppCacheNF.get(keyName);
else
rs = getLocalStorage4P(keyName);
// delete props given in specObject
Object.keys(specObject).forEach(function(key) {
try { delete rs[key] } catch (ex) { }
});
gAppCacheNF.set(keyName,rs); //save
localStorage.setItem(keyName, JSON.stringify(rs));
resolve(keyName);
});
}
/* DELETE PROPS BY NAME (OR ARRAY OF NAMES) */
// deletes from existing cache item props that are specified by propNameArray
var deletePropNameCacheStorageP = function(keyName,propNameArray) {
return new Promise(function(resolve,reject) {
var rs = {};
if (gAppCacheNF.has(keyName))
rs = gAppCacheNF.get(keyName);
else
rs = getLocalStorage4P(keyName);
// delete
if (Array.isArray(propNameArray)) {
var l = propNameArray.length;
for (var i=0;i<l;i++) {
try { delete rs[propNameArray[i]] } catch (ex) { }
}
}
else
{
var l = propNameArray.length;
for (var i=0;i<l;i++) {
try { delete rs[propNameArray] } catch (ex) { }
}
}
gAppCacheNF.set(keyName,rs); //save
localStorage.setItem(keyName, JSON.stringify(rs));
resolve(keyName);
});
}