Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose context to storage strategy methods. #105

Merged
merged 1 commit into from
Apr 23, 2021
Merged
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
14 changes: 7 additions & 7 deletions cacheable.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
: new cacheConfig.storageStrategy();
const pendingCachePairs: Array<ICachePair<Observable<any>>> = [];
if (cacheConfig.cacheModifier) {
cacheConfig.cacheModifier.subscribe(callback => storageStrategy.addMany(callback(storageStrategy.getAll(cacheKey)), cacheKey))
cacheConfig.cacheModifier.subscribe(callback => storageStrategy.addMany(callback(storageStrategy.getAll(cacheKey, this)), cacheKey, this))
}
/**
* subscribe to the globalCacheBuster
Expand All @@ -32,7 +32,7 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
? cacheConfig.cacheBusterObserver
: empty()
).subscribe(_ => {
storageStrategy.removeAll(cacheKey);
storageStrategy.removeAll(cacheKey, this);
pendingCachePairs.length = 0;
});
const cacheResolver = cacheConfig.cacheResolver || GlobalCacheConfig.cacheResolver;
Expand All @@ -46,7 +46,7 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {

/* use function instead of an arrow function to keep context of invocation */
(propertyDescriptor.value as any) = function(...parameters: Array<any>) {
const cachePairs: Array<ICachePair<Observable<any>>> = storageStrategy.getAll(cacheKey);
const cachePairs: Array<ICachePair<Observable<any>>> = storageStrategy.getAll(cacheKey, this);
let cacheParameters = cacheConfig.cacheHasher(parameters);
let _foundCachePair = cachePairs.find(cp =>
cacheConfig.cacheResolver(cp.parameters, cacheParameters));
Expand All @@ -64,14 +64,14 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
/**
* cache duration has expired - remove it from the cachePairs array
*/
storageStrategy.remove ? storageStrategy.remove(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey) : storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
storageStrategy.remove ? storageStrategy.remove(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey, this) : storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey, this);
_foundCachePair = null;
} else if (cacheConfig.slidingExpiration || GlobalCacheConfig.slidingExpiration) {
/**
* renew cache duration
*/
_foundCachePair.created = new Date();
storageStrategy.update ? storageStrategy.update(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey) : storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
storageStrategy.update ? storageStrategy.update(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey, this) : storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey, this);
}
}

Expand Down Expand Up @@ -111,13 +111,13 @@ export function Cacheable(cacheConfig: IObservableCacheConfig = {}) {
((cacheConfig.maxCacheCount || GlobalCacheConfig.maxCacheCount) &&
(cacheConfig.maxCacheCount || GlobalCacheConfig.maxCacheCount) < cachePairs.length + 1)
) {
storageStrategy.remove ? storageStrategy.remove(0, cachePairs[0], cacheKey) : storageStrategy.removeAtIndex(0, cacheKey);
storageStrategy.remove ? storageStrategy.remove(0, cachePairs[0], cacheKey, this) : storageStrategy.removeAtIndex(0, cacheKey, this);
}
storageStrategy.add({
parameters: cacheParameters,
response,
created: (cacheConfig.maxAge || GlobalCacheConfig.maxAge) ? new Date() : null
}, cacheKey);
}, cacheKey, this);
}
}),
publishReplay(1),
Expand Down
16 changes: 8 additions & 8 deletions common/IAsyncStorageStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {ICachePair} from '.';

export abstract class IAsyncStorageStrategy {
abstract getAll(cacheKey: string): Array<ICachePair<any>> | Promise<Array<ICachePair<any>>>;
abstract add(entity: ICachePair<any>, cacheKey: string): void | Promise<void>;
abstract getAll(cacheKey: string, ctx?: any): Array<ICachePair<any>> | Promise<Array<ICachePair<any>>>;
abstract add(entity: ICachePair<any>, cacheKey: string, ctx?: any): void | Promise<void>;
/**
* @deprecated Use update instead.
*/
abstract updateAtIndex(index: number, entity: ICachePair<any>, cacheKey: string): void | Promise<void>;
abstract update?(index: number, entity: ICachePair<any>, cacheKey: string): Promise<void>;
abstract updateAtIndex(index: number, entity: ICachePair<any>, cacheKey: string, ctx?: any): void | Promise<void>;
abstract update?(index: number, entity: ICachePair<any>, cacheKey: string, ctx?: any): Promise<void>;
/**
* @deprecated Use remove instead.
*/
abstract removeAtIndex(index: number, cacheKey: string): void | Promise<void>;
abstract remove?(index: number, entity: ICachePair<any>, cacheKey: string): Promise<void>;
abstract removeAll(cacheKey: string): void | Promise<void>;
abstract addMany(entities: ICachePair<any>[], cacheKey: string): Promise<void>;
abstract removeAtIndex(index: number, cacheKey: string, ctx?: any): void | Promise<void>;
abstract remove?(index: number, entity: ICachePair<any>, cacheKey: string, ctx?: any): Promise<void>;
abstract removeAll(cacheKey: string, ctx?: any): void | Promise<void>;
abstract addMany(entities: ICachePair<any>[], cacheKey: string, ctx?: any): Promise<void>;
}
16 changes: 8 additions & 8 deletions common/IStorageStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {ICachePair} from '.';

