@thi.ng/cache 2.2.21 → 2.3.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-07-22T13:15:57Z
3
+ - **Last updated**: 2024-07-28T10:53:10Z
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.
@@ -9,6 +9,16 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/cache@2.3.0) (2024-07-28)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add `TLRUCacheOpts.autoExtend` option ([9fbda4c](https://github.com/thi-ng/umbrella/commit/9fbda4c))
17
+ - add support to auto-extend TTL of cached values upon cache hit
18
+ - add tests
19
+ - add/update docs
20
+ - update readme
21
+
12
22
  ### [2.2.11](https://github.com/thi-ng/umbrella/tree/@thi.ng/cache@2.2.11) (2024-04-20)
13
23
 
14
24
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -86,7 +86,7 @@ For Node.js REPL:
86
86
  const cache = await import("@thi.ng/cache");
87
87
  ```
88
88
 
89
- Package sizes (brotli'd, pre-treeshake): ESM: 1.05 KB
89
+ Package sizes (brotli'd, pre-treeshake): ESM: 1.09 KB
90
90
 
91
91
  ## Dependencies
92
92
 
@@ -221,18 +221,24 @@ lru.size
221
221
  ### TLRU
222
222
 
223
223
  Time-aware [LRU cache](#lru). Extends LRU strategy with TTL (time-to-live)
224
- values associated with each entry. `has()` will only return `true` and `get()`
225
- only returns a cached value if its TTL hasn't yet expired. When adding a new
226
- value to the cache, first removes expired entries and if there's still not
227
- sufficient space removes entries in LRU order. `set()` takes an optional entry
228
- specific `ttl` arg. If not given, uses the cache instance's default (provided
229
- via ctor option arg). If no instance TTL is given, TTL defaults to 1 hour.
224
+ values associated with each entry, which has an impact on:
225
+
226
+ - `has()` only returns `true` if a cached value's TTL hasn't yet expired
227
+ - `get()` only returns a cached value if its TTL hasn't yet expired. Using the
228
+ `autoExtend` option given via the cache constructor options, the cache can be
229
+ configured such that a successful cache hit will update/extend the expiry time
230
+ of that respective entry.
231
+ - `set()` takes an optional entry specific `ttl` arg. If not given, uses the
232
+ cache's default (provided via ctor option arg). Default TTL is 1 hour.
233
+
234
+ When adding a new value to the cache, first removes expired entries and if
235
+ there's still not sufficient space removes entries in LRU order.
230
236
 
231
237
  ```ts
232
238
  import { TLRUCache } from "@thi.ng/cache";
233
239
 
234
- // same opts as LRUCache, but here with custom default TTL period (in ms)
235
- tlru = new TLRUCache(null, { ttl: 10000 });
240
+ // same opts as LRUCache, but here with additional custom TTL period (in ms)
241
+ tlru = new TLRUCache(null, { ttl: 10000, autoExtend: true });
236
242
 
237
243
  // with item specific TTL (500ms)
238
244
  tlru.set("foo", 42, 500)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/cache",
3
- "version": "2.2.21",
3
+ "version": "2.3.0",
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",
@@ -89,5 +89,5 @@
89
89
  ],
90
90
  "year": 2018
91
91
  },
92
- "gitHead": "bd22b0826134b79064169371665b4d6caa9b6066\n"
92
+ "gitHead": "c8461a678e3c0d496dfde24b8c494845d7514cc0\n"
93
93
  }
package/tlru.d.ts CHANGED
@@ -10,9 +10,26 @@ export interface TLRUCacheOpts<K, V> extends CacheOpts<K, V> {
10
10
  * @defaultValue 3600000 (1 hour)
11
11
  */
12
12
  ttl: number;
13
+ /**
14
+ * If true, a cache hit (i.e. reading a cached value via
15
+ * {@link TLRUCache.get}) will auto-extend the expiry time for that cache
16
+ * entry.
17
+ *
18
+ * @defaultValue false
19
+ */
20
+ autoExtend: boolean;
13
21
  }
14
22
  export interface TLRUCacheEntry<K, V> extends CacheEntry<K, V> {
23
+ /**
24
+ * Expiry timestamp
25
+ */
15
26
  t: number;
27
+ /**
28
+ * TTL for this entry (defaults to {@link TLRUCacheOpts.ttl}, but can be
29
+ * customized per key via {@link TLRUCache.set} or
30
+ * {@link TLRUCache.getSet}).
31
+ */
32
+ ttl: number;
16
33
  }
