File size: 1,480 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
   */
  static create(owner: string, leaderOnly: boolean): ServerConfigsCache {
    if (cacheConfig.USE_REDIS) {
      return new ServerConfigsCacheRedis(owner, leaderOnly);
    }

    // In-memory mode uses a simple Map - doesn't need owner/namespace
    return new ServerConfigsCacheInMemory();
  }
}