lunisolar
Version:
专业农历库,支持公历阴历互转,支持各类黄历数据查询,如八字四柱、阴历、神煞宜忌、时辰吉凶、建除十二神、胎神占方、五行纳音等。支持自定义插件。
18 lines (17 loc) • 731 B
text/typescript
export function cache(cacheKey: string, isArgsAffectKey: boolean = false): MethodDecorator {
return function (target, propertyKey, descriptor) {
const original =
descriptor.value === void 0 ? (descriptor.get as Function) : (descriptor.value as Function)
const getOrValue = descriptor.value === void 0 ? 'get' : 'value'
;(descriptor as any)[getOrValue] = function (this: any, ...args: any[]) {
if (args.length > 0 && isArgsAffectKey) {
const argsStr = JSON.stringify(args)
cacheKey += argsStr
}
if (this.cache.has(cacheKey)) return this.cache.get(cacheKey)
const result = original.call(this, ...args)
this.cache.set(cacheKey, result)
return result
}
}
}