|
1 |
| -import { Keyv } from 'keyv' |
2 |
| -import type { CacheableMemoryOptions } from 'cacheable' |
3 |
| -import { KeyvCacheableMemory } from 'cacheable' |
| 1 | +import { LRUCache } from 'lru-cache' |
| 2 | +import cloneDeep from 'lodash.clonedeep' |
| 3 | +import type { Cache, Config, Store } from 'cache-manager' |
4 | 4 |
|
5 |
| -export type MemoryConfig = CacheableMemoryOptions & { |
6 |
| - namespace?: string |
| 5 | +function clone<T>(object: T): T { |
| 6 | + if (typeof object === 'object' && object !== null) |
| 7 | + return cloneDeep(object) |
| 8 | + |
| 9 | + return object |
7 | 10 | }
|
8 | 11 |
|
9 |
| -export function memoryStore(options?: MemoryConfig) { |
10 |
| - const store = new KeyvCacheableMemory(options) |
11 |
| - const keyv = new Keyv({ store }, { namespace: '' }) |
12 |
| - return keyv |
| 12 | +type LRU = LRUCache<string, any> |
| 13 | + |
| 14 | +type Pre = LRUCache.OptionsTTLLimit<string, any, unknown> |
| 15 | +type Options = Omit<Pre, 'ttlAutopurge'> & Partial<Pick<Pre, 'ttlAutopurge'>> |
| 16 | +export type MemoryConfig = { |
| 17 | + max?: number |
| 18 | + sizeCalculation?: (value: unknown, key: string) => number |
| 19 | + shouldCloneBeforeSet?: boolean |
| 20 | +} & Options & |
| 21 | +Config |
| 22 | + |
| 23 | +export type MemoryStore = Store & { |
| 24 | + dump: LRU['dump'] |
| 25 | + load: LRU['load'] |
| 26 | + calculatedSize: LRU['calculatedSize'] |
| 27 | + get size(): number |
| 28 | +} |
| 29 | +export type MemoryCache = Cache<MemoryStore> |
| 30 | + |
| 31 | +/** |
| 32 | + * Wrapper for lru-cache. |
| 33 | + */ |
| 34 | +export function memoryStore(arguments_?: MemoryConfig): MemoryStore { |
| 35 | + const shouldCloneBeforeSet = arguments_?.shouldCloneBeforeSet !== false // Clone by default |
| 36 | + const isCacheable = arguments_?.isCacheable ?? (value => value !== undefined) |
| 37 | + |
| 38 | + const lruOptions = { |
| 39 | + ttlAutopurge: true, |
| 40 | + ...arguments_, |
| 41 | + max: arguments_?.max ?? 500, |
| 42 | + ttl: arguments_?.ttl === undefined ? 0 : arguments_.ttl, |
| 43 | + } |
| 44 | + |
| 45 | + const lruCache = new LRUCache(lruOptions) |
| 46 | + |
| 47 | + return { |
| 48 | + async del(key) { |
| 49 | + lruCache.delete(key) |
| 50 | + }, |
| 51 | + get: async <T>(key: string) => lruCache.get(key) as T, |
| 52 | + keys: async (pattern?: string) => { |
| 53 | + const keys = [...lruCache.keys()] |
| 54 | + if (!pattern) |
| 55 | + return keys |
| 56 | + |
| 57 | + const regex = new RegExp(`(?<!.)${pattern}`) |
| 58 | + return keys.filter(key => regex.test(key)) |
| 59 | + }, |
| 60 | + |
| 61 | + mget: async (...arguments_) => arguments_.map(x => lruCache.get(x)), |
| 62 | + async mset(arguments_, ttl?) { |
| 63 | + const opt = { ttl: ttl ?? lruOptions.ttl } as const |
| 64 | + for (const [key, value] of arguments_) { |
| 65 | + if (!isCacheable(value)) |
| 66 | + throw new Error(`no cacheable value ${JSON.stringify(value)}`) |
| 67 | + |
| 68 | + if (shouldCloneBeforeSet) |
| 69 | + lruCache.set(key, clone(value), opt) |
| 70 | + else |
| 71 | + lruCache.set(key, value, opt) |
| 72 | + } |
| 73 | + }, |
| 74 | + async mdel(...arguments_) { |
| 75 | + for (const key of arguments_) |
| 76 | + lruCache.delete(key) |
| 77 | + }, |
| 78 | + async reset() { |
| 79 | + lruCache.clear() |
| 80 | + }, |
| 81 | + ttl: async key => lruCache.getRemainingTTL(key), |
| 82 | + async set(key, value, opt) { |
| 83 | + if (!isCacheable(value)) |
| 84 | + throw new Error(`no cacheable value ${JSON.stringify(value)}`) |
| 85 | + |
| 86 | + if (shouldCloneBeforeSet) |
| 87 | + value = clone(value) |
| 88 | + |
| 89 | + const ttl = opt ?? lruOptions.ttl |
| 90 | + |
| 91 | + lruCache.set(key, value, { ttl }) |
| 92 | + }, |
| 93 | + get calculatedSize() { |
| 94 | + return lruCache.calculatedSize |
| 95 | + }, |
| 96 | + /** |
| 97 | + * This method is not available in the caching modules. |
| 98 | + */ |
| 99 | + get size() { |
| 100 | + return lruCache.size |
| 101 | + }, |
| 102 | + /** |
| 103 | + * This method is not available in the caching modules. |
| 104 | + */ |
| 105 | + dump: () => lruCache.dump(), |
| 106 | + /** |
| 107 | + * This method is not available in the caching modules. |
| 108 | + */ |
| 109 | + load(...arguments_: Parameters<LRU['load']>) { |
| 110 | + lruCache.load(...arguments_) |
| 111 | + }, |
| 112 | + } |
13 | 113 | }
|
0 commit comments