@thi.ng/cache 2.1.80 → 2.1.82

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.
Files changed (6) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/api.js +0 -1
  3. package/lru.js +144 -139
  4. package/mru.js +23 -21
  5. package/package.json +9 -7
  6. package/tlru.js +72 -84
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-03T12:13:31Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
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
- export class LRUCache {
4
- map;
5
- items;
6
- opts;
7
- _size;
8
- constructor(pairs, opts) {
9
- const _opts = Object.assign({
10
- maxlen: Infinity,
11
- maxsize: Infinity,
12
- map: () => new Map(),
13
- ksize: () => 0,
14
- vsize: () => 0,
15
- }, opts);
16
- this.map = _opts.map();
17
- this.items = new DCons();
18
- this._size = 0;
19
- this.opts = _opts;
20
- if (pairs) {
21
- this.into(pairs);
22
- }
23
- }
24
- get length() {
25
- return this.items.length;
26
- }
27
- get size() {
28
- return this._size;
29
- }
30
- [Symbol.iterator]() {
31
- return this.entries();
32
- }
33
- entries() {
34
- return map((e) => [e.k, e], this.items);
35
- }
36
- keys() {
37
- return map((e) => e.k, this.items);
38
- }
39
- values() {
40
- return map((e) => e.v, this.items);
41
- }
42
- copy() {
43
- const c = this.empty();
44
- c.items = this.items.copy();
45
- let cell = c.items.head;
46
- while (cell) {
47
- c.map.set(cell.value.k, cell);
48
- cell = cell.next;
49
- }
50
- return c;
51
- }
52
- empty() {
53
- return new LRUCache(null, this.opts);
54
- }
55
- release() {
56
- this._size = 0;
57
- this.map.clear();
58
- const release = this.opts.release;
59
- if (release) {
60
- let e;
61
- while ((e = this.items.drop())) {
62
- release(e.k, e.v);
63
- }
64
- return true;
65
- }
66
- return this.items.release();
67
- }
68
- has(key) {
69
- return this.map.has(key);
70
- }
71
- get(key, notFound) {
72
- const e = this.map.get(key);
73
- if (e) {
74
- return this.resetEntry(e);
75
- }
76
- return notFound;
77
- }
78
- set(key, value) {
79
- const size = this.opts.ksize(key) + this.opts.vsize(value);
80
- const e = this.map.get(key);
81
- this._size += Math.max(0, size - (e ? e.value.s : 0));
82
- this.ensureSize() && this.doSetEntry(e, key, value, size);
83
- return value;
84
- }
85
- into(pairs) {
86
- for (let p of pairs) {
87
- this.set(p[0], p[1]);
88
- }
89
- return this;
90
- }
91
- getSet(key, retrieve) {
92
- const e = this.map.get(key);
93
- if (e) {
94
- return Promise.resolve(this.resetEntry(e));
95
- }
96
- return retrieve().then((v) => this.set(key, v));
97
- }
98
- delete(key) {
99
- const e = this.map.get(key);
100
- if (e) {
101
- this.removeEntry(e);
102
- return true;
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
- resetEntry(e) {
107
- this.items.asTail(e);
108
- return e.value.v;
109
- }
110
- ensureSize() {
111
- const release = this.opts.release;
112
- const maxs = this.opts.maxsize;
113
- const maxl = this.opts.maxlen;
114
- while (this._size > maxs || this.length >= maxl) {
115
- const e = this.items.drop();
116
- if (!e) {
117
- return false;
118
- }
119
- this.map.delete(e.k);
120
- release && release(e.k, e.v);
121
- this._size -= e.s;
122
- }
123
- return true;
124
- }
125
- removeEntry(e) {
126
- const ee = e.value;
127
- this.map.delete(ee.k);
128
- this.items.remove(e);
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
- export 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
- }
19
- else {
20
- this.items.prepend({ k, v, s });
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.80",
3
+ "version": "2.1.82",
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 clean && tsc --declaration",
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,13 +35,13 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/api": "^8.9.10",
37
- "@thi.ng/dcons": "^3.2.75",
38
- "@thi.ng/transducers": "^8.8.13"
38
+ "@thi.ng/api": "^8.9.12",
39
+ "@thi.ng/dcons": "^3.2.77",
40
+ "@thi.ng/transducers": "^8.8.15"
39
41
  },
40
42
  "devDependencies": {
41
43
  "@microsoft/api-extractor": "^7.38.3",
42
- "@thi.ng/testament": "^0.4.3",
44
+ "esbuild": "^0.19.8",
43
45
  "rimraf": "^5.0.5",
44
46
  "tools": "^0.0.1",
45
47
  "typedoc": "^0.25.4",
@@ -89,5 +91,5 @@
89
91
  ],
90
92
  "year": 2018
91
93
  },
92
- "gitHead": "04d1de79f256d7a53c6b5fd157b37f49bc88e11d\n"
94
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
93
95
  }
package/tlru.js CHANGED
@@ -1,91 +1,79 @@
1
1
  import { LRUCache } from "./lru.js";
2
- /**
3
- * Time-aware LRU cache. Extends LRU strategy with TTL (time-to-live)
4
- * values associated to each entry.
5
- *
6
- * @remarks
7
- * {@link ICache.has} will only return true and {@link ICache.get} only
8
- * returns a cached value if its TTL hasn't yet expired. When adding a
9
- * new value to the cache, first removes expired entries and if still
10
- * not sufficient space then removes entries in LRU order.
11
- *
12
- * {@link ICache.set} takes an optional entry specific `ttl` arg. If not
13
- * given, uses the cache instance's default (provided via ctor option
14
- * arg). If no instance TTL is given, TTL defaults to 1 hour.
15
- */
16
- export class TLRUCache extends LRUCache {
17
- constructor(pairs, opts) {
18
- opts = Object.assign({ ttl: 60 * 60 * 1000 }, opts);
19
- super(pairs, opts);
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
- empty() {
22
- return new TLRUCache(null, this.opts);
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
- has(key) {
25
- return this.get(key) !== undefined;
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
- get(key, notFound) {
28
- const e = this.map.get(key);
29
- if (e) {
30
- if (e.value.t >= Date.now()) {
31
- return this.resetEntry(e);
32
- }
33
- this.removeEntry(e);
34
- }
35
- return notFound;
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
- set(key, value, ttl = this.opts.ttl) {
38
- const size = this.opts.ksize(key) + this.opts.vsize(value);
39
- const e = this.map.get(key);
40
- this._size += Math.max(0, size - (e ? e.value.s : 0));
41
- if (this.ensureSize()) {
42
- const t = Date.now() + ttl;
43
- if (e) {
44
- e.value.v = value;
45
- e.value.s = size;
46
- e.value.t = t;
47
- this.items.asTail(e);
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
+ };