|
1 |
| -export class SimpleCache { |
2 |
| - // The key under which the cache is stored in sessionStorage |
3 |
| - private storageKey = 'simpleCache'; |
4 |
| - |
5 |
| - // In-memory cache |
6 |
| - private cache: { [key: string]: any } = {}; |
7 |
| - |
8 |
| - constructor() { |
9 |
| - this.loadCache(); |
10 |
| - } |
11 |
| - |
12 |
| - /** |
13 |
| - * Retrieves the cached value for a given key-object pair. |
14 |
| - * @param key The string key. |
15 |
| - * @param obj The object to namespace the key. |
16 |
| - * @returns The cached value if found; otherwise, null. |
17 |
| - */ |
18 |
| - public get(key: string, obj: object): any | null { |
19 |
| - const compositeKey = this.createCompositeKey(key, obj); |
20 |
| - if (compositeKey in this.cache) { |
21 |
| - return this.cache[compositeKey]; |
22 |
| - } |
23 |
| - return null; |
24 |
| - } |
25 |
| - |
26 |
| - /** |
27 |
| - * Stores a value in the cache for a given key-object pair. |
28 |
| - * @param key The string key. |
29 |
| - * @param obj The object to namespace the key. |
30 |
| - * @param value The value to cache. |
31 |
| - */ |
32 |
| - public set(key: string, obj: object, value: any): void { |
33 |
| - const compositeKey = this.createCompositeKey(key, obj); |
34 |
| - this.cache[compositeKey] = value; |
35 |
| - this.saveCache(); |
36 |
| - } |
37 |
| - |
38 |
| - /** |
39 |
| - * Clears the entire cache both in-memory and in sessionStorage. |
40 |
| - */ |
41 |
| - public clear(): void { |
42 |
| - this.cache = {}; |
43 |
| - sessionStorage.removeItem(this.storageKey); |
44 |
| - } |
45 |
| - |
46 |
| - /** |
47 |
| - * Creates a composite key by combining the key and a stable string representation of the object. |
48 |
| - * @param key The string key. |
49 |
| - * @param obj The object to namespace the key. |
50 |
| - * @returns The composite key as a string. |
51 |
| - */ |
52 |
| - private createCompositeKey(key: string, obj: object): string { |
53 |
| - const serializedObj = this.stableStringify(obj); |
54 |
| - return `${key}|${serializedObj}`; |
55 |
| - } |
56 |
| - |
57 |
| - /** |
58 |
| - * Loads the cache from sessionStorage into the in-memory cache. |
59 |
| - */ |
60 |
| - private loadCache(): void { |
61 |
| - const storedCache = sessionStorage.getItem(this.storageKey); |
62 |
| - if (storedCache) { |
63 |
| - try { |
64 |
| - this.cache = JSON.parse(storedCache); |
65 |
| - } catch (error) { |
66 |
| - console.error('Failed to parse cache from sessionStorage:', error); |
67 |
| - this.cache = {}; |
68 |
| - } |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - /** |
73 |
| - * Saves the in-memory cache to sessionStorage. |
74 |
| - */ |
75 |
| - private saveCache(): void { |
76 |
| - try { |
77 |
| - sessionStorage.setItem(this.storageKey, JSON.stringify(this.cache)); |
78 |
| - } catch (error) { |
79 |
| - console.error('Failed to save cache to sessionStorage:', error); |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - /** |
84 |
| - * Serializes an object into a JSON string with sorted keys to ensure consistency. |
85 |
| - * @param obj The object to serialize. |
86 |
| - * @returns A JSON string with sorted keys. |
87 |
| - */ |
88 |
| - private stableStringify(obj: any): string { |
89 |
| - if (obj !== null && typeof obj === 'object') { |
90 |
| - if (Array.isArray(obj)) { |
91 |
| - return `[${obj.map((item) => this.stableStringify(item)).join(',')}]`; |
92 |
| - } else { |
93 |
| - const keys = Object.keys(obj).sort(); |
94 |
| - return `{${keys.map((key) => `"${key}":${this.stableStringify(obj[key])}`).join(',')}}`; |
95 |
| - } |
96 |
| - } else if (typeof obj === 'string') { |
97 |
| - return `"${obj}"`; |
98 |
| - } else { |
99 |
| - return String(obj); |
100 |
| - } |
101 |
| - } |
102 |
| -} |
0 commit comments