@peerbit/cache 1.1.1 → 2.0.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.js +15 -36
- package/lib/esm/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +15 -37
package/lib/esm/index.js
CHANGED
|
@@ -34,19 +34,21 @@ export class Cache {
|
|
|
34
34
|
return this._map.get(key)?.value;
|
|
35
35
|
}
|
|
36
36
|
trim(time = +new Date()) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
37
|
+
for (;;) {
|
|
38
|
+
const headKey = this.list.head;
|
|
39
|
+
if (headKey?.value !== undefined) {
|
|
40
|
+
const cacheValue = this._map.get(headKey.value);
|
|
41
|
+
const outOfDate = this.ttl !== undefined && cacheValue.time < time - this.ttl;
|
|
42
|
+
if (outOfDate || this.currentSize > this.max) {
|
|
43
|
+
this.list.shift();
|
|
44
|
+
this._map.delete(headKey.value);
|
|
45
|
+
const wasDeleted = this.deleted.delete(headKey.value);
|
|
46
|
+
if (!wasDeleted) {
|
|
47
|
+
this.currentSize -= cacheValue.size;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
break;
|
|
50
52
|
}
|
|
51
53
|
}
|
|
52
54
|
else {
|
|
@@ -54,29 +56,6 @@ export class Cache {
|
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
}
|
|
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
|
-
*/
|
|
80
59
|
del(key) {
|
|
81
60
|
const cacheValue = this._map.get(key);
|
|
82
61
|
if (cacheValue && !this.deleted.has(key)) {
|
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,WAAW,CAAS;IACpB,WAAW,CAAS;IAEpB,GAAG,CAAS;IACZ,GAAG,CAAU;IACb,YAAY,OAAsC;QACjD,IAAI,OAAO,CAAC,GAAG,IAAI,CAAC,EAAE;
|
|
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,WAAW,CAAS;IACpB,WAAW,CAAS;IAEpB,GAAG,CAAS;IACZ,GAAG,CAAU;IACb,YAAY,OAAsC;QACjD,IAAI,OAAO,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACvC,CAAC;QACD,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CA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D,IAAI,CAAC,IAAI,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": "
|
|
3
|
+
"version": "2.0.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": "
|
|
37
|
+
"@peerbit/time": "2.0.0",
|
|
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": "c48cb37d237a25b0bcc849482b43f6941d53e3d5"
|
|
47
47
|
}
|
package/src/index.ts
CHANGED
|
@@ -40,49 +40,27 @@ export class Cache<T = undefined> {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
trim(time = +new Date()) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
43
|
+
for (;;) {
|
|
44
|
+
const headKey = this.list.head;
|
|
45
|
+
if (headKey?.value !== undefined) {
|
|
46
|
+
const cacheValue = this._map.get(headKey.value)!;
|
|
47
|
+
const outOfDate =
|
|
48
|
+
this.ttl !== undefined && cacheValue.time < time - this.ttl;
|
|
49
|
+
if (outOfDate || this.currentSize > this.max) {
|
|
50
|
+
this.list.shift();
|
|
51
|
+
this._map.delete(headKey.value);
|
|
52
|
+
const wasDeleted = this.deleted.delete(headKey.value);
|
|
53
|
+
if (!wasDeleted) {
|
|
54
|
+
this.currentSize -= cacheValue.size;
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
break;
|
|
57
58
|
}
|
|
58
59
|
} else {
|
|
59
60
|
break;
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
}
|
|
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
|
-
|
|
80
|
-
} else {
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
*/
|
|
86
64
|
|
|
87
65
|
del(key: string) {
|
|
88
66
|
const cacheValue = this._map.get(key)!;
|