2025-10-31 13:00:21 -06:00
|
|
|
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.
|
|
|
|
|
*
|
2025-11-28 16:07:09 +01:00
|
|
|
* @param namespace - The namespace for the cache (e.g., 'App') - only used for Redis namespacing
|
2025-10-31 13:00:21 -06:00
|
|
|
* @param leaderOnly - Whether operations should only be performed by the leader (only applies to Redis)
|
|
|
|
|
* @returns ServerConfigsCache instance
|
|
|
|
|
*/
|
2025-11-28 16:07:09 +01:00
|
|
|
static create(namespace: string, leaderOnly: boolean): ServerConfigsCache {
|
2025-10-31 13:00:21 -06:00
|
|
|
if (cacheConfig.USE_REDIS) {
|
2025-11-28 16:07:09 +01:00
|
|
|
return new ServerConfigsCacheRedis(namespace, leaderOnly);
|
2025-10-31 13:00:21 -06:00
|
|
|
}
|
|
|
|
|
|
2025-11-28 16:07:09 +01:00
|
|
|
// In-memory mode uses a simple Map - doesn't need namespace
|
2025-10-31 13:00:21 -06:00
|
|
|
return new ServerConfigsCacheInMemory();
|
|
|
|
|
}
|
|
|
|
|
}
|