accept client config for clustered redis

Signed-off-by: Mark Nachazel <markn@doordash.com>
This commit is contained in:
Mark Nachazel
2025-03-12 16:47:52 -04:00
parent 01edf6e916
commit fc1c605b9e
2 changed files with 57 additions and 4 deletions
@@ -264,4 +264,57 @@ describe('CacheManager store options', () => {
defaults: undefined,
});
});
it('respects client config for non-clustered mode', () => {
const manager = CacheManager.fromConfig(
mockServices.rootConfig({
data: {
backend: {
cache: {
store: 'redis',
connection: 'redis://localhost:6379',
redis: {
client: {
keyPrefixSeparator: '!',
},
},
},
},
},
}),
);
manager.forPlugin('p1');
expect(KeyvRedis).toHaveBeenCalledWith('redis://localhost:6379', {
keyPrefixSeparator: '!',
});
});
it('accepts client config for clustered mode', () => {
const manager = CacheManager.fromConfig(
mockServices.rootConfig({
data: {
backend: {
cache: {
store: 'redis',
connection: 'redis://localhost:6379',
redis: {
client: {
keyPrefixSeparator: '!',
},
cluster: {
rootNodes: [{ url: 'redis://localhost:6379' }],
},
},
},
},
},
}),
);
manager.forPlugin('p1');
expect(KeyvRedis).toHaveBeenCalledWith(expect.anything(), {
keyPrefixSeparator: '!',
});
});
});
@@ -211,15 +211,15 @@ export class CacheManager {
return (pluginId, defaultTtl) => {
if (!stores[pluginId]) {
const redisOptions = this.storeOptions?.client || {
keyPrefixSeparator: ':',
};
if (this.storeOptions?.cluster) {
// Create a Redis cluster
const cluster = createCluster(this.storeOptions?.cluster);
stores[pluginId] = new KeyvRedis(cluster);
stores[pluginId] = new KeyvRedis(cluster, redisOptions);
} else {
// Create a regular Redis connection
const redisOptions = this.storeOptions?.client || {
keyPrefixSeparator: ':',
};
stores[pluginId] = new KeyvRedis(this.connection, redisOptions);
}