diff --git a/packages/shared/src/controller/RuleManager.ts b/packages/shared/src/controller/RuleManager.ts index 09b42da2..68a2d47d 100644 --- a/packages/shared/src/controller/RuleManager.ts +++ b/packages/shared/src/controller/RuleManager.ts @@ -69,6 +69,9 @@ export class RuleManager extends BaseController { cacheKey({ args }) { const { filePath = '', chapterPath = '', ruleId = '' } = args[0]; return `content@${ruleId || '__local__'}@${md5(filePath)}@v3_${md5(chapterPath)}`; + }, + verify(data: any) { + return !!data?.content?.length; } }) async content({ filePath, chapterPath, ruleId }: { ruleId: string; filePath: string; chapterPath: string }) { @@ -79,6 +82,9 @@ export class RuleManager extends BaseController { } // 本地 const content = await createBookParser(filePath).getContent(chapterPath); + + if (!content.length) throw new Error('获取内容失败'); + return { content }; diff --git a/packages/shared/src/decorators/create-cache-decorator.ts b/packages/shared/src/decorators/create-cache-decorator.ts index 7ab2369c..1a4cb085 100644 --- a/packages/shared/src/decorators/create-cache-decorator.ts +++ b/packages/shared/src/decorators/create-cache-decorator.ts @@ -16,8 +16,14 @@ function isEmpty(data: any): boolean { else return !Object.keys(data).length; } +export interface DecoratorArgs { + ttl?: number; + cacheKey: (arg: { className: string; methodName: string; args: any[] }) => string; + verify?: (data: any) => boolean; +} + export function createCacheDecorator(options: CreateCacheDecoratorOptions) { - return (decoratorArgs: { ttl?: number; cacheKey: (arg: { className: string; methodName: string; args: any[] }) => string }): MethodDecorator => { + return (decoratorArgs: DecoratorArgs): MethodDecorator => { return (_target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor) => { const fn = descriptor.value; @@ -35,8 +41,9 @@ export function createCacheDecorator(options: CreateCacheDecoratorOptions) } const cachedResult = await options.getItem(cacheKey); + const verified = typeof decoratorArgs.verify !== 'function' || decoratorArgs.verify(cachedResult); - if (!isEmpty(cachedResult)) { + if (!isEmpty(cachedResult) && verified) { return cachedResult; } else { const result = await fn.apply(this, args);