From e6e61f5e2836a65bc583f51942fc835df7ea3d08 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 11 Feb 2026 22:05:26 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20refactor:=20Update=20cache=20con?= =?UTF-8?q?figuration=20for=20in-memory=20storage=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Enhanced the handling of `FORCED_IN_MEMORY_CACHE_NAMESPACES` in `cacheConfig.ts` to default to `CONFIG_STORE` and `APP_CONFIG`, ensuring safer blue/green deployments. - Updated `.env.example` with clearer comments regarding the usage of in-memory cache namespaces. - Improved unit tests to validate the new default behavior and handling of empty strings for cache namespaces. --- .env.example | 6 +++--- packages/api/src/cache/__tests__/cacheConfig.spec.ts | 6 +++--- packages/api/src/cache/cacheConfig.ts | 11 ++++++++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index 8ca9ce4285..4fd526f569 100644 --- a/.env.example +++ b/.env.example @@ -748,10 +748,10 @@ HELP_AND_FAQ_URL=https://librechat.ai # REDIS_PING_INTERVAL=300 # Force specific cache namespaces to use in-memory storage even when Redis is enabled -# Comma-separated list of CacheKeys (e.g., ROLES,MESSAGES) -# For blue/green deployments, keep YAML-derived config per-container while sharing tools via Redis: +# Comma-separated list of CacheKeys +# Defaults to CONFIG_STORE,APP_CONFIG so YAML-derived config stays per-container (safe for blue/green deployments) +# Set to empty string to force all namespaces through Redis: FORCED_IN_MEMORY_CACHE_NAMESPACES= # FORCED_IN_MEMORY_CACHE_NAMESPACES=CONFIG_STORE,APP_CONFIG -# FORCED_IN_MEMORY_CACHE_NAMESPACES=ROLES,MESSAGES # Leader Election Configuration (for multi-instance deployments with Redis) # Duration in seconds that the leader lease is valid before it expires (default: 25) diff --git a/packages/api/src/cache/__tests__/cacheConfig.spec.ts b/packages/api/src/cache/__tests__/cacheConfig.spec.ts index 007404f669..0488cfecfc 100644 --- a/packages/api/src/cache/__tests__/cacheConfig.spec.ts +++ b/packages/api/src/cache/__tests__/cacheConfig.spec.ts @@ -215,16 +215,16 @@ describe('cacheConfig', () => { }).rejects.toThrow('Invalid cache keys in FORCED_IN_MEMORY_CACHE_NAMESPACES: INVALID_KEY'); }); - test('should handle empty string gracefully', async () => { + test('should produce empty array when set to empty string (opt out of defaults)', async () => { process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES = ''; const { cacheConfig } = await import('../cacheConfig'); expect(cacheConfig.FORCED_IN_MEMORY_CACHE_NAMESPACES).toEqual([]); }); - test('should handle undefined env var gracefully', async () => { + test('should default to CONFIG_STORE and APP_CONFIG when env var is not set', async () => { const { cacheConfig } = await import('../cacheConfig'); - expect(cacheConfig.FORCED_IN_MEMORY_CACHE_NAMESPACES).toEqual([]); + expect(cacheConfig.FORCED_IN_MEMORY_CACHE_NAMESPACES).toEqual(['CONFIG_STORE', 'APP_CONFIG']); }); test('should accept TOOL_CACHE as a valid namespace', async () => { diff --git a/packages/api/src/cache/cacheConfig.ts b/packages/api/src/cache/cacheConfig.ts index 32ea2cddd1..0d4304f5c3 100644 --- a/packages/api/src/cache/cacheConfig.ts +++ b/packages/api/src/cache/cacheConfig.ts @@ -27,9 +27,14 @@ const USE_REDIS_STREAMS = // Comma-separated list of cache namespaces that should be forced to use in-memory storage // even when Redis is enabled. This allows selective performance optimization for specific caches. -const FORCED_IN_MEMORY_CACHE_NAMESPACES = process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES - ? process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES.split(',').map((key) => key.trim()) - : []; +// Defaults to CONFIG_STORE,APP_CONFIG so YAML-derived config stays per-container. +// Set to empty string to force all namespaces through Redis. +const FORCED_IN_MEMORY_CACHE_NAMESPACES = + process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES !== undefined + ? process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES.split(',') + .map((key) => key.trim()) + .filter(Boolean) + : [CacheKeys.CONFIG_STORE, CacheKeys.APP_CONFIG]; // Validate against CacheKeys enum if (FORCED_IN_MEMORY_CACHE_NAMESPACES.length > 0) {