cache
Source Code | DocumentationCaches get and find results based on params. On mutating methods (create, update, patch, remove), affected cache entries are automatically invalidated. Works as a before, after, or around hook.
ts
import { cache } from 'feathers-utils/hooks';Example
ts
import { cache } from 'feathers-utils/hooks'
const myCache = new Map()
app.service('users').hooks({
around: {
all: [cache({ map: myCache, transformParams: (params) => ({ query: params.query }) })]
}
})Type declaration
Show Type Declarations
ts
type Cache = {
get: (key: string) => Promisable<any>
set: (key: string, value: any) => Promisable<any>
delete: (key: string) => Promisable<any>
clear: () => any
keys: () => Generator<string, void, unknown>
}
export type CacheOptions = {
/**
* The cache implementation to use. It should implement the methods `get`, `set`, `delete`, `clear`, and `keys`.
* This can be a Map, Redis client, or any other cache implementation.
*
* Use 'lru-cache' for an LRU cache implementation.
*/
map: Cache
/**
* The id field to use for caching. Defaults to `service.options.id` and if not found, then 'id'.
*/
id?: string
/**
* params are stringified for the key-value cache.
* There are params properties you don't want to include in the cache key.
* You can use this function to transform the params before they are stringified.
*/
transformParams: (params: Params) => Params
}
/**
* Caches `get` and `find` results based on `params`. On mutating methods (`create`, `update`,
* `patch`, `remove`), affected cache entries are automatically invalidated.
* Works as a `before`, `after`, or `around` hook.
*
* @example
* ```ts
*
*
* const myCache = new Map()
*
* app.service('users').hooks({
* around: {
* all: [cache({ map: myCache, transformParams: (params) => ({ query: params.query }) })]
* }
* })
* ```
*
* @see https://utils.feathersjs.com/hooks/cache.html
*/
export declare const cache: <H extends HookContext = HookContext>(
options: CacheOptions,
) => (context: H, next?: NextFunction) => Promise<HookContext>| Argument | Type | Description |
|---|---|---|
| options | CacheOptions |
| type | methods | multi |
|---|---|---|
| before, after | find, get, create, update, patch, remove | yes |
