|
| 1 | +# Client Side Caching Support |
| 2 | + |
| 3 | +Client Side Caching enables Redis Servers and Clients to work together to enable a client to cache results from command sent to a server and be informed by the server when the cached result is no longer valid. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +node-redis supports two ways of instantiating client side caching support |
| 8 | + |
| 9 | +Note: Client Side Caching is only supported with RESP3. |
| 10 | + |
| 11 | +### Anonymous Cache |
| 12 | + |
| 13 | +```javascript |
| 14 | +const client = createClient({RESP: 3, clientSideCache: {ttl: 0, maxEntries: 0, lru: false}}) |
| 15 | +``` |
| 16 | + |
| 17 | +In this instance, the cache is opaque to the user, and they have no control over it. |
| 18 | + |
| 19 | +### Controllable Cache |
| 20 | + |
| 21 | +```javascript |
| 22 | +const ttl = 0, maxEntries = 0, lru = false; |
| 23 | +const cache = new BasicClientSideCache(ttl, maxEntries, lru); |
| 24 | +const client = createClient({RESP: 3, clientSideCache: cache}); |
| 25 | +``` |
| 26 | + |
| 27 | +In this instance, the user has full control over the cache, as they have access to the cache object. |
| 28 | + |
| 29 | +They can manually invalidate keys |
| 30 | + |
| 31 | +```javascript |
| 32 | +cache.invalidate(key); |
| 33 | +``` |
| 34 | + |
| 35 | +they can clear the entire cache |
| 36 | +g |
| 37 | +```javascript |
| 38 | +cache.clear(); |
| 39 | +``` |
| 40 | + |
| 41 | +as well as get cache metrics |
| 42 | + |
| 43 | +```typescript |
| 44 | +const hits: number = cache.cacheHits(); |
| 45 | +const misses: number = cache.cacheMisses(); |
| 46 | +``` |
| 47 | + |
| 48 | +## Pooled Caching |
| 49 | + |
| 50 | +Similar to individual clients, node-redis also supports caching for its pooled client object, with the cache being able to be instantiated in an anonymous manner or a controllable manner. |
| 51 | + |
| 52 | +### Anonymous Cache |
| 53 | + |
| 54 | +```javascript |
| 55 | +const client = createClientPool({RESP: 3}, {clientSideCache: {ttl: 0, maxEntries: 0, lru: false}, minimum: 8}); |
| 56 | +``` |
| 57 | + |
| 58 | +### Controllable Cache |
| 59 | + |
| 60 | +```javascript |
| 61 | +const ttl = 0, maxEntries = 0, lru = false; |
| 62 | +const cache = new BasicPooledClientSideCache(ttl, maxEntries, lru); |
| 63 | +const client = createClientPool({RESP: 3}, {clientSideCache: cache, minimum: 8}); |
| 64 | +``` |
0 commit comments