17
34
  /**
18
35
  * Time-aware LRU cache. Extends LRU strategy with TTL (time-to-live)
@@ -35,7 +52,28 @@ export declare class TLRUCache<K, V> extends LRUCache<K, V> {
35
52
  constructor(pairs?: Nullable<Iterable<[K, V]>>, opts?: Partial<TLRUCacheOpts<K, V>>);
36
53
  empty(): TLRUCache<K, V>;
37
54
  has(key: K): boolean;
55
+ /**
56
+ * Attempts to retrieve & return cached value for `key`. If found, also
57
+ * resets the cache entry's configured TTL. If cache miss, returns
58
+ * `notFound`.
59
+ *
60
+ * @param key
61
+ * @param notFound
62
+ */
38
63
  get(key: K, notFound?: V): V | undefined;
64
+ /**
65
+ * Stores given `value` under `key` in the cache, optionally with custom
66
+ * `ttl` (in milliseconds). Returns `value`.
67
+ *
68
+ * @remarks
69
+ * Also see {@link TLRUCache.getSet} for alternative, and
70
+ * {@link CacheOpts.update} for user callback when updating an existing
71
+ * `key` in the cache.
72
+ *
73
+ * @param key
74
+ * @param value
75
+ * @param ttl
76
+ */
39
77
  set(key: K, value: V, ttl?: number): V;
40
78
  getSet(key: K, retrieve: Fn0<Promise<V>>, ttl?: number): Promise<V>;
41
79
  /**
@@ -50,5 +88,6 @@ export declare class TLRUCache<K, V> extends LRUCache<K, V> {
50
88
  prune(): number;
51
89
  protected ensureSize(): boolean;
52
90
  protected doSetEntry(e: Maybe<ConsCell<TLRUCacheEntry<K, V>>>, k: K, v: V, s: number, ttl?: number): void;
91
+ protected resetEntry(e: ConsCell<TLRUCacheEntry<K, V>>): V;
53
92
  }
54
93
  //# sourceMappingURL=tlru.d.ts.map
package/tlru.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { LRUCache } from "./lru.js";
2
2
  class TLRUCache extends LRUCache {
3
3
  constructor(pairs, opts) {
4
- super(pairs, { ttl: 60 * 60 * 1e3, ...opts });
4
+ super(pairs, { ttl: 60 * 60 * 1e3, autoExtend: false, ...opts });
5
5
  }
6
6
  empty() {
7
7
  return new TLRUCache(null, this.opts);
@@ -9,6 +9,14 @@ class TLRUCache extends LRUCache {
9
9
  has(key) {
10
10
  return this.get(key) !== void 0;
11
11
  }
12
+ /**
13
+ * Attempts to retrieve & return cached value for `key`. If found, also
14
+ * resets the cache entry's configured TTL. If cache miss, returns
15
+ * `notFound`.
16
+ *
17
+ * @param key
18
+ * @param notFound
19
+ */
12
20
  get(key, notFound) {
13
21
  const e = this.map.get(key);
14
22
  if (e) {
@@ -19,6 +27,19 @@ class TLRUCache extends LRUCache {
19
27
  }
20
28
  return notFound;
21
29
  }
30
+ /**
31
+ * Stores given `value` under `key` in the cache, optionally with custom
32
+ * `ttl` (in milliseconds). Returns `value`.
33
+ *
34
+ * @remarks
35
+ * Also see {@link TLRUCache.getSet} for alternative, and
36
+ * {@link CacheOpts.update} for user callback when updating an existing
37
+ * `key` in the cache.
38
+ *
39
+ * @param key
40
+ * @param value
41
+ * @param ttl
42
+ */
22
43
  set(key, value, ttl = this.opts.ttl) {
23
44
  const size = this.opts.ksize(key) + this.opts.vsize(value);
24
45
  const e = this.map.get(key);
@@ -78,10 +99,16 @@ class TLRUCache extends LRUCache {
78
99
  e.value.t = t;
79
100
  this.items.asTail(e);
80
101
  } else {
81
- this.items.push({ k, v, s, t });
102
+ this.items.push({ k, v, s, t, ttl });
82
103
  this.map.set(k, this.items.tail);
83
104
  }
84
105
  }
106
+ resetEntry(e) {
107
+ if (this.opts.autoExtend) {
108
+ e.value.t = Date.now() + e.value.ttl;
109
+ }
110
+ return super.resetEntry(e);
111
+ }
85
112
  }
86
113
  export {
87
114
  TLRUCache