@smartico/public-api 0.0.201 → 0.0.202
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/NodeCache.d.ts +2 -2
- package/dist/index.js +23 -13
- package/dist/index.js.map +1 -1
- package/dist/index.modern.mjs +22 -13
- package/dist/index.modern.mjs.map +1 -1
- package/package.json +1 -1
- package/src/NodeCache.ts +18 -14
- package/src/OCache.ts +5 -0
package/package.json
CHANGED
package/src/NodeCache.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
class NodeCache {
|
|
2
|
-
private
|
|
2
|
+
private ttlChecker: NodeJS.Timeout;
|
|
3
3
|
|
|
4
|
-
private
|
|
4
|
+
private cache: { [key: string]: any } = {};
|
|
5
5
|
|
|
6
|
-
constructor() {
|
|
7
|
-
if (
|
|
8
|
-
|
|
6
|
+
constructor() {
|
|
7
|
+
if (this.ttlChecker === undefined) {
|
|
8
|
+
this.ttlChecker = setInterval(() => {
|
|
9
9
|
const now = new Date().getTime();
|
|
10
|
-
for (const key in
|
|
11
|
-
if (
|
|
12
|
-
const o =
|
|
10
|
+
for (const key in this.cache) {
|
|
11
|
+
if (this.cache.hasOwnProperty(key)) {
|
|
12
|
+
const o = this.cache[key];
|
|
13
13
|
if (o.ttl < now) {
|
|
14
|
-
delete
|
|
14
|
+
delete this.cache[key];
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
}
|
|
@@ -20,27 +20,31 @@ class NodeCache {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
public get(key: string): any {
|
|
23
|
-
const o =
|
|
23
|
+
const o = this.cache[key];
|
|
24
24
|
if (o !== undefined && o.ttl > new Date().getTime()) {
|
|
25
25
|
return o.value;
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
public set(key: string, value: any, ttlSeconds: number = 60) {
|
|
30
|
-
|
|
30
|
+
this.cache[key] = {
|
|
31
31
|
value,
|
|
32
32
|
ttl: new Date().getTime() + ttlSeconds * 1000,
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
public remove(key: string) {
|
|
37
|
-
if (
|
|
38
|
-
delete
|
|
37
|
+
if (this.cache.hasOwnProperty(key)) {
|
|
38
|
+
delete this.cache[key];
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
public flushAll() {
|
|
43
|
-
|
|
43
|
+
this.cache = {};
|
|
44
|
+
if (this.ttlChecker) {
|
|
45
|
+
clearInterval(this.ttlChecker);
|
|
46
|
+
this.ttlChecker = undefined
|
|
47
|
+
}
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
50
|
|
package/src/OCache.ts
CHANGED