export abstract class IStorageStrategy {
abstract getAll(cacheKey: string): Array<ICachePair<any>>;
abstract add(entity: ICachePair<any>, cacheKey: string): void;
abstract getAll(cacheKey: string, ctx?: any): Array<ICachePair<any>>;
abstract add(entity: ICachePair<any>, cacheKey: string, ctx?: any): void;
/**
* @deprecated Use update instead.
*/
abstract updateAtIndex(index: number, entity: ICachePair<any>, cacheKey: string): void;
abstract update?(index: number, entity: ICachePair<any>, cacheKey: string): void;
abstract updateAtIndex(index: number, entity: ICachePair<any>, cacheKey: string, ctx?: any): void;
abstract update?(index: number, entity: ICachePair<any>, cacheKey: string, ctx?: any): void;
/**
* @deprecated Use remove instead.
*/
abstract removeAtIndex(index: number, cacheKey: string): void;
abstract remove?(index: number, entity: ICachePair<any>, cacheKey: string): void;
abstract removeAll(cacheKey: string): void;
abstract addMany(entities: ICachePair<any>[], cacheKey: string): void;
abstract removeAtIndex(index: number, cacheKey: string, ctx?: any): void;
abstract remove?(index: number, entity: ICachePair<any>, cacheKey: string, ctx?: any): void;
abstract removeAll(cacheKey: string, ctx?: any): void;
abstract addMany(entities: ICachePair<any>[], cacheKey: string, ctx?: any): void;
}
4 changes: 2 additions & 2 deletions common/InMemoryStorageStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {ICachePair} from '.';
export class InMemoryStorageStrategy extends IStorageStrategy {
private cachePairs: Array<ICachePair<any>> = [];

add(cachePair: ICachePair<any>) {
this.cachePairs.push(cachePair)
add(cachePair: ICachePair<any>, cacheKey: string, ctx?: any) {
this.cachePairs.push(cachePair);
};

addMany(cachePairs: ICachePair<any>[]) {
Expand Down
14 changes: 7 additions & 7 deletions promise.cacheable.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ const getResponse = (oldMethod: Function, cacheKey: string, cacheConfig: ICacheC
/**
* cache duration has expired - remove it from the cachePairs array
*/
storageStrategy.remove ? storageStrategy.remove(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey) : storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
storageStrategy.remove ? storageStrategy.remove(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey, this) : storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey, this);
_foundCachePair = null;
} else if (cacheConfig.slidingExpiration || GlobalCacheConfig.slidingExpiration) {
/**
* renew cache duration
*/
_foundCachePair.created = new Date();
storageStrategy.update ? storageStrategy.update(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey) : storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
storageStrategy.update ? storageStrategy.update(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey, this) : storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey, this);
}
}

Expand Down Expand Up @@ -60,13 +60,13 @@ const getResponse = (oldMethod: Function, cacheKey: string, cacheConfig: ICacheC
((cacheConfig.maxCacheCount || GlobalCacheConfig.maxCacheCount) &&
(cacheConfig.maxCacheCount || GlobalCacheConfig.maxCacheCount) < cachePairs.length + 1)
) {
storageStrategy.remove ? storageStrategy.remove(0, cachePairs[0], cacheKey) : storageStrategy.removeAtIndex(0, cacheKey);
storageStrategy.remove ? storageStrategy.remove(0, cachePairs[0], cacheKey, this) : storageStrategy.removeAtIndex(0, cacheKey, this);
}
storageStrategy.add({
parameters: cacheParameters,
response,
created: (cacheConfig.maxAge || GlobalCacheConfig.maxAge) ? new Date() : null
}, cacheKey);
}, cacheKey, this);
}

return response;
Expand Down Expand Up @@ -116,7 +116,7 @@ export function PCacheable(cacheConfig: ICacheConfig = {}) {
: new cacheConfig.storageStrategy();
const pendingCachePairs: Array<ICachePair<Promise<any>>> = [];
if (cacheConfig.cacheModifier) {
cacheConfig.cacheModifier.subscribe(async callback => storageStrategy.addMany(callback(await storageStrategy.getAll(cacheKey)), cacheKey))
cacheConfig.cacheModifier.subscribe(async callback => storageStrategy.addMany(callback(await storageStrategy.getAll(cacheKey, this)), cacheKey, this))
}
/**
* subscribe to the promiseGlobalCacheBusterNotifier
Expand All @@ -129,7 +129,7 @@ export function PCacheable(cacheConfig: ICacheConfig = {}) {
? cacheConfig.cacheBusterObserver
: empty()
).subscribe(_ => {
storageStrategy.removeAll(cacheKey);
storageStrategy.removeAll(cacheKey, this);
pendingCachePairs.length = 0;
});

Expand All @@ -147,7 +147,7 @@ export function PCacheable(cacheConfig: ICacheConfig = {}) {
const promiseImplementation = typeof GlobalCacheConfig.promiseImplementation === 'function' && (GlobalCacheConfig.promiseImplementation !== Promise) ?
(GlobalCacheConfig.promiseImplementation as () => PromiseConstructorLike).call(this)
: GlobalCacheConfig.promiseImplementation as PromiseConstructorLike;
let cachePairs = storageStrategy.getAll(cacheKey);
let cachePairs = storageStrategy.getAll(cacheKey, this);
if (!(cachePairs instanceof promiseImplementation)) {
cachePairs = promiseImplementation.resolve(cachePairs);
}
Expand Down
Loading