feat(cache): enhance tryGet method to support generic return types (#19261)

This commit is contained in:
纪华裕 2025-06-03 09:37:43 +08:00 committed by GitHub
parent c47d19c01d
commit 401c0c563f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 2 additions and 2 deletions

View File

@ -71,7 +71,7 @@ export default {
* @param refresh Whether to renew the cache expiration time when the cache is hit. `true` by default.
* @returns
*/
tryGet: async (key: string, getValueFunc: () => Promise<string | Record<string, any>>, maxAge = config.cache.contentExpire, refresh = true) => {
tryGet: async <T extends string | Record<string, any>>(key: string, getValueFunc: () => Promise<T>, maxAge = config.cache.contentExpire, refresh = true) => {
if (typeof key !== 'string') {
throw new TypeError('Cache key must be a string');
}
@ -87,7 +87,7 @@ export default {
v = parsed;
}
return v;
return v as T;
} else {
const value = await getValueFunc();
cacheModule.set(key, value, maxAge);