diff --git a/README.md b/README.md index 256a45b..fdc26d3 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,12 @@ by setting this to `CacheReturnType.Promise`. cacheUntilRejected?: boolean When caching a method returning a Promise, this option will clear the relevant cache when the Promise is -rejected. If the Promise resolves normally, the cache persists. +rejected. If the Promise resolves normally, the cache persists. + + calculateCacheKeyFunction?: (target: any, args: any) => string; + +If necessary, it is possible to set your own function for creating a key for caching + ### `MethodCacheService` diff --git a/src/core/decorator/base-cache.decorator.spec.ts b/src/core/decorator/base-cache.decorator.spec.ts index 2e49c15..a9bfbec 100644 --- a/src/core/decorator/base-cache.decorator.spec.ts +++ b/src/core/decorator/base-cache.decorator.spec.ts @@ -8,6 +8,17 @@ class TestCache { public getterCalled: number = 0; + public b: number = 0; + + @baseCacheDecorator(CacheType.Memory, { + calculateCacheKeyFunction: (target: TestCache, args) => { + return JSON.stringify({ uniqCacheKey: target.b, ...args }); + } + }) + public sum(a): number { + return a + this.b; + } + @baseCacheDecorator(CacheType.Memory) get testGetter(): string { this.getterCalled++; @@ -86,4 +97,12 @@ describe('Cache decorator is properly set', () => { expect(noCache3).toEqual(cache3); expect(noCache3).toEqual(value1 + increment - value2); }); + it('if a key calculation method is passed, it is used for calculations', () => { + const testCache2 = new TestCache(); + testCache.b = 1; + testCache2.b = 10; + + expect(testCache.sum(1)).toEqual(2); + expect(testCache2.sum(1)).toEqual(11); + }); }); diff --git a/src/core/interface/base-cache-options.ts b/src/core/interface/base-cache-options.ts index 1f8b2df..508d824 100644 --- a/src/core/interface/base-cache-options.ts +++ b/src/core/interface/base-cache-options.ts @@ -1,8 +1,9 @@ -import { CacheReturnType } from '../enum/cache-return-type.enum'; +import {CacheReturnType} from '../enum/cache-return-type.enum'; export interface BaseCacheOptions { - key?: string; - returnType?: CacheReturnType; - ttl?: string | number | Date; - cacheUntilRejected?: boolean; + key?: string; + returnType?: CacheReturnType; + ttl?: string | number | Date; + cacheUntilRejected?: boolean; + calculateCacheKeyFunction?: (target: any, args: any) => string; } diff --git a/src/core/util/decorator.util.ts b/src/core/util/decorator.util.ts index 4fefb38..e6e2851 100644 --- a/src/core/util/decorator.util.ts +++ b/src/core/util/decorator.util.ts @@ -24,8 +24,9 @@ export function createCacheDecorator( let container: CacheContainerOptions | undefined | null = null; return function(this: (...args) => any, ...args: any[]): any { - const argsString: string = JSON.stringify(args); - + const argsString: string = options.calculateCacheKeyFunction + ? options.calculateCacheKeyFunction(this, args) + : JSON.stringify(args); if (container === null) { container = getCacheContainer(target.constructor as any); if (container) {