|
1 | | -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
2 | | -// eslint-disable-next-line import/no-unassigned-import |
3 | | -import 'reflect-metadata'; |
4 | | -import cacheManager, {type Cache} from 'cache-manager'; |
5 | | -import {redisStore} from 'cache-manager-ioredis-yet'; |
6 | | -import {get} from 'lodash'; |
7 | | - |
8 | | -export type CacheInitializationConfig = { |
9 | | - host?: string; |
10 | | - port?: number; |
11 | | - password?: string; |
12 | | - ttl: number; |
13 | | -}; |
14 | | - |
15 | | -export type KeySelector = ((...arguments_: unknown[]) => unknown[]) | string | string[]; |
16 | | - |
17 | | -export type CacheOptions = { |
18 | | - ttl: number; |
19 | | - keySelector: KeySelector; |
20 | | -} & Partial<CacheInitializationConfig>; |
21 | | - |
22 | | -let cacheManagerInstance: Cache | undefined; |
23 | | - |
24 | | -/** |
25 | | - * Initializes the cache manager with ioredis store by default. |
26 | | - */ |
27 | | -export async function initializeCache(config: CacheInitializationConfig): Promise<void> { |
28 | | - const store = async () => redisStore({ |
29 | | - host: config.host ?? 'localhost', |
30 | | - port: config.port ?? 6379, |
31 | | - password: config.password, |
32 | | - }); |
33 | | - |
34 | | - cacheManagerInstance = await cacheManager.caching(store, {ttl: config.ttl}); |
35 | | -} |
36 | | - |
37 | | -/** |
38 | | - * Automatically initializes cache if not already initialized and initialization data is present. |
39 | | - */ |
40 | | -export async function ensureCacheInitialized(config?: CacheInitializationConfig): Promise<void> { |
41 | | - if (!cacheManagerInstance && config) { |
42 | | - await initializeCache(config); |
43 | | - } |
44 | | -} |
45 | | - |
46 | | -/** |
47 | | - * Decorator to provide metadata for caching with TTL and key selection. |
48 | | - */ |
49 | | -export function cacheMeta(options: CacheOptions) { |
50 | | - return async (target: Record<string, unknown>, propertyKey: string): Promise<void> => { |
51 | | - await ensureCacheInitialized(options); |
52 | | - Reflect.defineMetadata('cacheKeySelector', options.keySelector, target, propertyKey); |
53 | | - Reflect.defineMetadata('cacheTtl', options.ttl, target, propertyKey); |
54 | | - }; |
55 | | -} |
56 | | - |
57 | | -/** |
58 | | - * Decorator to handle caching directly with TTL and key selection. |
59 | | - */ |
60 | | -export function cache(options: CacheOptions) { |
61 | | - return (target: unknown, propertyKey: string, descriptor: PropertyDescriptor): void => { |
62 | | - ensureCacheInitialized(options).catch((error: unknown) => { |
63 | | - console.error(error); |
64 | | - }); |
65 | | - const originalMethod = descriptor.value; |
66 | | - |
67 | | - descriptor.value = async function (...arguments_: unknown[]): Promise<unknown> { |
68 | | - if (!cacheManagerInstance) { |
69 | | - throw new Error('Cache not initialized.'); |
70 | | - } |
71 | | - |
72 | | - const cacheKey = createCacheKey(arguments_, options.keySelector); |
73 | | - const cachedResult = await cacheManagerInstance.get(cacheKey); |
74 | | - if (cachedResult) { |
75 | | - return cachedResult; |
76 | | - } |
77 | | - |
78 | | - const result = await originalMethod.apply(this, arguments_); |
79 | | - await cacheManagerInstance.set(cacheKey, result, options.ttl); |
80 | | - |
81 | | - return result; |
82 | | - }; |
83 | | - }; |
84 | | -} |
85 | | - |
86 | | -/** |
87 | | - * Creates a cache key based on the key selector function or paths. |
88 | | - */ |
89 | | -export function createCacheKey(arguments_: unknown[], keySelector: KeySelector): string { |
90 | | - if (typeof keySelector === 'function') { |
91 | | - const result = keySelector(...arguments_); |
92 | | - return Array.isArray(result) ? result.join(':') : String(result); |
93 | | - } |
94 | | - |
95 | | - if (typeof keySelector === 'string') { |
96 | | - return String(get(arguments_[0] as Record<string, unknown>, keySelector)); |
97 | | - } |
98 | | - |
99 | | - if (Array.isArray(keySelector)) { |
100 | | - return keySelector.map(path => String(get(arguments_[0] as Record<string, unknown>, path))).join(':'); |
101 | | - } |
102 | | - |
103 | | - throw new Error('Invalid key selector type.'); |
104 | | -} |
105 | | - |
106 | | -/** |
107 | | - * Wraps and caches the original function using cache-manager and supports dynamic key selection. |
108 | | - */ |
109 | | -export function cacheFunction( |
110 | | - function_: (...arguments_: unknown[]) => Promise<unknown>, |
111 | | - options: CacheOptions, |
112 | | -): (...arguments_: unknown[]) => Promise<unknown> { |
113 | | - ensureCacheInitialized(options).catch((error: unknown) => { |
114 | | - console.error(error); |
115 | | - }); |
116 | | - return async (...arguments_: unknown[]): Promise<unknown> => { |
117 | | - if (!cacheManagerInstance) { |
118 | | - throw new Error('Cache not initialized.'); |
119 | | - } |
120 | | - |
121 | | - const cacheKey = createCacheKey(arguments_, options.keySelector); |
122 | | - const cachedResult = await cacheManagerInstance.get(cacheKey); |
123 | | - if (cachedResult) { |
124 | | - return cachedResult; |
125 | | - } |
126 | | - |
127 | | - const result = await function_(...arguments_); |
128 | | - await cacheManagerInstance.set(cacheKey, result, options.ttl); |
129 | | - |
130 | | - return result; |
131 | | - }; |
132 | | -} |
| 1 | +console.log('test'); |
0 commit comments