cache-manager: add store to Cache (#42554)

This commit is contained in:
jwigley
2020-02-26 15:37:33 -08:00
committed by GitHub
parent 7752c903f5
commit 9583dfad4a
2 changed files with 49 additions and 2 deletions
@@ -48,3 +48,26 @@ memoryCache.wrap<{ id: number, name: string }>(key, (cb: any) => {
});
});
memoryCache.store.keys().then((result) => {
//console.log(result);
});
const multiCache = cacheManager.multiCaching([memoryCache]);
multiCache.set('foo', 'bar', { ttl: ttl }, (err) => {
if (err) {
throw err;
}
multiCache.get('foo', (err, result) => {
// console.log(result);
multiCache.del('foo', (err) => {
});
});
});
+26 -2
View File
@@ -56,7 +56,31 @@ export interface Cache {
set<T>(key: string, value: T, ttl: number): Promise<any>;
set<T>(key: string, value: T, options: CachingConfig, callback: (error: any) => void): void;
set<T>(key: string, value: T, ttl: number, callback: (error: any) => void): void;
// Because the library accepts multiple keys as arguments but not as an array and rather as individual parameters
// of the function, the type definition had to be changed to this rather than specific ones
// actual definitions would looks like this (impossible in typescript):
// wrap<T>(...keys: string[], work: (callback: (error: any, result: T) => void) => void, options: CachingConfig, callback: (error: any, result: T) => void): void
// wrap<T>(...keys: string[], work: (callback: (error: any, result: T) => void) => void, callback: (error: any, result: T) => void): void
// wrap<T>(...keys: string[], work: (callback: (error: any, result: T) => void) => void, options: CachingConfig): void
// wrap<T>(...keys: string[], work: (callback: (error: any, result: T) => void) => void): Promise<any>;
wrap<T>(...args: WrapArgsType<T>[]): Promise<any>;
get<T>(key: string, callback: (error: any, result: T) => void): void;
get<T>(key: string): Promise<any>;
del(key: string, callback: (error: any) => void): void;
del(key: string): Promise<any>;
store: Store;
}
export interface MultiCache {
set<T>(key: string, value: T, options: CachingConfig): Promise<any>;
set<T>(key: string, value: T, ttl: number): Promise<any>;
set<T>(key: string, value: T, options: CachingConfig, callback: (error: any) => void): void;
set<T>(key: string, value: T, ttl: number, callback: (error: any) => void): void;
// Because the library accepts multiple keys as arguments but not as an array and rather as individual parameters
// of the function, the type definition had to be changed to this rather than specific ones
// actual definitions would looks like this (impossible in typescript):
@@ -74,4 +98,4 @@ export interface Cache {
}
export function caching(IConfig: StoreConfig & CacheOptions): Cache;
export function multiCaching(Caches: Cache[], options?: CacheOptions): Cache;
export function multiCaching(Caches: Cache[], options?: CacheOptions): MultiCache;