Skip to content

Commit 0deb9aa

Browse files
authored
cacheable - feat: adding in readme around @keyv/redis and non blocking (#1211)
* cacheable - adding in readme around @keyv/redis and nonBlocking * toc
1 parent e6956c3 commit 0deb9aa

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

packages/cacheable/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* [TTL Propagation and Storage Tiering](#ttl-propagation-and-storage-tiering)
3333
* [Shorthand for Time to Live (ttl)](#shorthand-for-time-to-live-ttl)
3434
* [Non-Blocking Operations](#non-blocking-operations)
35+
* [Non-Blocking with @keyv/redis](#non-blocking-with-keyvredis)
3536
* [CacheSync - Distributed Updates](#cachesync---distributed-updates)
3637
* [Cacheable Options](#cacheable-options)
3738
* [Cacheable Statistics (Instance Only)](#cacheable-statistics-instance-only)
@@ -256,6 +257,29 @@ raws.forEach((entry, idx) => {
256257

257258
If you want your layer 2 (secondary) store to be non-blocking you can set the `nonBlocking` property to `true` in the options. This will make the secondary store non-blocking and will not wait for the secondary store to respond on `setting data`, `deleting data`, or `clearing data`. This is useful if you want to have a faster response time and not wait for the secondary store to respond.
258259

260+
# Non-Blocking with @keyv/redis
261+
262+
`@keyv/redis` is one of the most popular storage adapters used with `cacheable`. It provides a Redis-backed cache store that can be used as a secondary store. It is a bit complicated to setup as by default it causes hangs and blocking with its default configuration. To get past this you will need to configure the following:
263+
264+
Construct your own Redis client via the `createClient()` method from `@keyv/redis` with the following options:
265+
* Set `disableOfflineQueue` to `true`
266+
* Set `socket.reconnectStrategy` to `false`
267+
In the KeyvRedis options:
268+
* Set `throwOnConnectError` to `false`
269+
In the Cacheable options:
270+
* Set `nonBlocking` to `true`
271+
272+
We have also build a function to help with this called `createKeyvNonBlocking` inside the `@keyv/redis` package after version `4.6.0`. Here is an example of how to use it:
273+
274+
```javascript
275+
import { Cacheable } from 'cacheable';
276+
import { createKeyvNonBlocking } from '@keyv/redis';
277+
278+
const secondary = createKeyvNonBlocking('redis://user:pass@localhost:6379');
279+
280+
const cache = new Cacheable({ secondary, nonBlocking: true });
281+
```
282+
259283
# GetOrSet
260284

261285
The `getOrSet` method provides a convenient way to implement the cache-aside pattern. It attempts to retrieve a value

0 commit comments

Comments
 (0)