@thi.ng/cache 2.1.81 → 2.1.83
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/CHANGELOG.md +1 -1
- package/api.js +0 -1
- package/lru.js +144 -139
- package/mru.js +23 -21
- package/package.json +10 -7
- package/tlru.js +72 -84
package/CHANGELOG.md
CHANGED
package/api.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lru.js
CHANGED
|
@@ -1,143 +1,148 @@
|
|
|
1
1
|
import { DCons } from "@thi.ng/dcons/dcons";
|
|
2
2
|
import { map } from "@thi.ng/transducers/map";
|
|
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
|
-
|
|
3
|
+
class LRUCache {
|
|
4
|
+
map;
|
|
5
|
+
items;
|
|
6
|
+
opts;
|
|
7
|
+
_size;
|
|
8
|
+
constructor(pairs, opts) {
|
|
9
|
+
const _opts = Object.assign(
|
|
10
|
+
{
|
|
11
|
+
maxlen: Infinity,
|
|
12
|
+
maxsize: Infinity,
|
|
13
|
+
map: () => /* @__PURE__ */ new Map(),
|
|
14
|
+
ksize: () => 0,
|
|
15
|
+
vsize: () => 0
|
|
16
|
+
},
|
|
17
|
+
opts
|
|
18
|
+
);
|
|
19
|
+
this.map = _opts.map();
|
|
20
|
+
this.items = new DCons();
|
|
21
|
+
this._size = 0;
|
|
22
|
+
this.opts = _opts;
|
|
23
|
+
if (pairs) {
|
|
24
|
+
this.into(pairs);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
get length() {
|
|
28
|
+
return this.items.length;
|
|
29
|
+
}
|
|
30
|
+
get size() {
|
|
31
|
+
return this._size;
|
|
32
|
+
}
|
|
33
|
+
[Symbol.iterator]() {
|
|
34
|
+
return this.entries();
|
|
35
|
+
}
|
|
36
|
+
entries() {
|
|
37
|
+
return map((e) => [e.k, e], this.items);
|
|
38
|
+
}
|
|
39
|
+
keys() {
|
|
40
|
+
return map((e) => e.k, this.items);
|
|
41
|
+
}
|
|
42
|
+
values() {
|
|
43
|
+
return map((e) => e.v, this.items);
|
|
44
|
+
}
|
|
45
|
+
copy() {
|
|
46
|
+
const c = this.empty();
|
|
47
|
+
c.items = this.items.copy();
|
|
48
|
+
let cell = c.items.head;
|
|
49
|
+
while (cell) {
|
|
50
|
+
c.map.set(cell.value.k, cell);
|
|
51
|
+
cell = cell.next;
|
|
52
|
+
}
|
|
53
|
+
return c;
|
|
54
|
+
}
|
|
55
|
+
empty() {
|
|
56
|
+
return new LRUCache(null, this.opts);
|
|
57
|
+
}
|
|
58
|
+
release() {
|
|
59
|
+
this._size = 0;
|
|
60
|
+
this.map.clear();
|
|
61
|
+
const release = this.opts.release;
|
|
62
|
+
if (release) {
|
|
63
|
+
let e;
|
|
64
|
+
while (e = this.items.drop()) {
|
|
65
|
+
release(e.k, e.v);
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
return this.items.release();
|
|
70
|
+
}
|
|
71
|
+
has(key) {
|
|
72
|
+
return this.map.has(key);
|
|
73
|
+
}
|
|
74
|
+
get(key, notFound) {
|
|
75
|
+
const e = this.map.get(key);
|
|
76
|
+
if (e) {
|
|
77
|
+
return this.resetEntry(e);
|
|
78
|
+
}
|
|
79
|
+
return notFound;
|
|
80
|
+
}
|
|
81
|
+
set(key, value) {
|
|
82
|
+
const size = this.opts.ksize(key) + this.opts.vsize(value);
|
|
83
|
+
const e = this.map.get(key);
|
|
84
|
+
this._size += Math.max(0, size - (e ? e.value.s : 0));
|
|
85
|
+
this.ensureSize() && this.doSetEntry(e, key, value, size);
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
into(pairs) {
|
|
89
|
+
for (let p of pairs) {
|
|
90
|
+
this.set(p[0], p[1]);
|
|
91
|
+
}
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
getSet(key, retrieve) {
|
|
95
|
+
const e = this.map.get(key);
|
|
96
|
+
if (e) {
|
|
97
|
+
return Promise.resolve(this.resetEntry(e));
|
|
98
|
+
}
|
|
99
|
+
return retrieve().then((v) => this.set(key, v));
|
|
100
|
+
}
|
|
101
|
+
delete(key) {
|
|
102
|
+
const e = this.map.get(key);
|
|
103
|
+
if (e) {
|
|
104
|
+
this.removeEntry(e);
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
resetEntry(e) {
|
|
110
|
+
this.items.asTail(e);
|
|
111
|
+
return e.value.v;
|
|
112
|
+
}
|
|
113
|
+
ensureSize() {
|
|
114
|
+
const release = this.opts.release;
|
|
115
|
+
const maxs = this.opts.maxsize;
|
|
116
|
+
const maxl = this.opts.maxlen;
|
|
117
|
+
while (this._size > maxs || this.length >= maxl) {
|
|
118
|
+
const e = this.items.drop();
|
|
119
|
+
if (!e) {
|
|
104
120
|
return false;
|
|
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
|
-
this.opts.release && this.opts.release(ee.k, ee.v);
|
|
130
|
-
this._size -= ee.s;
|
|
131
|
-
}
|
|
132
|
-
doSetEntry(e, k, v, s) {
|
|
133
|
-
if (e) {
|
|
134
|
-
e.value.v = v;
|
|
135
|
-
e.value.s = s;
|
|
136
|
-
this.items.asTail(e);
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
this.items.push({ k, v, s });
|
|
140
|
-
this.map.set(k, this.items.tail);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
121
|
+
}
|
|
122
|
+
this.map.delete(e.k);
|
|
123
|
+
release && release(e.k, e.v);
|
|
124
|
+
this._size -= e.s;
|
|
125
|
+
}
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
removeEntry(e) {
|
|
129
|
+
const ee = e.value;
|
|
130
|
+
this.map.delete(ee.k);
|
|
131
|
+
this.items.remove(e);
|
|
132
|
+
this.opts.release && this.opts.release(ee.k, ee.v);
|
|
133
|
+
this._size -= ee.s;
|
|
134
|
+
}
|
|
135
|
+
doSetEntry(e, k, v, s) {
|
|
136
|
+
if (e) {
|
|
137
|
+
e.value.v = v;
|
|
138
|
+
e.value.s = s;
|
|
139
|
+
this.items.asTail(e);
|
|
140
|
+
} else {
|
|
141
|
+
this.items.push({ k, v, s });
|
|
142
|
+
this.map.set(k, this.items.tail);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
143
145
|
}
|
|
146
|
+
export {
|
|
147
|
+
LRUCache
|
|
148
|
+
};
|
package/mru.js
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
import { LRUCache } from "./lru.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this.map.set(k, this.items.head);
|
|
22
|
-
}
|
|
2
|
+
class MRUCache extends LRUCache {
|
|
3
|
+
constructor(pairs, opts) {
|
|
4
|
+
super(pairs, opts);
|
|
5
|
+
}
|
|
6
|
+
empty() {
|
|
7
|
+
return new MRUCache(null, this.opts);
|
|
8
|
+
}
|
|
9
|
+
resetEntry(e) {
|
|
10
|
+
this.items.asHead(e);
|
|
11
|
+
return e.value.v;
|
|
12
|
+
}
|
|
13
|
+
doSetEntry(e, k, v, s) {
|
|
14
|
+
if (e) {
|
|
15
|
+
e.value.v = v;
|
|
16
|
+
e.value.s = s;
|
|
17
|
+
this.items.asHead(e);
|
|
18
|
+
} else {
|
|
19
|
+
this.items.prepend({ k, v, s });
|
|
20
|
+
this.map.set(k, this.items.head);
|
|
23
21
|
}
|
|
22
|
+
}
|
|
24
23
|
}
|
|
24
|
+
export {
|
|
25
|
+
MRUCache
|
|
26
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/cache",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.83",
|
|
4
4
|
"description": "In-memory cache implementations with ES6 Map-like API and different eviction strategies",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -33,12 +35,13 @@
|
|
|
33
35
|
"test": "bun test"
|
|
34
36
|
},
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@thi.ng/api": "^8.9.
|
|
37
|
-
"@thi.ng/dcons": "^3.2.
|
|
38
|
-
"@thi.ng/transducers": "^8.8.
|
|
38
|
+
"@thi.ng/api": "^8.9.13",
|
|
39
|
+
"@thi.ng/dcons": "^3.2.78",
|
|
40
|
+
"@thi.ng/transducers": "^8.8.16"
|
|
39
41
|
},
|
|
40
42
|
"devDependencies": {
|
|
41
43
|
"@microsoft/api-extractor": "^7.38.3",
|
|
44
|
+
"esbuild": "^0.19.8",
|
|
42
45
|
"rimraf": "^5.0.5",
|
|
43
46
|
"tools": "^0.0.1",
|
|
44
47
|
"typedoc": "^0.25.4",
|
|
@@ -59,7 +62,7 @@
|
|
|
59
62
|
"access": "public"
|
|
60
63
|
},
|
|
61
64
|
"engines": {
|
|
62
|
-
"node": ">=
|
|
65
|
+
"node": ">=18"
|
|
63
66
|
},
|
|
64
67
|
"files": [
|
|
65
68
|
"./*.js",
|
|
@@ -88,5 +91,5 @@
|
|
|
88
91
|
],
|
|
89
92
|
"year": 2018
|
|
90
93
|
},
|
|
91
|
-
"gitHead": "
|
|
94
|
+
"gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
|
|
92
95
|
}
|
package/tlru.js
CHANGED
|
@@ -1,91 +1,79 @@
|
|
|
1
1
|
import { LRUCache } from "./lru.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
2
|
+
class TLRUCache extends LRUCache {
|
|
3
|
+
constructor(pairs, opts) {
|
|
4
|
+
opts = Object.assign({ ttl: 60 * 60 * 1e3 }, opts);
|
|
5
|
+
super(pairs, opts);
|
|
6
|
+
}
|
|
7
|
+
empty() {
|
|
8
|
+
return new TLRUCache(null, this.opts);
|
|
9
|
+
}
|
|
10
|
+
has(key) {
|
|
11
|
+
return this.get(key) !== void 0;
|
|
12
|
+
}
|
|
13
|
+
get(key, notFound) {
|
|
14
|
+
const e = this.map.get(key);
|
|
15
|
+
if (e) {
|
|
16
|
+
if (e.value.t >= Date.now()) {
|
|
17
|
+
return this.resetEntry(e);
|
|
18
|
+
}
|
|
19
|
+
this.removeEntry(e);
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
return notFound;
|
|
22
|
+
}
|
|
23
|
+
set(key, value, ttl = this.opts.ttl) {
|
|
24
|
+
const size = this.opts.ksize(key) + this.opts.vsize(value);
|
|
25
|
+
const e = this.map.get(key);
|
|
26
|
+
this._size += Math.max(0, size - (e ? e.value.s : 0));
|
|
27
|
+
if (this.ensureSize()) {
|
|
28
|
+
const t = Date.now() + ttl;
|
|
29
|
+
if (e) {
|
|
30
|
+
e.value.v = value;
|
|
31
|
+
e.value.s = size;
|
|
32
|
+
e.value.t = t;
|
|
33
|
+
this.items.asTail(e);
|
|
34
|
+
} else {
|
|
35
|
+
this.items.push({
|
|
36
|
+
k: key,
|
|
37
|
+
v: value,
|
|
38
|
+
s: size,
|
|
39
|
+
t
|
|
40
|
+
});
|
|
41
|
+
this.map.set(key, this.items.tail);
|
|
42
|
+
}
|
|
23
43
|
}
|
|
24
|
-
|
|
25
|
-
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
getSet(key, retrieve, ttl = this.opts.ttl) {
|
|
47
|
+
const e = this.get(key);
|
|
48
|
+
if (e) {
|
|
49
|
+
return Promise.resolve(e);
|
|
26
50
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
51
|
+
return retrieve().then((v) => this.set(key, v, ttl));
|
|
52
|
+
}
|
|
53
|
+
prune() {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
let cell = this.items.head;
|
|
56
|
+
while (cell) {
|
|
57
|
+
if (cell.value.t < now) {
|
|
58
|
+
this.removeEntry(cell);
|
|
59
|
+
}
|
|
60
|
+
cell = cell.next;
|
|
36
61
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
this.items.push({
|
|
51
|
-
k: key,
|
|
52
|
-
v: value,
|
|
53
|
-
s: size,
|
|
54
|
-
t,
|
|
55
|
-
});
|
|
56
|
-
this.map.set(key, this.items.tail);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return value;
|
|
60
|
-
}
|
|
61
|
-
getSet(key, retrieve, ttl = this.opts.ttl) {
|
|
62
|
-
const e = this.get(key);
|
|
63
|
-
if (e) {
|
|
64
|
-
return Promise.resolve(e);
|
|
65
|
-
}
|
|
66
|
-
return retrieve().then((v) => this.set(key, v, ttl));
|
|
67
|
-
}
|
|
68
|
-
prune() {
|
|
69
|
-
const now = Date.now();
|
|
70
|
-
let cell = this.items.head;
|
|
71
|
-
while (cell) {
|
|
72
|
-
if (cell.value.t < now) {
|
|
73
|
-
this.removeEntry(cell);
|
|
74
|
-
}
|
|
75
|
-
cell = cell.next;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
ensureSize() {
|
|
79
|
-
const maxs = this.opts.maxsize;
|
|
80
|
-
const maxl = this.opts.maxlen;
|
|
81
|
-
const now = Date.now();
|
|
82
|
-
let cell = this.items.head;
|
|
83
|
-
while (cell && (this._size > maxs || this.length >= maxl)) {
|
|
84
|
-
if (cell.value.t < now) {
|
|
85
|
-
this.removeEntry(cell);
|
|
86
|
-
}
|
|
87
|
-
cell = cell.next;
|
|
88
|
-
}
|
|
89
|
-
return super.ensureSize();
|
|
62
|
+
}
|
|
63
|
+
ensureSize() {
|
|
64
|
+
const maxs = this.opts.maxsize;
|
|
65
|
+
const maxl = this.opts.maxlen;
|
|
66
|
+
const now = Date.now();
|
|
67
|
+
let cell = this.items.head;
|
|
68
|
+
while (cell && (this._size > maxs || this.length >= maxl)) {
|
|
69
|
+
if (cell.value.t < now) {
|
|
70
|
+
this.removeEntry(cell);
|
|
71
|
+
}
|
|
72
|
+
cell = cell.next;
|
|
90
73
|
}
|
|
74
|
+
return super.ensureSize();
|
|
75
|
+
}
|
|
91
76
|
}
|
|
77
|
+
export {
|
|
78
|
+
TLRUCache
|
|
79
|
+
};
|