@boredland/node-ts-cache 1.0.0 → 1.0.1

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.
@@ -1,90 +0,0 @@
1
- import { debug } from "./debug.ts";
2
- import type { Storage } from "./storage.ts";
3
-
4
- export type CachedItem<T = unknown> = {
5
- content: T;
6
- meta: {
7
- createdAt: number;
8
- ttl: number | null;
9
- isLazy: boolean;
10
- };
11
- };
12
-
13
- export type CachingOptions = {
14
- /** Number of milliseconds to expire the cachte item - defaults to forever */
15
- ttl: number | null;
16
- /** (Default: true) If true, expired cache entries will be deleted on touch and returned anyway. If false, entries will be deleted after the given ttl. */
17
- isLazy: boolean;
18
- /** (Default: JSON.stringify combination of className, methodName and call args) */
19
- calculateKey: (data: {
20
- /** The class name for the method being decorated */
21
- className: string;
22
- /** The method name being decorated */
23
- methodName: string;
24
- /** The arguments passed to the method when called */
25
- args: unknown[];
26
- }) => string;
27
- };
28
-
29
- export class CacheContainer {
30
- constructor(private storage: Storage) {}
31
-
32
- public async getItem<T>(
33
- key: string,
34
- ): Promise<
35
- { content: T; meta: { expired: boolean; createdAt: number } } | undefined
36
- > {
37
- const item = await this.storage.getItem(key);
38
-
39
- if (!item) return;
40
-
41
- const result = {
42
- content: item.content as T,
43
- meta: {
44
- createdAt: item.meta.createdAt,
45
- expired: this.isItemExpired(item),
46
- },
47
- };
48
-
49
- if (result.meta.expired) await this.unsetKey(key);
50
-
51
- if (result.meta.expired && !item.meta.isLazy) return undefined;
52
-
53
- return result;
54
- }
55
-
56
- public async setItem(
57
- key: string,
58
- content: unknown,
59
- options?: Partial<CachingOptions>,
60
- ): Promise<void> {
61
- const finalOptions = {
62
- ttl: null,
63
- isLazy: true,
64
- ...options,
65
- };
66
-
67
- const meta: CachedItem<typeof content>["meta"] = {
68
- createdAt: Date.now(),
69
- isLazy: finalOptions.isLazy,
70
- ttl: finalOptions.ttl,
71
- };
72
-
73
- await this.storage.setItem(key, { meta, content });
74
- }
75
-
76
- public async clear(): Promise<void> {
77
- await this.storage.clear();
78
-
79
- debug("Cleared cache");
80
- }
81
-
82
- private isItemExpired(item: CachedItem): boolean {
83
- if (item.meta.ttl === null) return false;
84
- return Date.now() > item.meta.createdAt + item.meta.ttl;
85
- }
86
-
87
- public async unsetKey(key: string): Promise<void> {
88
- await this.storage.removeItem(key);
89
- }
90
- }
package/src/debug.ts DELETED
@@ -1,3 +0,0 @@
1
- import { debug as _debug } from "node:util";
2
-
3
- export const debug = _debug("node-ts-cache");
package/src/hash.ts DELETED
@@ -1,15 +0,0 @@
1
- import { hasher } from "node-object-hash";
2
-
3
- /**
4
- * Hash function for creating consistent, deterministic hashes from JavaScript objects.
5
- * Used primarily for generating cache keys and ensuring object equality checks.
6
- *
7
- * @param obj - Any JavaScript object to hash
8
- * @returns String hash of the object
9
- */
10
- const { hash } = hasher({
11
- sort: true, // Ensures consistent order for object properties
12
- coerce: true, // Converts values to a consistent type (e.g., numbers to strings)
13
- });
14
-
15
- export default hash;
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from "./cacheContainer.ts";
2
- export * from "./lruStorage.ts";
3
- export * from "./storage.ts";
4
- export * from "./withCache.ts";