{"name":"cache-manager","version":"6.3.2","description":"Cache Manager for Node.js","type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.js"}},"repository":{"type":"git","url":"git+https://github.com/jaredwray/cacheable.git","directory":"packages/cache-manager"},"keywords":["cache","caching","cache manager","node","node.js","in-memory cache","redis","memcached","multi-store cache","ttl","caching layer","cache abstraction","cache middleware","cache strategies","cache wrapper"],"authors":[{"name":"Jared Wray","email":"me@jaredwray.com"},{"name":"Tim Phan","email":"timphan.dev@gmail.com"}],"license":"MIT","dependencies":{"keyv":"^5.2.3"},"devDependencies":{"@faker-js/faker":"^9.3.0","@keyv/redis":"^4.2.0","@types/node":"^22.10.2","@vitest/coverage-v8":"^2.1.8","cache-manager-redis-yet":"^5.1.5","rimraf":"^6.0.1","tsup":"^8.3.5","typescript":"^5.7.2","vitest":"^2.1.8","xo":"^0.60.0","cacheable":"^1.8.6"},"xo":{"rules":{"@typescript-eslint/no-unsafe-call":"off","@typescript-eslint/no-unsafe-assignment":"off"}},"scripts":{"clean":"rimraf ./dist ./coverage ./node_modules","test":"xo --fix && vitest run --coverage","test:ci":"xo && vitest run","build":"rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean","prepublish":"pnpm run build"},"_id":"cache-manager@6.3.2","bugs":{"url":"https://github.com/jaredwray/cacheable/issues"},"homepage":"https://github.com/jaredwray/cacheable#readme","_integrity":"sha512-VmLouPUrvpm9dfwYB6OE7YVXDZ7BCfbt7hq10EHiBYaW9K9ZthK1bbjDQAtXGDK7d9u8t4G/7dMWSJOwN33msg==","_resolved":"/private/var/folders/q4/x95kq1ln6cd7rrnct9cby32r0000gn/T/9f025e644b7a0b7f74d1998ede875ed8/cache-manager-6.3.2.tgz","_from":"file:cache-manager-6.3.2.tgz","_nodeVersion":"22.12.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-VmLouPUrvpm9dfwYB6OE7YVXDZ7BCfbt7hq10EHiBYaW9K9ZthK1bbjDQAtXGDK7d9u8t4G/7dMWSJOwN33msg==","shasum":"49275f3f52da07c5f1ff212f706fde0e26f2580b","tarball":"http://registry-npm.ywwl.com/cache-manager/download/cache-manager-6.3.2.tgz","fileCount":7,"unpackedSize":42442,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBE6dG8ZEezAHxPEger1s/VkEdvwTAkcgTXbFwBKOv7dAiEAnAk06tgYjvmTbuizIKPyFu2iuDFz1QJY9Im1Wlt+XKI="}],"size":9117,"noattachment":false,"key":"/cache-manager/-/cache-manager-6.3.2.tgz"},"_npmUser":{"name":"jaredwray","email":"me@jaredwray.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/cache-manager_6.3.2_1735256042375_0.30947347541743686"},"_hasShrinkwrap":false,"readme":"[<img align=\"center\" src=\"https://cacheable.org/symbol.svg\" alt=\"Cacheable\" />](https://github.com/jaredwray/cacheable)\n\n# cache-manager\n[![codecov](https://codecov.io/gh/jaredwray/cacheable/graph/badge.svg?token=lWZ9OBQ7GM)](https://codecov.io/gh/jaredwray/cacheable)\n[![tests](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)\n[![npm](https://img.shields.io/npm/dm/cache-manager)](https://npmjs.com/package/cache-manager)\n[![npm](https://img.shields.io/npm/v/cache-manager)](https://npmjs.com/package/cache-manager)\n[![license](https://img.shields.io/github/license/jaredwray/cacheable)](https://github.com/jaredwray/cacheable/blob/main/LICENSE)\n\n# Simple and fast NodeJS caching module.\nA cache module for NodeJS that allows easy wrapping of functions in cache, tiered caches, and a consistent interface.\n- Made with Typescript and compatible with [ESModules](https://nodejs.org/docs/latest-v14.x/api/esm.html).\n- Easy way to wrap any function in cache, supports a mechanism to refresh expiring cache keys in background.\n- Tiered caches -- data gets stored in each cache and fetched from the highest priority cache(s) first.\n- `nonBlocking` option that optimizes how the system handles multiple stores.\n- Use with any [Keyv](https://keyv.org/) compatible storage adapter.\n- 100% test coverage via [vitest](https://github.com/vitest-dev/vitest).\n\nWe moved to using [Keyv](https://keyv.org/) which are more actively maintained and have a larger community.\n\nA special thanks to [Tim Phan](https://github.com/timphandev) who took `cache-manager` v5 and ported it to [Keyv](https://keyv.org/) which is the foundation of v6. 🎉 Another special thanks to [Doug Ayers](https://github.com/douglascayers) who wrote `promise-coalesce` which was used in v5 and now embedded in v6.\n\nIf you are looking for older documentation you can find it here:\n* [v5 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv5.md)\n* [v4 Documentation](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv4.md)\n\n## Table of Contents\n* [Installation](#installation)\n* [Quick start](#quick-start)\n* [Using `CacheableMemory` or `lru-cache` as storage adapter](#using-cacheablememory-or-lru-cache-as-storage-adapter)\n* [Options](#options)\n* [Methods](#methods)\n  * [.set](#set)\n  * [.mset](#mset)\n  * [.get](#get)\n  * [.mget](#mget)\n  * [.ttl](#ttl)\n  * [.del](#del)\n  * [.mdel](#mdel)\n  * [.clear](#clear)\n  * [.wrap](#wrap)\n* [Events](#events)\n  * [.set](#set)\n  * [.del](#del)\n  * [.clear](#clear)\n  * [.refresh](#refresh)\n* [Update on `redis` and `ioredis` Support](#update-on-redis-and-ioredis-support)\n* [Using Legacy Storage Adapters](#using-legacy-storage-adapters)\n* [Contribute](#contribute)\n* [License](#license)\n\n# Installation\n\n```sh\nnpm install cache-manager\n```\n\nBy default, everything is stored in memory; you can optionally also install a storage adapter; choose one from any of the storage adapters supported by Keyv:\n\n```sh\nnpm install @keyv/redis\nnpm install @keyv/memcache\nnpm install @keyv/mongo\nnpm install @keyv/sqlite\nnpm install @keyv/postgres\nnpm install @keyv/mysql\nnpm install @keyv/etcd\n```\nIn addition Keyv supports other storage adapters such as `lru-cache` and `CacheableMemory` from Cacheable (more examples below). Please read [Keyv document](https://keyv.org/docs/) for more information.\n\n# Quick start\n```typescript\nimport { Keyv } from 'keyv';\nimport { createCache } from 'cache-manager';\n\n// Memory store by default\nconst cache = createCache()\n\n// Single store which is in memory\nconst cache = createCache({\n  stores: [new Keyv()],\n})\n```\nHere is an example of doing layer 1 and layer 2 caching with the in-memory being `CacheableMemory` from Cacheable and the second layer being `@keyv/redis`:\n\n```ts\nimport { Keyv } from 'keyv';\nimport KeyvRedis from '@keyv/redis';\nimport { CacheableMemory } from 'cacheable';\nimport { createCache } from 'cache-manager';\n\n// Multiple stores\nconst cache = createCache({\n  stores: [\n    //  High performance in-memory cache with LRU and TTL\n    new Keyv({\n      store: new CacheableMemory({ ttl: 60000, lruSize: 5000 }),\n    }),\n\n    //  Redis Store\n    new Keyv({\n      store: new KeyvRedis('redis://user:pass@localhost:6379'),\n    }),\n  ],\n})\n```\n\nOnce it is created, you can use the cache object to set, get, delete, and wrap functions in cache.\n\n```ts\n\n// With default ttl and refreshThreshold\nconst cache = createCache({\n  ttl: 10000,\n  refreshThreshold: 3000,\n})\n\nawait cache.set('foo', 'bar')\n// => bar\n\nawait cache.get('foo')\n// => bar\n\nawait cache.del('foo')\n// => true\n\nawait cache.get('foo')\n// => null\n\nawait cache.wrap('key', () => 'value')\n// => value\n```\n\n# Using CacheableMemory or lru-cache as storage adapter\n\nBecause we are using [Keyv](https://keyv.org/), you can use any storage adapter that Keyv supports such as `lru-cache` or `CacheableMemory` from Cacheable. Below is an example of using `CacheableMemory`:\n\nIn this example we are using `CacheableMemory` from Cacheable which is a fast in-memory cache that supports LRU and and TTL expiration.\n\n```ts\nimport { createCache } from 'cache-manager';\nimport { Keyv } from 'keyv';\nimport { KeyvCacheableMemory } from 'cacheable';\n\nconst store = new KeyvCacheableMemory({ ttl: 60000, lruSize: 5000 });\nconst keyv = new Keyv({ store });\nconst cache = createCache({ stores: [keyv] });\n```\n\nHere is an example using `lru-cache`:\n\n```ts\nimport { createCache } from 'cache-manager';\nimport { Keyv } from 'keyv';\nimport { LRU } from 'lru-cache';\n\nconst keyv = new Keyv({ store: new LRU({ max: 5000, maxAge: 60000 }) });\nconst cache = createCache({ stores: [keyv] });\n```\n\n## Options\n- **stores**?: Keyv[]\n\n    List of Keyv instance. Please refer to the [Keyv document](https://keyv.org/docs/#3.-create-a-new-keyv-instance) for more information.\n- **ttl**?: number - Default time to live in milliseconds.\n\n    The time to live in milliseconds. This is the maximum amount of time that an item can be in the cache before it is removed.\n- **refreshThreshold**?: number - Default refreshThreshold in milliseconds.\n\n    If the remaining TTL is less than **refreshThreshold**, the system will update the value asynchronously in background.\n- **refreshAllStores**?: boolean - Default false\n\n    If set to true, the system will update the value of all stores when the refreshThreshold is met. Otherwise, it will only update from the top to the store that triggered the refresh.\n\n- **nonBlocking**?: boolean - Default false\n\n    If set to true, the system will not block when multiple stores are used. Here is how it affects the type of functions:\n    * `set and mset` - will not wait for all stores to finish.\n    * `get and mget` - will return the first (fastest) value found.\n    * `del and mdel` - will not wait for all stores to finish.\n    * `clear` - will not wait for all stores to finish.\n    * `wrap` - will do the same as `get` and `set` (return the first value found and not wait for all stores to finish).\n\n# Methods\n## set\n`set(key, value, [ttl]): Promise<value>`\n\nSets a key value pair. It is possible to define a ttl (in milliseconds). An error will be throw on any failed\n\n```ts\nawait cache.set('key-1', 'value 1')\n\n// expires after 5 seconds\nawait cache.set('key 2', 'value 2', 5000)\n```\nSee unit tests in [`test/set.test.ts`](./test/set.test.ts) for more information.\n\n## mset\n\n`mset(keys: [ { key, value, ttl } ]): Promise<true>`\n\nSets multiple key value pairs. It is possible to define a ttl (in milliseconds). An error will be throw on any failed\n\n```ts\nawait cache.mset([\n  { key: 'key-1', value: 'value 1' },\n  { key: 'key-2', value: 'value 2', ttl: 5000 },\n]);\n```\n\n## get\n`get(key): Promise<value>`\n\nGets a saved value from the cache. Returns a null if not found or expired. If the value was found it returns the value.\n\n```ts\nawait cache.set('key', 'value')\n\nawait cache.get('key')\n// => value\n\nawait cache.get('foo')\n// => null\n```\nSee unit tests in [`test/get.test.ts`](./test/get.test.ts) for more information.\n\n## mget\n\n`mget(keys: [key]): Promise<value[]>`\n\nGets multiple saved values from the cache. Returns a null if not found or expired. If the value was found it returns the value.\n\n```ts\nawait cache.mset([\n  { key: 'key-1', value: 'value 1' },\n  { key: 'key-2', value: 'value 2' },\n]);\n\nawait cache.mget(['key-1', 'key-2', 'key-3'])\n// => ['value 1', 'value 2', null]\n```\n\n## ttl\n`ttl(key): Promise<number | null>`\n\nGets the expiration time of a key in milliseconds. Returns a null if not found or expired.\n\n```ts\nawait cache.set('key', 'value', 1000); // expires after 5 seconds\n\nawait cache.ttl('key'); // => the expiration time in milliseconds\n\nawait cache.get('foo'); // => null\n```\nSee unit tests in [`test/ttl.test.ts`](./test/ttl.test.ts) for more information.\n\n## del\n`del(key): Promise<true>`\n\nDelete a key, an error will be throw on any failed.\n\n```ts\nawait cache.set('key', 'value')\n\nawait cache.get('key')\n// => value\n\nawait cache.del('key')\n\nawait cache.get('key')\n// => null\n```\nSee unit tests in [`test/del.test.ts`](./test/del.test.ts) for more information.\n\n## mdel\n\n`mdel(keys: [key]): Promise<true>`\n\nDelete multiple keys, an error will be throw on any failed.\n\n```ts\nawait cache.mset([\n  { key: 'key-1', value: 'value 1' },\n  { key: 'key-2', value: 'value 2' },\n]);\n\nawait cache.mdel(['key-1', 'key-2'])\n```\n\n## clear\n`clear(): Promise<true>`\n\nFlush all data, an error will be throw on any failed.\n\n```ts\nawait cache.set('key-1', 'value 1')\nawait cache.set('key-2', 'value 2')\n\nawait cache.get('key-1')\n// => value 1\nawait cache.get('key-2')\n// => value 2\n\nawait cache.clear()\n\nawait cache.get('key-1')\n// => null\nawait cache.get('key-2')\n// => null\n```\nSee unit tests in [`test/clear.test.ts`](./test/clear.test.ts) for more information.\n\n## wrap\n`wrap(key, fn: async () => value, [ttl], [refreshThreshold]): Promise<value>`\n\nWraps a function in cache. The first time the function is run, its results are stored in cache so subsequent calls retrieve from cache instead of calling the function.\n\nIf `refreshThreshold` is set and the remaining TTL is less than `refreshThreshold`, the system will update the value asynchronously. In the meantime, the system will return the old value until expiration.\n\n```typescript\nawait cache.wrap('key', () => 1, 5000, 3000)\n// call function then save the result to cache\n// =>  1\n\nawait cache.wrap('key', () => 2, 5000, 3000)\n// return data from cache, function will not be called again\n// => 1\n\n// wait 3 seconds\nawait sleep(3000)\n\nawait cache.wrap('key', () => 2, 5000, 3000)\n// return data from cache, call function in background and save the result to cache\n// =>  1\n\nawait cache.wrap('key', () => 3, 5000, 3000)\n// return data from cache, function will not be called\n// =>  2\n\nawait cache.wrap('error', () => {\n  throw new Error('failed')\n})\n// => error\n```\n**NOTES:**\n\n* The store that will be checked for refresh is the one where the key will be found first (highest priority).\n* If the threshold is low and the worker function is slow, the key may expire and you may encounter a racing condition with updating values.\n* If no `ttl` is set for the key, the refresh mechanism will not be triggered.\n\nSee unit tests in [`test/wrap.test.ts`](./test/wrap.test.ts) for more information.\n\n## disconnect\n\n`disconnect(key): Promise<void>`\n\nWill disconnect from the relevant store(s). It is highly recomended to use this when using a [Keyv](https://keyv.org/) storage adapter that requires a disconnect. For each storage adapter, the use case for when to use disconnect is different. An example is that `@keyv/redis` should be used only when you are done with the cache.\n\n```ts\nawait cache.disconnect();\n```\n\nSee unit tests in [`test/disconnect.test.ts`](./test/disconnect.test.ts) for more information.\n\n# Events\n## set\nFired when a key has been added or changed.\n\n```ts\ncache.on('set', ({ key, value, error }) => {\n\t// ... do something ...\n})\n```\n\n## del\nFired when a key has been removed manually.\n\n```ts\ncache.on('del', ({ key, error }) => {\n\t// ... do something ...\n})\n```\n\n## clear\nFired when the cache has been flushed.\n\n```ts\ncache.on('clear', (error) => {\n  if (error) {\n    // ... do something ...\n  }\n})\n```\n\n## refresh\nFired when the cache has been refreshed in the background.\n\n```ts\ncache.on('refresh', ({ key, value, error }) => {\n  if (error) {\n    // ... do something ...\n  }\n})\n```\n\nSee unit tests in [`test/events.test.ts`](./test/events.test.ts) for more information.\n\n# Update on redis and ioredis Support\n\nWe will not be supporting `cache-manager-ioredis-yet` or `cache-manager-redis-yet` in the future as we have moved to using `Keyv` as the storage adapter `@keyv/redis`.\n\n# Using Legacy Storage Adapters\n\nThere are many storage adapters built for `cache-manager` and because of that we wanted to provide a way to use them with `KeyvAdapter`. Below is an example of using `cache-manager-redis-yet`:\n\n```ts\nimport { createCache, KeyvAdapter } from 'cache-manager';\nimport { Keyv } from 'keyv';\nimport { redisStore } from 'cache-manager-redis-yet';\n\nconst adapter = new KeyvAdapter( await redisStore() );\nconst cache = createCache({\n  stores: [new Keyv({ store: adapter })],\n});\n```\n\nThis adapter will allow you to add in any storage adapter. If there are issues it needs to follow `CacheManagerStore` interface.\n\n# Contribute\n\nIf you would like to contribute to the project, please read how to contribute here [CONTRIBUTING.md](https://github.com/jaredwray/cacheable/blob/main/CONTRIBUTING.md).\n\n# License\n\n[MIT © Jared Wray ](./LICENSE)\n","publish_time":1735256042562,"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"_cnpm_publish_time":1735256042562,"dist-tags":{"latest":"7.2.9"}}