1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

fix(client): cache subsequent clients (#2963)

* fix(client): cache subsequent clients

we dont need to recreate a client if
its config hasnt changed

fixes #2954

* handle circular structures

* make cache generic
This commit is contained in:
Nikolay Karadzhov
2025-05-14 17:23:22 +03:00
committed by GitHub
parent ebd03036d6
commit 6f961bd715
5 changed files with 177 additions and 30 deletions

View File

@@ -12,6 +12,7 @@ import { RedisTcpSocketOptions } from '../client/socket';
import ASKING from '../commands/ASKING';
import { BasicCommandParser } from '../client/parser';
import { parseArgs } from '../commands/generic-transformers';
import SingleEntryCache from '../single-entry-cache';
interface ClusterCommander<
M extends RedisModules,
@@ -213,6 +214,8 @@ export default class RedisCluster<
};
}
static #SingleEntryCache = new SingleEntryCache<any, any>();
static factory<
M extends RedisModules = {},
F extends RedisFunctions = {},
@@ -221,17 +224,22 @@ export default class RedisCluster<
TYPE_MAPPING extends TypeMapping = {},
// POLICIES extends CommandPolicies = {}
>(config?: ClusterCommander<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>) {
const Cluster = attachConfig({
BaseClass: RedisCluster,
commands: COMMANDS,
createCommand: RedisCluster.#createCommand,
createModuleCommand: RedisCluster.#createModuleCommand,
createFunctionCommand: RedisCluster.#createFunctionCommand,
createScriptCommand: RedisCluster.#createScriptCommand,
config
});
Cluster.prototype.Multi = RedisClusterMultiCommand.extend(config);
let Cluster = RedisCluster.#SingleEntryCache.get(config);
if (!Cluster) {
Cluster = attachConfig({
BaseClass: RedisCluster,
commands: COMMANDS,
createCommand: RedisCluster.#createCommand,
createModuleCommand: RedisCluster.#createModuleCommand,
createFunctionCommand: RedisCluster.#createFunctionCommand,
createScriptCommand: RedisCluster.#createScriptCommand,
config
});
Cluster.prototype.Multi = RedisClusterMultiCommand.extend(config);
RedisCluster.#SingleEntryCache.set(config, Cluster);
}
return (options?: Omit<RedisClusterOptions, keyof Exclude<typeof config, undefined>>) => {
// returning a "proxy" to prevent the namespaces._self to leak between "proxies"