Skip to content

Commit 7143297

Browse files
committed
make cache generic
1 parent 4e35ff1 commit 7143297

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

packages/client/lib/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export default class RedisClient<
207207
}
208208
}
209209

210-
static #SingleEntryCache = new SingleEntryCache()
210+
static #SingleEntryCache = new SingleEntryCache<any, any>()
211211

212212
static factory<
213213
M extends RedisModules = {},

packages/client/lib/client/pool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class RedisClientPool<
111111
};
112112
}
113113

114-
static #SingleEntryCache = new SingleEntryCache();
114+
static #SingleEntryCache = new SingleEntryCache<any, any>();
115115

116116
static create<
117117
M extends RedisModules,

packages/client/lib/cluster/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export default class RedisCluster<
214214
};
215215
}
216216

217-
static #SingleEntryCache = new SingleEntryCache();
217+
static #SingleEntryCache = new SingleEntryCache<any, any>();
218218

219219
static factory<
220220
M extends RedisModules = {},
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1-
function makeCircularReplacer() {
2-
const seen = new WeakSet();
3-
return function serialize(_: string, value: any) {
4-
if (value && typeof value === 'object') {
5-
if (seen.has(value)) {
6-
return 'circular';
7-
}
8-
seen.add(value);
9-
return value;
10-
}
11-
return value;
12-
}
13-
}
14-
export default class SingleEntryCache {
15-
#cached?: any;
16-
#key?: string;
1+
export default class SingleEntryCache<K, V> {
2+
#cached?: V;
3+
#serializedKey?: string;
174

185
/**
196
* Retrieves an instance from the cache based on the provided key object.
@@ -25,12 +12,26 @@ export default class SingleEntryCache {
2512
* This method uses JSON.stringify for comparison, which may not work correctly
2613
* if the properties in the key object are rearranged or reordered.
2714
*/
28-
get(keyObj?: object) {
29-
return JSON.stringify(keyObj, makeCircularReplacer()) === this.#key ? this.#cached : undefined;
15+
get(keyObj?: K): V | undefined {
16+
return JSON.stringify(keyObj, makeCircularReplacer()) === this.#serializedKey ? this.#cached : undefined;
3017
}
3118

32-
set(keyObj: object | undefined, obj: any) {
19+
set(keyObj: K | undefined, obj: V) {
3320
this.#cached = obj;
34-
this.#key = JSON.stringify(keyObj, makeCircularReplacer());
21+
this.#serializedKey = JSON.stringify(keyObj, makeCircularReplacer());
3522
}
3623
}
24+
25+
function makeCircularReplacer() {
26+
const seen = new WeakSet();
27+
return function serialize(_: string, value: any) {
28+
if (value && typeof value === 'object') {
29+
if (seen.has(value)) {
30+
return 'circular';
31+
}
32+
seen.add(value);
33+
return value;
34+
}
35+
return value;
36+
}
37+
}

0 commit comments

Comments
 (0)