@sebspark/promise-cache 0.1.0 → 0.2.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/dist/index.d.mts CHANGED
@@ -1,8 +1,26 @@
1
1
  declare class PromiseCache<T, U> {
2
2
  private cache;
3
+ private readonly caseSensitive;
3
4
  private readonly ttl;
4
- constructor(ttlInSeconds: number);
5
- wrap(key: T, delegate: () => Promise<U>): Promise<U>;
5
+ /**
6
+ * Initialize a new PromiseCache.
7
+ * @param ttlInSeconds Default cache TTL.
8
+ * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
9
+ */
10
+ constructor(ttlInSeconds: number, caseSensitive?: boolean);
11
+ /**
12
+ * Cache size.
13
+ * @returns The number of entries in the cache.
14
+ */
15
+ size(): number;
16
+ /**
17
+ * A simple promise cache wrapper.
18
+ * @param key Cache key.
19
+ * @param delegate The function to execute if the key is not in the cache.
20
+ * @param ttlInSeconds Time to live in seconds.
21
+ * @returns The result of the delegate function.
22
+ */
23
+ wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
6
24
  }
7
25
 
8
26
  export { PromiseCache };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,26 @@
1
1
  declare class PromiseCache<T, U> {
2
2
  private cache;
3
+ private readonly caseSensitive;
3
4
  private readonly ttl;
4
- constructor(ttlInSeconds: number);
5
- wrap(key: T, delegate: () => Promise<U>): Promise<U>;
5
+ /**
6
+ * Initialize a new PromiseCache.
7
+ * @param ttlInSeconds Default cache TTL.
8
+ * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
9
+ */
10
+ constructor(ttlInSeconds: number, caseSensitive?: boolean);
11
+ /**
12
+ * Cache size.
13
+ * @returns The number of entries in the cache.
14
+ */
15
+ size(): number;
16
+ /**
17
+ * A simple promise cache wrapper.
18
+ * @param key Cache key.
19
+ * @param delegate The function to execute if the key is not in the cache.
20
+ * @param ttlInSeconds Time to live in seconds.
21
+ * @returns The result of the delegate function.
22
+ */
23
+ wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
6
24
  }
7
25
 
8
26
  export { PromiseCache };
package/dist/index.js CHANGED
@@ -27,20 +27,55 @@ module.exports = __toCommonJS(src_exports);
27
27
  // src/promiseCache.ts
28
28
  var PromiseCache = class {
29
29
  cache;
30
+ caseSensitive;
30
31
  ttl;
31
- // Time to live in milliseconds
32
- constructor(ttlInSeconds) {
32
+ // Time to live in milliseconds.
33
+ /**
34
+ * Initialize a new PromiseCache.
35
+ * @param ttlInSeconds Default cache TTL.
36
+ * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
37
+ */
38
+ constructor(ttlInSeconds, caseSensitive = false) {
33
39
  this.cache = /* @__PURE__ */ new Map();
40
+ this.caseSensitive = caseSensitive;
34
41
  this.ttl = ttlInSeconds * 1e3;
35
42
  }
36
- async wrap(key, delegate) {
43
+ /**
44
+ * Cache size.
45
+ * @returns The number of entries in the cache.
46
+ */
47
+ size() {
48
+ return this.cache.size;
49
+ }
50
+ /**
51
+ * A simple promise cache wrapper.
52
+ * @param key Cache key.
53
+ * @param delegate The function to execute if the key is not in the cache.
54
+ * @param ttlInSeconds Time to live in seconds.
55
+ * @returns The result of the delegate function.
56
+ */
57
+ async wrap(key, delegate, ttlInSeconds) {
37
58
  const now = Date.now();
38
- const cached = this.cache.get(key);
39
- if (cached && now - cached.timestamp < this.ttl) {
59
+ const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
60
+ const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
61
+ const cached = this.cache.get(effectiveKey);
62
+ if (cached) {
63
+ if (cached.ttl !== effectiveTTL) {
64
+ console.error(
65
+ `WARNING: TTL mismatch for key: ${effectiveKey}. It is recommended to use the same TTL for the same key.`
66
+ );
67
+ }
40
68
  return cached.value;
41
69
  }
42
70
  const response = await delegate();
43
- this.cache.set(key, { value: response, timestamp: now });
71
+ this.cache.set(effectiveKey, {
72
+ value: response,
73
+ timestamp: now,
74
+ ttl: effectiveTTL
75
+ });
76
+ setTimeout(() => {
77
+ this.cache.delete(effectiveKey);
78
+ }, effectiveTTL);
44
79
  return response;
45
80
  }
46
81
  };
package/dist/index.mjs CHANGED
@@ -1,20 +1,55 @@
1
1
  // src/promiseCache.ts
2
2
  var PromiseCache = class {
3
3
  cache;
4
+ caseSensitive;
4
5
  ttl;
5
- // Time to live in milliseconds
6
- constructor(ttlInSeconds) {
6
+ // Time to live in milliseconds.
7
+ /**
8
+ * Initialize a new PromiseCache.
9
+ * @param ttlInSeconds Default cache TTL.
10
+ * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
11
+ */
12
+ constructor(ttlInSeconds, caseSensitive = false) {
7
13
  this.cache = /* @__PURE__ */ new Map();
14
+ this.caseSensitive = caseSensitive;
8
15
  this.ttl = ttlInSeconds * 1e3;
9
16
  }
10
- async wrap(key, delegate) {
17
+ /**
18
+ * Cache size.
19
+ * @returns The number of entries in the cache.
20
+ */
21
+ size() {
22
+ return this.cache.size;
23
+ }
24
+ /**
25
+ * A simple promise cache wrapper.
26
+ * @param key Cache key.
27
+ * @param delegate The function to execute if the key is not in the cache.
28
+ * @param ttlInSeconds Time to live in seconds.
29
+ * @returns The result of the delegate function.
30
+ */
31
+ async wrap(key, delegate, ttlInSeconds) {
11
32
  const now = Date.now();
12
- const cached = this.cache.get(key);
13
- if (cached && now - cached.timestamp < this.ttl) {
33
+ const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
34
+ const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
35
+ const cached = this.cache.get(effectiveKey);
36
+ if (cached) {
37
+ if (cached.ttl !== effectiveTTL) {
38
+ console.error(
39
+ `WARNING: TTL mismatch for key: ${effectiveKey}. It is recommended to use the same TTL for the same key.`
40
+ );
41
+ }
14
42
  return cached.value;
15
43
  }
16
44
  const response = await delegate();
17
- this.cache.set(key, { value: response, timestamp: now });
45
+ this.cache.set(effectiveKey, {
46
+ value: response,
47
+ timestamp: now,
48
+ ttl: effectiveTTL
49
+ });
50
+ setTimeout(() => {
51
+ this.cache.delete(effectiveKey);
52
+ }, effectiveTTL);
18
53
  return response;
19
54
  }
20
55
  };
package/package.json CHANGED
@@ -1,13 +1,11 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
- "files": [
9
- "dist"
10
- ],
8
+ "files": ["dist"],
11
9
  "scripts": {
12
10
  "build": "tsup-node src/index.ts --format esm,cjs --dts",
13
11
  "dev": "tsc --watch --noEmit",
@@ -18,4 +16,4 @@
18
16
  "devDependencies": {
19
17
  "tsconfig": "*"
20
18
  }
21
- }
19
+ }