@thi.ng/cache 2.1.104 → 2.1.106

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-03-07T20:40:47Z
3
+ - **Last updated**: 2024-03-11T08:41:23Z
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,12 @@ 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.1.106](https://github.com/thi-ng/umbrella/tree/@thi.ng/cache@2.1.106) (2024-03-11)
13
+
14
+ #### 🩹 Bug fixes
15
+
16
+ - fix ICache.get() return type, add docs ([d9f98f7](https://github.com/thi-ng/umbrella/commit/d9f98f7))
17
+
12
18
  ### [2.1.89](https://github.com/thi-ng/umbrella/tree/@thi.ng/cache@2.1.89) (2024-01-26)
13
19
 
14
20
  #### 🩹 Bug fixes
package/api.d.ts CHANGED
@@ -1,21 +1,89 @@
1
1
  import type { Fn, Fn0, Fn2, ICopy, IEmpty, ILength, IRelease } from "@thi.ng/api";
2
2
  export interface ICache<K, V> extends Iterable<Readonly<[K, CacheEntry<K, V>]>>, ICopy<ICache<K, V>>, IEmpty<ICache<K, V>>, ILength, IRelease {
3
3
  readonly size: number;
4
+ /**
5
+ * Returns true if the given `key` is currently in the cache.
6
+ *
7
+ * @param key
8
+ */
4
9
  has(key: K): boolean;
5
- get(key: K, notFound?: V): V;
10
+ /**
11
+ * Looks up value for given `key` and if cached returns it. For cache
12
+ * misses, returns the optional `notFound` value or else `undefined`.
13
+ *
14
+ * @param key
15
+ * @param notFound
16
+ */
17
+ get(key: K, notFound?: V): V | undefined;
18
+ /**
19
+ * Set or updates value for given `key` and updates cache internal
20
+ * statistics (depending on cache policy).
21
+ *
22
+ * @param key
23
+ * @param val
24
+ */
6
25
  set(key: K, val: V): V;
26
+ /**
27
+ * Combination of {@link ICache.get} and {@link ICache.set}. Looks up the
28
+ * value for given `key` and returns it. In case of a cache miss, calls
29
+ * given `fn` to provide a value for the `key` and then stores it in the
30
+ * cache before returning that value.
31
+ *
32
+ * @param key
33
+ * @param fn
34
+ */
7
35
  getSet(key: K, fn: Fn0<Promise<V>>): Promise<V>;
36
+ /**
37
+ * Evicts cache entry for given `key` and returns true if the `key` was
38
+ * still cached. If that's the case and if {@link CacheOpts.release} was
39
+ * given when the cache was created, also calls that user provided release
40
+ * handler to perform custom clean up tasks.
41
+ *
42
+ * @param key
43
+ */
8
44
  delete(key: K): boolean;
45
+ /**
46
+ * Returns an iterator of cache entries.
47
+ */
9
48
  entries(): IterableIterator<Readonly<[K, CacheEntry<K, V>]>>;
49
+ /**
50
+ * Returns an iterator of currently cached keys.
51
+ */
10
52
  keys(): IterableIterator<Readonly<K>>;
53
+ /**
54
+ * Returns an iterator of currently cached values.
55
+ */
11
56
  values(): IterableIterator<Readonly<V>>;
12
57
  }
13
58
  export interface CacheOpts<K, V> {
59
+ /**
60
+ * Key size in arbitrary user defined units (must be same unit as given to
61
+ * {@link CacheOpts.maxSize}).
62
+ */
14
63
  ksize: Fn<K, number>;
64
+ /**
65
+ * Value size in arbitrary user defined units (must be same unit as given to
66
+ * {@link CacheOpts.maxSize}).
67
+ */
15
68
  vsize: Fn<V, number>;
69
+ /**
70
+ * Function to perform custom cleanup tasks when an item gets evicted from
71
+ * the cache.
72
+ */
16
73
  release: Fn2<K, V, void>;
74
+ /**
75
+ * Custom ES6 Map compatible implementation to use as the cache's backing
76
+ * store.
77
+ */
17
78
  map: Fn0<Map<K, any>>;
79
+ /**
80
+ * Max number of items in the cache.
81
+ */
18
82
  maxlen: number;
83
+ /**
84
+ * Cache max size in arbitrary user defined units (must be same unit as
85
+ * given to {@link CacheOpts.ksize} and/or {@link CacheOpts.vsize}).
86
+ */
19
87
  maxsize: number;
20
88
  }
21
89
  export interface CacheEntry<K, V> {
package/lru.d.ts CHANGED
@@ -18,7 +18,7 @@ export declare class LRUCache<K, V> implements ICache<K, V> {
18
18
  empty(): LRUCache<K, V>;
19
19
  release(): boolean;
20
20
  has(key: K): boolean;
21
- get(key: K, notFound?: any): any;
21
+ get(key: K, notFound?: V): V | undefined;
22
22
  set(key: K, value: V): V;
23
23
  into(pairs: Iterable<[K, V]>): this;
24
24
  getSet(key: K, retrieve: Fn0<Promise<V>>): Promise<V>;
package/lru.js CHANGED
@@ -96,12 +96,9 @@ class LRUCache {
96
96
  }
97
97
  return this;
98
98
  }
99
- getSet(key, retrieve) {
99
+ async getSet(key, retrieve) {
100
100
  const e = this.map.get(key);
101
- if (e) {
102
- return Promise.resolve(this.resetEntry(e));
103
- }
104
- return retrieve().then((v) => this.set(key, v));
101
+ return e ? this.resetEntry(e) : this.set(key, await retrieve());
105
102
  }
106
103
  delete(key) {
107
104
  const e = this.map.get(key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/cache",
3
- "version": "2.1.104",
3
+ "version": "2.1.106",
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",
@@ -36,9 +36,9 @@
36
36
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
37
37
  },
38
38
  "dependencies": {
39
- "@thi.ng/api": "^8.9.28",
40
- "@thi.ng/dcons": "^3.2.99",
41
- "@thi.ng/transducers": "^8.9.10"
39
+ "@thi.ng/api": "^8.9.29",
40
+ "@thi.ng/dcons": "^3.2.100",
41
+ "@thi.ng/transducers": "^8.9.11"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@microsoft/api-extractor": "^7.40.1",
@@ -91,5 +91,5 @@
91
91
  ],
92
92
  "year": 2018
93
93
  },
94
- "gitHead": "a421058a65ba76608d94129ac29451bfedaf201c\n"
94
+ "gitHead": "aadddd399541d69266672a5815a3f28359a71425\n"
95
95
  }
package/tlru.d.ts CHANGED
@@ -29,7 +29,7 @@ export declare class TLRUCache<K, V> extends LRUCache<K, V> {
29
29
  constructor(pairs?: Iterable<[K, V]> | null, opts?: Partial<TLRUCacheOpts<K, V>>);
30
30
  empty(): TLRUCache<K, V>;
31
31
  has(key: K): boolean;
32
- get(key: K, notFound?: any): any;
32
+ get(key: K, notFound?: V): V | undefined;
33
33
  set(key: K, value: V, ttl?: number): V;
34
34
  getSet(key: K, retrieve: Fn0<Promise<V>>, ttl?: number): Promise<V>;
35
35
  prune(): void;