|
| 1 | +export type CacheGetResult<T> = { value: T } | null; |
| 2 | + |
| 3 | +export interface CacheService { |
| 4 | + getJSON<T>(key: string): Promise<CacheGetResult<T>>; |
| 5 | + setJSON<T>(key: string, value: T, ttlSeconds: number): Promise<void>; |
| 6 | + del(key: string): Promise<void>; |
| 7 | + /** |
| 8 | + * Atomically increments a version counter stored at `key`. |
| 9 | + * |
| 10 | + * Implementations must return the new value. |
| 11 | + */ |
| 12 | + incr(key: string, ttlSeconds?: number): Promise<number>; |
| 13 | + /** |
| 14 | + * Returns current integer value for `key` or null if missing. |
| 15 | + */ |
| 16 | + getIncr(key: string): Promise<number | null>; |
| 17 | +} |
| 18 | + |
| 19 | +export class NoopCacheService implements CacheService { |
| 20 | + async getJSON<T>(_key: string): Promise<CacheGetResult<T>> { |
| 21 | + return null; |
| 22 | + } |
| 23 | + async setJSON<T>(_key: string, _value: T, _ttlSeconds: number): Promise<void> { |
| 24 | + return; |
| 25 | + } |
| 26 | + async del(_key: string): Promise<void> { |
| 27 | + return; |
| 28 | + } |
| 29 | + async incr(_key: string, _ttlSeconds?: number): Promise<number> { |
| 30 | + return 1; |
| 31 | + } |
| 32 | + async getIncr(_key: string): Promise<number | null> { |
| 33 | + return null; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Simple in-memory cache for unit tests. |
| 39 | + */ |
| 40 | +export class InMemoryCacheService implements CacheService { |
| 41 | + private store = new Map< |
| 42 | + string, |
| 43 | + { value: unknown; expiresAt: number | null } |
| 44 | + >(); |
| 45 | + private incrStore = new Map<string, { value: number; expiresAt: number | null }>(); |
| 46 | + |
| 47 | + async getJSON<T>(key: string): Promise<CacheGetResult<T>> { |
| 48 | + const entry = this.store.get(key); |
| 49 | + if (!entry) return null; |
| 50 | + if (entry.expiresAt !== null && Date.now() > entry.expiresAt) { |
| 51 | + this.store.delete(key); |
| 52 | + return null; |
| 53 | + } |
| 54 | + return { value: entry.value as T }; |
| 55 | + } |
| 56 | + |
| 57 | + async setJSON<T>(key: string, value: T, ttlSeconds: number): Promise<void> { |
| 58 | + const expiresAt = ttlSeconds > 0 ? Date.now() + ttlSeconds * 1000 : null; |
| 59 | + this.store.set(key, { value, expiresAt }); |
| 60 | + } |
| 61 | + |
| 62 | + async del(key: string): Promise<void> { |
| 63 | + this.store.delete(key); |
| 64 | + this.incrStore.delete(key); |
| 65 | + } |
| 66 | + |
| 67 | + async incr(key: string, ttlSeconds?: number): Promise<number> { |
| 68 | + const existing = this.incrStore.get(key); |
| 69 | + const current = existing ? existing.value : 0; |
| 70 | + const next = current + 1; |
| 71 | + const expiresAt = ttlSeconds && ttlSeconds > 0 ? Date.now() + ttlSeconds * 1000 : null; |
| 72 | + this.incrStore.set(key, { value: next, expiresAt }); |
| 73 | + return next; |
| 74 | + } |
| 75 | + |
| 76 | + async getIncr(key: string): Promise<number | null> { |
| 77 | + const entry = this.incrStore.get(key); |
| 78 | + if (!entry) return null; |
| 79 | + if (entry.expiresAt !== null && Date.now() > entry.expiresAt) { |
| 80 | + this.incrStore.delete(key); |
| 81 | + return null; |
| 82 | + } |
| 83 | + return entry.value; |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments