These libraries provide solutions for storing and retrieving data efficiently to improve application performance. lru-cache and quick-lru offer in-memory storage with Least Recently Used eviction policies, suitable for simple key-value needs. cache-manager provides a wrapper that supports multiple storage backends (memory, Redis, etc.) and stacking. cacheable-request focuses specifically on caching HTTP requests based on standard HTTP headers. memory-cache is a simpler in-memory option but has fallen out of favor compared to more actively maintained alternatives.
Caching is essential for building fast applications. It reduces database load, speeds up API responses, and improves user experience. The JavaScript ecosystem offers several tools for this, but they solve different problems. Some focus on HTTP requests, others on in-memory data structures, and some on abstracting storage backends. Let's compare cache-manager, cacheable-request, lru-cache, memory-cache, and quick-lru to help you pick the right tool.
The first decision is whether you need a direct data structure or an abstraction layer.
lru-cache provides a direct implementation of a Least Recently Used map. You interact with the data structure itself. It is fast and has no dependencies.
// lru-cache: Direct usage
import { LRUCache } from 'lru-cache';
const cache = new LRUCache({ max: 500 });
cache.set('key', 'value');
const value = cache.get('key');
quick-lru is similar but focuses on minimalism. It also gives you a direct Map-like interface with LRU eviction.
// quick-lru: Direct usage
import QuickLRU from 'quick-lru';
const lru = new QuickLRU({ maxSize: 500 });
lru.set('key', 'value');
const value = lru.get('key');
cache-manager wraps storage engines. You don't talk to the memory directly; you talk to the manager, which talks to the store (memory, Redis, etc.).
// cache-manager: Abstraction layer
import cacheManager from 'cache-manager';
const cache = await cacheManager.caching({ store: 'memory', max: 500 });
await cache.set('key', 'value');
const value = await cache.get('key');
memory-cache is a direct in-memory store but uses a simpler, older API style compared to modern ES6 classes.
// memory-cache: Direct usage
import Cache from 'memory-cache';
const cache = new Cache();
cache.put('key', 'value');
const value = cache.get('key');
cacheable-request is different. It wraps HTTP requests, not just data. It caches the response of a network call.
// cacheable-request: HTTP wrapper
import CacheableRequest from 'cacheable-request';
const cacheableRequest = new CacheableRequest(request);
const emit = cacheableRequest.get('https://api.example.com/data');
emit.on('response', response => { /* handle response */ });
Most caches need to expire data. The way they handle this varies significantly.
lru-cache supports TTL per item or globally. It automatically removes stale items on access or via a background process in newer versions.
// lru-cache: TTL support
const cache = new LRUCache({
max: 500,
ttl: 1000 * 60 * 5 // 5 minutes
});
cache.set('key', 'value');
cache-manager handles TTL through its store configuration. When setting a value, you pass the time in seconds.
// cache-manager: TTL support
await cache.set('key', 'value', { ttl: 300 }); // 5 minutes
memory-cache allows you to pass a timeout in milliseconds when putting a value.
// memory-cache: TTL support
cache.put('key', 'value', 300 * 1000); // 5 minutes in ms
quick-lru does not support TTL natively. It only evicts based on size (maxSize). You would need to build time-based expiration yourself.
// quick-lru: No native TTL
// You must store timestamps manually
const lru = new QuickLRU({ maxSize: 500 });
lru.set('key', { value: 'data', timestamp: Date.now() });
// Custom logic needed to check timestamp on get()
cacheable-request relies on HTTP headers. It respects Cache-Control, Expires, and ETag from the server response.
// cacheable-request: HTTP Header TTL
// Automatically parses 'Cache-Control: max-age=300' from server
const emit = cacheableRequest.get('https://api.example.com/data');
When the cache is full, what gets removed?
lru-cache strictly follows Least Recently Used. The item you haven't touched in the longest time goes first.
// lru-cache: Strict LRU
const cache = new LRUCache({ max: 2 });
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3); // 'a' is evicted
console.log(cache.has('a')); // false
quick-lru also uses LRU eviction but is optimized for speed and simplicity.
// quick-lru: Strict LRU
const lru = new QuickLRU({ maxSize: 2 });
lru.set('a', 1);
lru.set('b', 2);
lru.set('c', 3); // 'a' is evicted
console.log(lru.has('a')); // false
memory-cache uses expiration primarily. While it has a max size, its main focus is time-based expiration rather than strict access-order eviction.
// memory-cache: Expiration focus
cache.put('a', 1, 1000);
cache.put('b', 2, 1000);
// Eviction happens based on checkInterval and timeouts
cache-manager depends on the underlying store. The memory store uses LRU, but a Redis store might use different strategies configured on the Redis side.
// cache-manager: Store dependent
// Memory store uses LRU, Redis uses Redis policies
const cache = await cacheManager.caching({ store: 'memory', max: 2 });
cacheable-request does not evict based on LRU in the traditional sense. It stores responses on disk or memory based on HTTP validity.
// cacheable-request: HTTP validity
// Stores responses until HTTP headers say they are stale
Choosing a library also means choosing a maintainer. Some of these packages are older and less active.
lru-cache is actively maintained and widely used in the Node.js core ecosystem. It receives regular updates for performance and security.
cache-manager is actively maintained. It is the go-to for multi-store caching in Node.js servers.
cacheable-request is maintained and commonly used with the got HTTP client. It is stable for HTTP caching needs.
quick-lru is stable and maintained by a well-known open-source developer. It is lightweight and reliable.
memory-cache shows signs of age. It has not seen significant updates in a long time. It is often flagged as a legacy option.
β οΈ Warning:
memory-cacheis not recommended for new projects. It lacks the modern features and active security patches oflru-cache.
You are building a Node.js API. You want to cache database query results in memory to reduce load.
lru-cache// lru-cache: Backend data caching
const queryCache = new LRUCache({ max: 1000, ttl: 60000 });
async function getUser(id) {
if (queryCache.has(id)) return queryCache.get(id);
const user = await db.query(id);
queryCache.set(id, user);
return user;
}
You are scraping websites. You want to avoid hitting the same URL twice within an hour.
cacheable-request// cacheable-request: HTTP scraping
const cacheableRequest = new CacheableRequest(request);
const emit = cacheableRequest.get('https://target.com/page');
You have a high-traffic app. You want fast local memory cache backed by a shared Redis cache.
cache-manager// cache-manager: Stacking stores
const multiCache = await cacheManager.multiCaching([memoryCache, redisCache]);
await multiCache.set('key', 'value');
You need a small cache in a browser extension or a small utility script without heavy dependencies.
quick-lru// quick-lru: Frontend utility
const lru = new QuickLRU({ maxSize: 100 });
lru.set('config', settings);
| Feature | lru-cache | quick-lru | cache-manager | memory-cache | cacheable-request |
|---|---|---|---|---|---|
| Primary Use | In-memory data | In-memory data | Multi-store wrapper | In-memory data | HTTP requests |
| Eviction | LRU | LRU | Store-dependent | Expiration/Size | HTTP Headers |
| TTL Support | β Native | β Manual | β Native | β Native | β HTTP Headers |
| Dependencies | None | None | Few | None | Few |
| Status | Active | Active | Active | Legacy | Active |
For most Node.js applications needing in-memory caching, lru-cache is the standard choice. It balances features, performance, and maintenance.
If you need to cache HTTP calls specifically, cacheable-request saves you from reinventing the wheel for header parsing.
If you need to abstract over Redis and Memory together, cache-manager is the only one on this list that handles that architecture.
Avoid memory-cache in new work. Use quick-lru when you need something smaller and simpler than lru-cache without TTL needs.
Choose cache-manager if you need a unified interface for multiple caching backends like Redis or memory, or if you require stacking caches (e.g., memory + Redis). It is ideal for server-side applications where flexibility and abstraction over the storage layer are critical.
Choose cacheable-request if your primary goal is to cache HTTP GET requests automatically based on standard HTTP headers. It integrates well with the got HTTP client and is best for reducing network overhead in data-fetching layers.
Choose lru-cache if you need a robust, feature-rich in-memory cache with strict LRU eviction, TTL support, and size limits. It is the industry standard for pure JavaScript in-memory caching in Node.js environments.
Avoid memory-cache for new projects as it is less actively maintained and lacks features found in modern alternatives. Only consider it for legacy systems where it is already deeply integrated and changing it poses too much risk.
Choose quick-lru if you need a minimal, dependency-free LRU cache with a simple API and no external dependencies. It is perfect for lightweight frontend or backend tasks where you just need basic max-size eviction without extra features.
A cache module for NodeJS that allows easy wrapping of functions in cache, tiered caches, and a consistent interface.
nonBlocking option that optimizes how the system handles multiple stores.We moved to using Keyv which are more actively maintained and have a larger community.
A special thanks to Tim Phan who took cache-manager v5 and ported it to Keyv which is the foundation of v6. π Another special thanks to Doug Ayers who wrote promise-coalesce which was used in v5 and now embedded in v6.
v7 has only one breaking change which is changing the return type from null to undefined when there is no data to return. This is to align with the Keyv API and to make it more consistent with the rest of the methods. Below is an example of how to migrate from v6 to v7:
import { createCache } from 'cache-manager';
const cache = createCache();
const result = await cache.get('key');
// result will be undefined if the key is not found or expired
console.log(result); // undefined
v6 is a major update and has breaking changes primarily around the storage adapters. We have moved to using Keyv which are more actively maintained and have a larger community. Below are the changes you need to make to migrate from v5 to v6. In v5 the memoryStore was used to create a memory store, in v6 you can use any storage adapter that Keyv supports. Below is an example of how to migrate from v5 to v6:
import { createCache, memoryStore } from 'cache-manager';
// Create memory cache synchronously
const memoryCache = createCache(memoryStore({
max: 100,
ttl: 10 * 1000 /*milliseconds*/,
}));
In v6 you can use any storage adapter that Keyv supports. Below is an example of using the in memory store with Keyv:
import { createCache } from 'cache-manager';
const cache = createCache();
If you would like to do multiple stores you can do the following:
import { createCache } from 'cache-manager';
import { createKeyv } from 'cacheable';
import { createKeyv as createKeyvRedis } from '@keyv/redis';
const memoryStore = createKeyv();
const redisStore = createKeyvRedis('redis://user:pass@localhost:6379');
const cache = createCache({
stores: [memoryStore, redisStore],
});
When doing in memory caching and getting errors on symbol or if the object is coming back wrong like on Uint8Array you will want to set the serialization and deserialization options in Keyv to undefined as it will try to do json serialization.
import { createCache } from "cache-manager";
import { Keyv } from "keyv";
const keyv = new Keyv();
keyv.serialize = undefined;
keyv.deserialize = undefined;
const memoryCache = createCache({
stores: [keyv],
});
The other option is to set the serialization to something that is not JSON.stringify. You can read more about it here: https://keyv.org/docs/keyv/#custom-serializers
If you would like a more robust in memory storage adapter you can use CacheableMemory from Cacheable. Below is an example of how to migrate from v5 to v6 using CacheableMemory:
import { createCache } from 'cache-manager';
import { createKeyv } from 'cacheable';
const cache = createCache({
stores: [createKeyv({ ttl: 60000, lruSize: 5000 })],
});
To learn more about CacheableMemory please visit: http://cacheable.org/docs/cacheable/#cacheablememory---in-memory-cache
If you are still wanting to use the legacy storage adapters you can use the KeyvAdapter to wrap the storage adapter. Below is an example of how to migrate from v5 to v6 using cache-manager-redis-yet by going to Using Legacy Storage Adapters.
If you are looking for older documentation you can find it here:
CacheableMemory or lru-cache as storage adapterredis and ioredis Supportnpm install cache-manager
By 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:
npm install @keyv/redis
npm install @keyv/memcache
npm install @keyv/mongo
npm install @keyv/sqlite
npm install @keyv/postgres
npm install @keyv/mysql
npm install @keyv/etcd
In addition Keyv supports other storage adapters such as lru-cache and CacheableMemory from Cacheable (more examples below). Please read Keyv document for more information.
import { Keyv } from 'keyv';
import { createCache } from 'cache-manager';
// Memory store by default
const cache = createCache()
// Single store which is in memory
const cache = createCache({
stores: [new Keyv()],
})
Here 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:
import { Keyv } from 'keyv';
import KeyvRedis from '@keyv/redis';
import { CacheableMemory } from 'cacheable';
import { createCache } from 'cache-manager';
// Multiple stores
const cache = createCache({
stores: [
// High performance in-memory cache with LRU and TTL
new Keyv({
store: new CacheableMemory({ ttl: 60000, lruSize: 5000 }),
}),
// Redis Store
new Keyv({
store: new KeyvRedis('redis://user:pass@localhost:6379'),
}),
],
})
Once it is created, you can use the cache object to set, get, delete, and wrap functions in cache.
// With default ttl and refreshThreshold
const cache = createCache({
ttl: 10000,
refreshThreshold: 3000,
})
await cache.set('foo', 'bar')
// => bar
await cache.get('foo')
// => bar
await cache.del('foo')
// => true
await cache.get('foo')
// => null
await cache.wrap('key', () => 'value')
// => value
Because we are using Keyv, you can use any storage adapter that Keyv supports such as lru-cache or CacheableMemory from Cacheable. Below is an example of using CacheableMemory:
In this example we are using CacheableMemory from Cacheable which is a fast in-memory cache that supports LRU and and TTL expiration.
import { createCache } from 'cache-manager';
import { Keyv } from 'keyv';
import { KeyvCacheableMemory } from 'cacheable';
const store = new KeyvCacheableMemory({ ttl: 60000, lruSize: 5000 });
const keyv = new Keyv({ store });
const cache = createCache({ stores: [keyv] });
Here is an example using lru-cache:
import { createCache } from 'cache-manager';
import { Keyv } from 'keyv';
import { LRU } from 'lru-cache';
const keyv = new Keyv({ store: new LRU({ max: 5000, maxAge: 60000 }) });
const cache = createCache({ stores: [keyv] });
stores?: Keyv[]
List of Keyv instance. Please refer to the Keyv document for more information.
ttl?: number - Default time to live in milliseconds.
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.
refreshThreshold?: number | (value:T) => number - Default refreshThreshold in milliseconds. You can also provide a function that will return the refreshThreshold based on the value.
If the remaining TTL is less than refreshThreshold, the system will update the value asynchronously in background.
refreshAllStores?: boolean - Default false
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.
nonBlocking?: boolean - Default false
If set to true, the system will not block when multiple stores are used. Here is how it affects the type of functions:
set and mset - will not wait for all stores to finish.get and mget - will return the first (fastest) value found.del and mdel - will not wait for all stores to finish.clear - will not wait for all stores to finish.wrap - will do the same as get and set (return the first value found and not wait for all stores to finish).cacheId?: string - Defaults to random string
Unique identifier for the cache instance. This is primarily used to not have conflicts when using wrap with multiple cache instances.
set(key, value, [ttl]): Promise<value>
Sets a key value pair. It is possible to define a ttl (in milliseconds). An error will be throw on any failed
await cache.set('key-1', 'value 1')
// expires after 5 seconds
await cache.set('key 2', 'value 2', 5000)
See unit tests in test/set.test.ts for more information.
mset(keys: [ { key, value, ttl } ]): Promise<true>
Sets multiple key value pairs. It is possible to define a ttl (in milliseconds). An error will be throw on any failed
await cache.mset([
{ key: 'key-1', value: 'value 1' },
{ key: 'key-2', value: 'value 2', ttl: 5000 },
]);
get(key): Promise<value>
Gets a saved value from the cache. Returns a null if not found or expired. If the value was found it returns the value.
await cache.set('key', 'value')
await cache.get('key')
// => value
await cache.get('foo')
// => null
See unit tests in test/get.test.ts for more information.
mget(keys: [key]): Promise<value[]>
Gets multiple saved values from the cache. Returns a null if not found or expired. If the value was found it returns the value.
await cache.mset([
{ key: 'key-1', value: 'value 1' },
{ key: 'key-2', value: 'value 2' },
]);
await cache.mget(['key-1', 'key-2', 'key-3'])
// => ['value 1', 'value 2', null]
ttl(key): Promise<number | null>
Gets the expiration time of a key in milliseconds. Returns a null if not found or expired.
await cache.set('key', 'value', 1000); // expires after 1 second
await cache.ttl('key'); // => the expiration time in milliseconds
await cache.get('foo'); // => null
See unit tests in test/ttl.test.ts for more information.
del(key): Promise<true>
Delete a key, an error will be throw on any failed.
await cache.set('key', 'value')
await cache.get('key')
// => value
await cache.del('key')
await cache.get('key')
// => null
See unit tests in test/del.test.ts for more information.
mdel(keys: [key]): Promise<true>
Delete multiple keys, an error will be throw on any failed.
await cache.mset([
{ key: 'key-1', value: 'value 1' },
{ key: 'key-2', value: 'value 2' },
]);
await cache.mdel(['key-1', 'key-2'])
clear(): Promise<true>
Flush all data, an error will be throw on any failed.
await cache.set('key-1', 'value 1')
await cache.set('key-2', 'value 2')
await cache.get('key-1')
// => value 1
await cache.get('key-2')
// => value 2
await cache.clear()
await cache.get('key-1')
// => null
await cache.get('key-2')
// => null
See unit tests in test/clear.test.ts for more information.
wrap(key, fn: async () => value, [ttl], [refreshThreshold]): Promise<value>
Alternatively, with optional parameters as options object supporting a raw parameter:
wrap(key, fn: async () => value, { ttl?: number, refreshThreshold?: number, raw?: true }): Promise<value>
Wraps 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.
If 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. You can also provide a function that will return the refreshThreshold based on the value (value:T) => number.
If the object format for the optional parameters is used, an additional raw parameter can be applied, changing the function return type to raw data including expiration timestamp as { value: [data], expires: [timestamp] }.
await cache.wrap('key', () => 1, 5000, 3000)
// call function then save the result to cache
// => 1
await cache.wrap('key', () => 2, 5000, 3000)
// return data from cache, function will not be called again
// => 1
await cache.wrap('key', () => 2, { ttl: 5000, refreshThreshold: 3000, raw: true })
// returns raw data including expiration timestamp
// => { value: 1, expires: [timestamp] }
// wait 3 seconds
await sleep(3000)
await cache.wrap('key', () => 2, 5000, 3000)
// return data from cache, call function in background and save the result to cache
// => 1
await cache.wrap('key', () => 3, 5000, 3000)
// return data from cache, function will not be called
// => 2
await cache.wrap('key', () => 4, 5000, () => 3000);
// return data from cache, function will not be called
// => 4
await cache.wrap('error', () => {
throw new Error('failed')
})
// => error
NOTES:
ttl is set for the key, the refresh mechanism will not be triggered.See unit tests in test/wrap.test.ts for more information.
disconnect(): Promise<void>
Will disconnect from the relevant store(s). It is highly recommended to use this when using a Keyv 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.
await cache.disconnect();
See unit tests in test/disconnect.test.ts for more information.
cacheId(): string
Returns cache instance id. This is primarily used to not have conflicts when using wrap with multiple cache instances.
stores(): Keyv[]
Returns the list of Keyv instances. This can be used to get the list of stores and then use the Keyv API to interact with the store directly.
const cache = createCache({cacheId: 'my-cache-id'});
cache.cacheId(); // => 'my-cache-id'
See unit tests in test/cache-id.test.ts for more information.
Fired when a key has been added or changed.
cache.on('set', ({ key, value, error }) => {
// ... do something ...
})
Fired when a key has been removed manually.
cache.on('del', ({ key, error }) => {
// ... do something ...
})
Fired when the cache has been flushed.
cache.on('clear', (error) => {
if (error) {
// ... do something ...
}
})
Fired when the cache has been refreshed in the background.
cache.on('refresh', ({ key, value, error }) => {
if (error) {
// ... do something ...
}
})
See unit tests in test/events.test.ts for more information.
You can use the stores method to get the list of stores and then use the Keyv API to interact with the store directly. Below is an example of iterating over all stores and getting all keys:
import Keyv from 'keyv';
import { createKeyv } from '@keyv/redis';
import { createCache } from 'cache-manager';
const keyv = new Keyv();
const keyvRedis = createKeyv('redis://user:pass@localhost:6379');
const cache = createCache({
stores: [keyv, keyvRedis],
});
// add some data
await cache.set('key-1', 'value 1');
await cache.set('key-2', 'value 2');
// get the store you want to iterate over. In this example we are using the second store (redis)
const store = cache.stores[1];
if(store?.iterator) {
for await (const [key, value] of store.iterator({})) {
console.log(key, value);
}
}
WARNING: Be careful when using iterator as it can cause major performance issues with the amount of data being retrieved. Also, Not all storage adapters support iterator so you may need to check the documentation for the storage adapter you are using.
We 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.
There 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:
import { createCache, KeyvAdapter } from 'cache-manager';
import { Keyv } from 'keyv';
import { redisStore } from 'cache-manager-redis-yet';
const adapter = new KeyvAdapter( await redisStore() );
const keyv = new Keyv({ store: adapter });
const cache = createCache({ stores: [keyv]});
This adapter will allow you to add in any storage adapter. If there are issues it needs to follow CacheManagerStore interface.
If you would like to contribute to the project, please read how to contribute here CONTRIBUTING.md.