Skip to content

Instantly share code, notes, and snippets.

@drmikecrowe
Created May 31, 2020 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmikecrowe/ba393f22b6ddf1ee3b7a2d4a42315598 to your computer and use it in GitHub Desktop.
Save drmikecrowe/ba393f22b6ddf1ee3b7a2d4a42315598 to your computer and use it in GitHub Desktop.
cache-manager index.d.ts types
declare module 'cache-manager/caching' {
/**
* Generic caching interface that wraps any caching library with a compatible interface.
* @param args.store - The store must at least have `set` and a `get` functions.
* @param [args.isCacheableValue] - A callback function which is called
* with every value returned from cache or from a wrapped function. This lets you specify
* which values should and should not be cached. If the function returns true, it will be
* stored in cache. By default it caches everything except undefined.
*/
function caching(args: {
store: any | string;
isCacheableValue?: (...params: any[]) => any;
[x: string]: any;
}): void;
/**
* Checks if the current key is expiring. I.e., if a refreshThreshold is set for this cache
* and if the cache supports the ttl method, this method checks if the remaining ttl is
* less than the refreshThreshold.
* In all other cases this method's callback will contain "false" (ie. not expiring).
* @param key - The cache key to check.
*/
function checkRefreshThreshold(key: string, cb: (...params: any[]) => any): void;
/**
* Wraps a function in cache. I.e., the first time the function is run,
* its results are stored in cache so subsequent calls retrieve from cache
* instead of calling the function.
* You can pass any number of keys as long as the wrapped function returns
* an array with the same number of values and in the same order.
* @example
* var key = 'user_' + userId;
* cache.wrap(key, function(cb) {
* User.get(userId, cb);
* }, function(err, user) {
* console.log(user);
* });
*
* // Multiple keys
* var key = 'user_' + userId;
* var key2 = 'user_' + userId2;
* cache.wrap(key, key2, function(cb) {
* User.getMany([userId, userId2], cb);
* }, function(err, users) {
* console.log(users[0]);
* console.log(users[1]);
* });
* @param key - The cache key to use in cache operations. Can be one or many.
* @param work - The function to wrap
* @param [options] - options passed to `set` function
*/
function wrap(key: string, work: (...params: any[]) => any, cb: (...params: any[]) => any): void;
function wrap(key: string, work: (...params: any[]) => any, options: any, cb: (...params: any[]) => any): void;
/**
* Binds to the underlying store's `get` function.
*/
function get(): void;
/**
* Get multiple keys at once.
* Binds to the underlying store's `mget` function.
*/
function mget(): void;
/**
* Binds to the underlying store's `set` function.
*/
function set(): void;
/**
* Set multiple keys at once.
* It accepts any number of {key, value} pair
* Binds to the underlying store's `mset` function.
*/
function mset(): void;
/**
* Binds to the underlying store's `del` function if it exists.
*/
function del(): void;
/**
* Binds to the underlying store's `setex` function if it exists.
*/
function setex(): void;
/**
* Binds to the underlying store's `reset` function if it exists.
*/
function reset(): void;
/**
* Binds to the underlying store's `keys` function if it exists.
*/
function keys(): void;
/**
* Binds to the underlying store's `ttl` function if it exists.
*/
function ttl(): void;
}
declare module 'cache-manager/multiCaching' {
/**
* Module that lets you specify a hierarchy of caches.
* @param caches - Array of caching objects.
* @param [options.isCacheableValue] - A callback function which is called
* with every value returned from cache or from a wrapped function. This lets you specify
* which values should and should not be cached. If the function returns true, it will be
* stored in cache. By default it caches everything except undefined.
*
* If an underlying cache specifies its own isCacheableValue function, that function will
* be used instead of the multiCaching's _isCacheableValue function.
*/
function multiCaching(
caches: any[],
options?: {
isCacheableValue?: (...params: any[]) => any;
},
): void;
/**
* Dynamically set the ttl by context depending of the store
*/
var cacheOptions: any;
/**
* Wraps a function in one or more caches.
* Has same API as regular caching module.
*
* If a key doesn't exist in any cache, it gets set in all caches.
* If a key exists in a high-priority (e.g., first) cache, it gets returned immediately
* without getting set in other lower-priority caches.
* If a key doesn't exist in a higher-priority cache but exists in a lower-priority
* cache, it gets set in all higher-priority caches.
* You can pass any number of keys as long as the wrapped function returns
* an array with the same number of values and in the same order.
* @param key - The cache key to use in cache operations. Can be one or many.
* @param work - The function to wrap
* @param [options] - options passed to `set` function
*/
function wrap(key: string, work: (...params: any[]) => any, cb: (...params: any[]) => any): void;
function wrap(key: string, work: (...params: any[]) => any, options: any, cb: (...params: any[]) => any): void;
/**
* Set value in all caches
* @param [options] - to pass to underlying set function.
*/
function set(key: string, value: any, cb: (...params: any[]) => any): void;
function set(key: string, value: any, options: any, cb: (...params: any[]) => any): void;
/**
* Set multiple values in all caches
* Accepts an unlimited pair of {key, value}
* @param [options] - to pass to underlying set function.
*/
function mset(
key: string,
value: any,
key2?: string,
value2?: any,
options?: any,
cb?: (...params: any[]) => any,
): void;
/**
* Get value from highest level cache that has stored it.
* @param [options] - to pass to underlying get function.
*/
function get(key: string, cb: (...params: any[]) => any): void;
function get(key: string, options: any, cb: (...params: any[]) => any): void;
/**
* Get multiple value from highest level cache that has stored it.
* If some values are not found, the next highest cache is used
* until either all keys are found or all caches have been fetched.
* Accepts an unlimited number of keys.
* @param key - key to get (any number)
* @param [options] - to pass to underlying get function.
* @param cb - optional callback
*/
function mget(key: string, cb: (...params: any[]) => any): void;
function mget(key: string, options: any, cb: (...params: any[]) => any): void;
/**
* Delete value from all caches.
* @param [options] - to pass to underlying del function.
*/
function del(key: string, cb: (...params: any[]) => any): void;
function del(key: string, options: any, cb: (...params: any[]) => any): void;
/**
* Reset all caches.
*/
function reset(cb: (...params: any[]) => any): void;
}
declare module 'cache-manager' {
/**
* This is a very basic example of how you can implement your own Redis-based
* cache store with connection pooling.
*/
export var RedisPool: any;
/**
* Wrapper for lru-cache.
* NOTE: If you want to use a memory store, you want to write your own to have full
* control over its behavior. E.g., you might need the extra Promise overhead or
* you may want to clone objects a certain way before storing.
* @param args - Args passed to underlying lru-cache, plus additional optional args:
* @param [args.shouldCloneBeforeSet = true] - Whether to clone the data being stored.
* Default: true
* @param [args.usePromises = true] - Whether to enable the use of Promises. Default: true
*/
export function memoryStore(args: {
shouldCloneBeforeSet?: boolean;
usePromises?: boolean;
[x: string]: any;
}): void;
/**
* Store that do nothing.
* Can be used for development environment.
*/
export function noneStore(): void;
export * from 'cache-manager/caching';
export * from 'cache-manager/multiCaching';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment