LibreChat/packages/api/src/mcp/registry/cache/ServerConfigsCacheFactory.ts

36 lines
1.5 KiB
TypeScript
Raw Normal View History

import { cacheConfig } from '~/cache';
import { ServerConfigsCacheInMemory } from './ServerConfigsCacheInMemory';
import { ServerConfigsCacheRedis } from './ServerConfigsCacheRedis';
export type ServerConfigsCache = ServerConfigsCacheInMemory | ServerConfigsCacheRedis;
/**
* Factory for creating the appropriate ServerConfigsCache implementation based on deployment mode.
* Automatically selects between in-memory and Redis-backed storage depending on USE_REDIS config.
* In single-instance mode (USE_REDIS=false), returns lightweight in-memory cache.
* In cluster mode (USE_REDIS=true), returns Redis-backed cache with distributed coordination.
* Provides a unified interface regardless of the underlying storage mechanism.
*/
export class ServerConfigsCacheFactory {
/**
* Create a ServerConfigsCache instance.
* Returns Redis implementation if Redis is configured, otherwise in-memory implementation.
*
* @param owner - The owner of the cache (e.g., 'user', 'global') - only used for Redis namespacing
* @param leaderOnly - Whether operations should only be performed by the leader (only applies to Redis)
* @returns ServerConfigsCache instance
*/
2025-11-26 15:11:36 +01:00
static create(
owner: string,
scope: 'Shared' | 'Private',
leaderOnly: boolean,
): ServerConfigsCache {
if (cacheConfig.USE_REDIS) {
2025-11-26 15:11:36 +01:00
return new ServerConfigsCacheRedis(owner, scope, leaderOnly);
}
// In-memory mode uses a simple Map - doesn't need owner/namespace
return new ServerConfigsCacheInMemory();
}
}