UNPKG

@gabliam/cache

Version:
82 lines (81 loc) 2.79 kB
import { Container } from '@gabliam/core'; import { Cache } from '../cache'; export type KeyGenerator = (...args: any[]) => string | undefined; export interface CacheOptions { /** * names of caches */ cacheNames?: string | string[]; /** * Key generator. * By default it is a concatenation of all arguments (Use JSON stringify) */ keyGenerator?: KeyGenerator; /** * Define what it uses to generate the key. By default, use all arguments * Can be an expression. * * $args can be used (represents the arguments passed to the method). * * sample: key: '$args[0].name' */ key?: string; /** * Condition for caching (used before invocation of method) * $args can be used (represents the arguments passed to the method) * * sample : condition: '$args[0].id !== 1', */ condition?: string; /** * The unless parameter can be used to veto the adding of a value to the cache. * Unlike condition, unless expressions are evaluated after the method has been called. * * sample : unless: '$result !== null' */ unless?: string; } export interface CacheConfig { passCondition: (...vals: any[]) => boolean; veto: (args: any[], result: any) => boolean; extractArgs: (...vals: any[]) => any | undefined; caches: Cache[]; } export declare function isCacheOptions(obj: any): obj is CacheOptions; /** * Cache internal options */ export interface CacheInternalOptions { cacheNames: string | string[]; /** * Key generator. * By default it is a concatenation of all arguments (Use JSON stringify) */ keyGenerator: KeyGenerator; /** * Define what it uses to generate the key. By default, use all arguments * Can be an expression. * * $args can be used (represents the arguments passed to the method). * * sample: key: '$args[0].name' */ key?: string; /** * Condition for caching (used before invocation of method) * $args can be used (represents the arguments passed to the method) * * sample : condition: '$args[0].id !== 1', */ condition?: string; /** * The unless parameter can be used to veto the adding of a value to the cache. * Unlike condition, unless expressions are evaluated after the method has been called. * * sample : unless: '$result !== null' */ unless?: string; } export declare const getCacheGroup: (target: Object) => string; export declare function extractCacheInternalOptions(value?: string | string[] | CacheOptions): CacheInternalOptions; export declare function createCacheConfig(cacheGroup: string, container: Container, cacheInternalOptions: CacheInternalOptions): Promise<CacheConfig>;