diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 82cd2626..103728e2 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -113,8 +113,12 @@ export const makeChatsSocket = (config: SocketConfig) => { * * MEMORY SAFETY: Limited by DEFAULT_CACHE_MAX_KEYS.SIGNAL_STORE with 1-hour TTL. * Auto-purges expired entries to maintain memory bounds. + * + * TYPE SAFETY: Only successful lookups (non-null values) are cached. + * Null/undefined values are NOT cached to prevent blocking newly arrived keys. + * LRUCache.get() returns undefined for missing keys. */ - const appStateSyncKeyCache = new LRUCache({ + const appStateSyncKeyCache = new LRUCache({ max: DEFAULT_CACHE_MAX_KEYS.SIGNAL_STORE, // Use constant from Defaults (10,000) ttl: DEFAULT_CACHE_TTLS.MSG_RETRY * 1000, // 1 hour TTL (convert seconds to ms) ttlAutopurge: true, // Automatically remove expired entries @@ -135,9 +139,8 @@ export const makeChatsSocket = (config: SocketConfig) => { // Use get() directly to avoid race between has() and get() (Fix: Copilot C) const cached = appStateSyncKeyCache.get(keyId) if (cached !== undefined) { - // Null in cache means we explicitly cached a null (which we don't do anymore) - // Undefined means not in cache - return cached ?? undefined + // Cache hit - return the cached key + return cached } // Cache miss - fetch from database