You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-07-31 05:44:24 +03:00
Client Side Caching (#2947)
* CSC POC ontop of Parser * add csc file that weren't merged after patch * address review comments * nits to try and fix github * last change from review * Update client-side cache and improve documentation * Add client side caching RESP3 validation * Add documentation for RESP and unstableResp3 options * Add comprehensive cache statistics The `CacheStats` class provides detailed metrics like hit/miss counts, load success/failure counts, total load time, and eviction counts. It also offers derived metrics such as hit/miss rates, load failure rate, and average load penalty. The design is inspired by Caffeine. `BasicClientSideCache` now uses a `StatsCounter` to accumulate these statistics, exposed via a new `stats()` method. The previous `cacheHits()` and `cacheMisses()` methods have been removed. A `recordStats` option (default: true) in `ClientSideCacheConfig` allows disabling statistics collection. --------- Co-authored-by: Shaya Potter <shaya@redislabs.com>
This commit is contained in:
97
docs/v5.md
97
docs/v5.md
@ -89,3 +89,100 @@ await multi.exec(); // Array<ReplyUnion>
|
||||
await multi.exec<'typed'>(); // [string]
|
||||
await multi.execTyped(); // [string]
|
||||
```
|
||||
|
||||
# Client Side Caching
|
||||
|
||||
Node Redis v5 adds support for [Client Side Caching](https://redis.io/docs/manual/client-side-caching/), which enables clients to cache query results locally. The server will notify the client when cached results are no longer valid.
|
||||
|
||||
Client Side Caching is only supported with RESP3.
|
||||
|
||||
## Usage
|
||||
|
||||
There are two ways to implement client side caching:
|
||||
|
||||
### Anonymous Cache
|
||||
|
||||
```javascript
|
||||
const client = createClient({
|
||||
RESP: 3,
|
||||
clientSideCache: {
|
||||
ttl: 0, // Time-to-live in milliseconds (0 = no expiration)
|
||||
maxEntries: 0, // Maximum entries to store (0 = unlimited)
|
||||
evictPolicy: "LRU" // Eviction policy: "LRU" or "FIFO"
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
In this instance, the cache is managed internally by the client.
|
||||
|
||||
### Controllable Cache
|
||||
|
||||
```javascript
|
||||
import { BasicClientSideCache } from 'redis';
|
||||
|
||||
const cache = new BasicClientSideCache({
|
||||
ttl: 0,
|
||||
maxEntries: 0,
|
||||
evictPolicy: "LRU"
|
||||
});
|
||||
|
||||
const client = createClient({
|
||||
RESP: 3,
|
||||
clientSideCache: cache
|
||||
});
|
||||
```
|
||||
|
||||
With this approach, you have direct access to the cache object for more control:
|
||||
|
||||
```javascript
|
||||
// Manually invalidate keys
|
||||
cache.invalidate(key);
|
||||
|
||||
// Clear the entire cache
|
||||
cache.clear();
|
||||
|
||||
// Get cache metrics
|
||||
// `cache.stats()` returns a `CacheStats` object with comprehensive statistics.
|
||||
const statistics = cache.stats();
|
||||
|
||||
// Key metrics:
|
||||
const hits = statistics.hitCount; // Number of cache hits
|
||||
const misses = statistics.missCount; // Number of cache misses
|
||||
const hitRate = statistics.hitRate(); // Cache hit rate (0.0 to 1.0)
|
||||
|
||||
// Many other metrics are available on the `statistics` object, e.g.:
|
||||
// statistics.missRate(), statistics.loadSuccessCount,
|
||||
// statistics.averageLoadPenalty(), statistics.requestCount()
|
||||
```
|
||||
|
||||
## Pooled Caching
|
||||
|
||||
Client side caching also works with client pools. For pooled clients, the cache is shared across all clients in the pool:
|
||||
|
||||
```javascript
|
||||
const client = createClientPool({RESP: 3}, {
|
||||
clientSideCache: {
|
||||
ttl: 0,
|
||||
maxEntries: 0,
|
||||
evictPolicy: "LRU"
|
||||
},
|
||||
minimum: 5
|
||||
});
|
||||
```
|
||||
|
||||
For a controllable pooled cache:
|
||||
|
||||
```javascript
|
||||
import { BasicPooledClientSideCache } from 'redis';
|
||||
|
||||
const cache = new BasicPooledClientSideCache({
|
||||
ttl: 0,
|
||||
maxEntries: 0,
|
||||
evictPolicy: "LRU"
|
||||
});
|
||||
|
||||
const client = createClientPool({RESP: 3}, {
|
||||
clientSideCache: cache,
|
||||
minimum: 5
|
||||
});
|
||||
```
|
||||
|
Reference in New Issue
Block a user