import { LRUCache } from 'lru-cache' /** * Cache entry for group keys */ export interface GroupKeyCacheEntry { secretKey: string version: number isActive: boolean // Active keys use shorter TTL, historical use longer } export const DEFAULT_KEY_CACHE_MAX_SIZE = 1000 export const DEFAULT_ACTIVE_TTL = 60 * 60 * 1000 // 1 hour export const DEFAULT_HISTORICAL_TTL = 24 * 60 * 60 * 1000 // 24 hours /** * Create a cache for group keys with TTL support */ export function createKeyCache( maxSize: number, ): LRUCache { // This will have a custom TTL based on whether key is active or historical return new LRUCache({ max: maxSize, ttlAutopurge: true, ttlResolution: 1000, // Check expiry every second updateAgeOnGet: false, // Don't refresh TTL on access (cache invalidation, not LRU) updateAgeOnHas: false, }) } /** * Service auth token cache entry */ export interface ServiceAuthTokenEntry { token: string expiresAt: number // Unix timestamp in milliseconds } const DEFAULT_TOKEN_TTL = 60 * 1000 // 60 seconds /** * Create a cache for service auth tokens with 60s TTL */ export function createTokenCache(): LRUCache { return new LRUCache({ max: 100, ttl: DEFAULT_TOKEN_TTL, ttlAutopurge: true, ttlResolution: 1000, updateAgeOnGet: false, updateAgeOnHas: false, }) } /** * Generate cache key for group key */ export function getGroupKeyCacheKey(groupId: string, version?: number): string { return version ? `${groupId}:${version}` : `${groupId}:latest` } /** * Generate cache key for service auth token */ export function getServiceAuthCacheKey(aud: string, lxm: string): string { return `${aud}:${lxm}` }