Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
19 changes: 19 additions & 0 deletions src/core/decorator/base-cache.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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);
});
});
11 changes: 6 additions & 5 deletions src/core/interface/base-cache-options.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 3 additions & 2 deletions src/core/util/decorator.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down