@peerbit/cache 1.0.1 → 1.1.0
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/lib/esm/index.d.ts +5 -2
- package/lib/esm/index.js +46 -10
- package/lib/esm/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +48 -11
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
export type CacheData<T> = {
|
|
2
2
|
value?: T | null;
|
|
3
3
|
time: number;
|
|
4
|
+
size: number;
|
|
4
5
|
};
|
|
5
6
|
export declare class Cache<T = undefined> {
|
|
6
7
|
private _map;
|
|
7
8
|
private deleted;
|
|
8
9
|
private list;
|
|
10
|
+
currentSize: number;
|
|
11
|
+
deletedSize: number;
|
|
9
12
|
max: number;
|
|
10
13
|
ttl?: number;
|
|
11
14
|
constructor(options: {
|
|
@@ -16,8 +19,8 @@ export declare class Cache<T = undefined> {
|
|
|
16
19
|
get map(): Map<string, CacheData<T>>;
|
|
17
20
|
get(key: string): T | null | undefined;
|
|
18
21
|
trim(time?: number): void;
|
|
19
|
-
del(key: string):
|
|
20
|
-
add(key: string, value?: T): void;
|
|
22
|
+
del(key: string): CacheData<T> | undefined;
|
|
23
|
+
add(key: string, value?: T, size?: number): void;
|
|
21
24
|
clear(): void;
|
|
22
25
|
get size(): number;
|
|
23
26
|
}
|
package/lib/esm/index.js
CHANGED
|
@@ -4,6 +4,8 @@ export class Cache {
|
|
|
4
4
|
_map;
|
|
5
5
|
deleted;
|
|
6
6
|
list;
|
|
7
|
+
currentSize;
|
|
8
|
+
deletedSize;
|
|
7
9
|
max;
|
|
8
10
|
ttl;
|
|
9
11
|
constructor(options) {
|
|
@@ -36,38 +38,72 @@ export class Cache {
|
|
|
36
38
|
let outOfDate = peek &&
|
|
37
39
|
this.ttl !== undefined &&
|
|
38
40
|
this._map.get(peek.value).time < time - this.ttl;
|
|
39
|
-
while (outOfDate || this.
|
|
41
|
+
while (outOfDate || this.currentSize > this.max) {
|
|
40
42
|
const key = this.list.shift();
|
|
41
43
|
if (key !== undefined) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
const cacheValue = this._map.get(key);
|
|
45
|
+
outOfDate = this.ttl !== undefined && cacheValue.time < time - this.ttl;
|
|
44
46
|
this._map.delete(key);
|
|
45
|
-
this.deleted.delete(key);
|
|
47
|
+
const wasDeleted = this.deleted.delete(key);
|
|
48
|
+
if (!wasDeleted) {
|
|
49
|
+
this.currentSize -= cacheValue.size;
|
|
50
|
+
}
|
|
46
51
|
}
|
|
47
52
|
else {
|
|
48
53
|
break;
|
|
49
54
|
}
|
|
50
55
|
}
|
|
51
56
|
}
|
|
57
|
+
/*
|
|
58
|
+
|
|
59
|
+
trim(time = +new Date()) {
|
|
60
|
+
const peek = this.list.head;
|
|
61
|
+
let outOfDate =
|
|
62
|
+
peek &&
|
|
63
|
+
this.ttl !== undefined &&
|
|
64
|
+
this._map.get(peek.value)!.time < time - this.ttl;
|
|
65
|
+
while (outOfDate || this.currentSize > this.max) {
|
|
66
|
+
const key = this.list.shift();
|
|
67
|
+
if (key !== undefined) {
|
|
68
|
+
const cacheValue = this.del(key);
|
|
69
|
+
if (cacheValue) {
|
|
70
|
+
outOfDate = this.ttl !== undefined && cacheValue.time < time - this.ttl;
|
|
71
|
+
this._map.delete(key);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
} else {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
*/
|
|
52
80
|
del(key) {
|
|
53
|
-
this.
|
|
81
|
+
const cacheValue = this._map.get(key);
|
|
82
|
+
if (cacheValue && !this.deleted.has(key)) {
|
|
83
|
+
this.deleted.add(key);
|
|
84
|
+
this.currentSize -= cacheValue.size;
|
|
85
|
+
return cacheValue;
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
54
88
|
}
|
|
55
|
-
add(key, value) {
|
|
89
|
+
add(key, value, size = 1) {
|
|
90
|
+
this.deleted.delete(key);
|
|
56
91
|
const time = +new Date();
|
|
57
|
-
this.trim(time);
|
|
58
92
|
if (!this._map.has(key)) {
|
|
59
93
|
this.list.push(key);
|
|
94
|
+
this.currentSize += size;
|
|
60
95
|
}
|
|
61
|
-
this._map.set(key, { time, value: value ?? null });
|
|
62
|
-
this.
|
|
96
|
+
this._map.set(key, { time, value: value ?? null, size });
|
|
97
|
+
this.trim(time);
|
|
63
98
|
}
|
|
64
99
|
clear() {
|
|
65
100
|
this.list = yallist.create();
|
|
66
101
|
this._map = new Map();
|
|
67
102
|
this.deleted = new Set();
|
|
103
|
+
this.currentSize = 0;
|
|
68
104
|
}
|
|
69
105
|
get size() {
|
|
70
|
-
return this.
|
|
106
|
+
return this.currentSize;
|
|
71
107
|
}
|
|
72
108
|
}
|
|
73
109
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO;AACP,OAAO,OAAO,MAAM,SAAS,CAAC;AAG9B,MAAM,OAAO,KAAK;IACT,IAAI,CAA4B;IAChC,OAAO,CAAc;IACrB,IAAI,CAAkB;IAC9B,GAAG,CAAS;IACZ,GAAG,CAAU;IACb,YAAY,OAAsC;QACjD,IAAI,OAAO,CAAC,GAAG,IAAI,CAAC,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;SACtC;QACD,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;QACvB,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;QACvB,IAAI,CAAC,KAAK,EAAE,CAAC;IACd,CAAC;IACD,GAAG,CAAC,GAAW;QACd,IAAI,CAAC,IAAI,EAAE,CAAC;QACZ,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAC1B,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I,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;IACD,KAAK;QACJ,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;QAC7B,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QACzB,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC;IACtB,CAAC;IAED,IAAI,IAAI;QACP,OAAO,IAAI,CAAC,WAAW,CAAC;IACzB,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/cache",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Simple cache",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"license": "MIT",
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@peerbit/time": "1.0.
|
|
37
|
+
"@peerbit/time": "1.0.3",
|
|
38
38
|
"@types/yallist": "^4.0.1"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"localMaintainers": [
|
|
44
44
|
"dao.xyz"
|
|
45
45
|
],
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "0cfa376bc90c31e1063ddaf5435c828b490e0228"
|
|
47
47
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
// Fifo
|
|
2
2
|
import yallist from "yallist";
|
|
3
3
|
|
|
4
|
-
export type CacheData<T> = { value?: T | null; time: number };
|
|
4
|
+
export type CacheData<T> = { value?: T | null; time: number; size: number };
|
|
5
5
|
export class Cache<T = undefined> {
|
|
6
6
|
private _map: Map<string, CacheData<T>>;
|
|
7
7
|
private deleted: Set<string>;
|
|
8
8
|
private list: yallist<string>;
|
|
9
|
+
currentSize: number;
|
|
10
|
+
deletedSize: number;
|
|
11
|
+
|
|
9
12
|
max: number;
|
|
10
13
|
ttl?: number;
|
|
11
14
|
constructor(options: { max: number; ttl?: number }) {
|
|
@@ -42,39 +45,73 @@ export class Cache<T = undefined> {
|
|
|
42
45
|
peek &&
|
|
43
46
|
this.ttl !== undefined &&
|
|
44
47
|
this._map.get(peek.value)!.time < time - this.ttl;
|
|
45
|
-
while (outOfDate || this.
|
|
48
|
+
while (outOfDate || this.currentSize > this.max) {
|
|
46
49
|
const key = this.list.shift();
|
|
47
50
|
if (key !== undefined) {
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
const cacheValue = this._map.get(key)!;
|
|
52
|
+
outOfDate = this.ttl !== undefined && cacheValue.time < time - this.ttl;
|
|
50
53
|
this._map.delete(key);
|
|
51
|
-
this.deleted.delete(key);
|
|
54
|
+
const wasDeleted = this.deleted.delete(key);
|
|
55
|
+
if (!wasDeleted) {
|
|
56
|
+
this.currentSize -= cacheValue.size;
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/*
|
|
64
|
+
|
|
65
|
+
trim(time = +new Date()) {
|
|
66
|
+
const peek = this.list.head;
|
|
67
|
+
let outOfDate =
|
|
68
|
+
peek &&
|
|
69
|
+
this.ttl !== undefined &&
|
|
70
|
+
this._map.get(peek.value)!.time < time - this.ttl;
|
|
71
|
+
while (outOfDate || this.currentSize > this.max) {
|
|
72
|
+
const key = this.list.shift();
|
|
73
|
+
if (key !== undefined) {
|
|
74
|
+
const cacheValue = this.del(key);
|
|
75
|
+
if (cacheValue) {
|
|
76
|
+
outOfDate = this.ttl !== undefined && cacheValue.time < time - this.ttl;
|
|
77
|
+
this._map.delete(key);
|
|
78
|
+
}
|
|
79
|
+
|
|
52
80
|
} else {
|
|
53
81
|
break;
|
|
54
82
|
}
|
|
55
83
|
}
|
|
56
84
|
}
|
|
85
|
+
*/
|
|
57
86
|
|
|
58
87
|
del(key: string) {
|
|
59
|
-
this.
|
|
88
|
+
const cacheValue = this._map.get(key)!;
|
|
89
|
+
if (cacheValue && !this.deleted.has(key)) {
|
|
90
|
+
this.deleted.add(key);
|
|
91
|
+
this.currentSize -= cacheValue.size;
|
|
92
|
+
return cacheValue;
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
60
95
|
}
|
|
61
96
|
|
|
62
|
-
add(key: string, value?: T) {
|
|
97
|
+
add(key: string, value?: T, size = 1) {
|
|
98
|
+
this.deleted.delete(key);
|
|
63
99
|
const time = +new Date();
|
|
64
|
-
this.trim(time);
|
|
65
100
|
if (!this._map.has(key)) {
|
|
66
101
|
this.list.push(key);
|
|
102
|
+
this.currentSize += size;
|
|
67
103
|
}
|
|
68
|
-
this._map.set(key, { time, value: value ?? null });
|
|
69
|
-
this.
|
|
104
|
+
this._map.set(key, { time, value: value ?? null, size });
|
|
105
|
+
this.trim(time);
|
|
70
106
|
}
|
|
71
107
|
clear() {
|
|
72
108
|
this.list = yallist.create();
|
|
73
109
|
this._map = new Map();
|
|
74
110
|
this.deleted = new Set();
|
|
111
|
+
this.currentSize = 0;
|
|
75
112
|
}
|
|
76
113
|
|
|
77
114
|
get size() {
|
|
78
|
-
return this.
|
|
115
|
+
return this.currentSize;
|
|
79
116
|
}
|
|
80
117
|
}
|