Skip to content

Commit 64a71c7

Browse files
committed
Update client-side cache and improve documentation
1 parent 3dcf682 commit 64a71c7

File tree

5 files changed

+124
-11
lines changed

5 files changed

+124
-11
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/client/lib/client/README-cache.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ Note: Client Side Caching is only supported with RESP3.
1111
### Anonymous Cache
1212

1313
```javascript
14-
const client = createClient({RESP: 3, clientSideCache: {ttl: 0, maxEntries: 0, lru: false}})
14+
const client = createClient({RESP: 3, clientSideCache: {ttl: 0, maxEntries: 0, evictPolicy: "FIFO"}})
1515
```
1616

1717
In this instance, the cache is opaque to the user, and they have no control over it.
1818

1919
### Controllable Cache
2020

2121
```javascript
22-
const ttl = 0, maxEntries = 0, lru = false;
23-
const cache = new BasicClientSideCache(ttl, maxEntries, lru);
22+
const cache = new BasicClientSideCache({
23+
ttl: 0,
24+
maxEntries: 0,
25+
evictPolicy: "FIFO"
26+
});
2427
const client = createClient({RESP: 3, clientSideCache: cache});
2528
```
2629

packages/client/lib/client/cache.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,31 @@ type CmdFunc = () => Promise<ReplyUnion>;
88

99
type EvictionPolicy = "LRU" | "FIFO"
1010

11+
/**
12+
* Configuration options for Client Side Cache
13+
*/
1114
export interface ClientSideCacheConfig {
15+
/**
16+
* Time-to-live in milliseconds for cached entries.
17+
* Use 0 for no expiration.
18+
* @default 0
19+
*/
1220
ttl?: number;
21+
22+
/**
23+
* Maximum number of entries to store in the cache.
24+
* Use 0 for unlimited entries.
25+
* @default 0
26+
*/
1327
maxEntries?: number;
14-
evictPolocy?: EvictionPolicy;
28+
29+
/**
30+
* Eviction policy to use when the cache reaches its capacity.
31+
* - "LRU" (Least Recently Used): Evicts least recently accessed entries first
32+
* - "FIFO" (First In First Out): Evicts oldest entries first
33+
* @default "LRU"
34+
*/
35+
evictPolicy?: EvictionPolicy;
1536
}
1637

1738
type CacheCreator = {
@@ -109,7 +130,7 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
109130
this.#keyToCacheKeySetMap = new Map<string, Set<string>>();
110131
this.ttl = config?.ttl ?? 0;
111132
this.maxEntries = config?.maxEntries ?? 0;
112-
this.lru = config?.evictPolocy !== "FIFO"
133+
this.lru = config?.evictPolicy !== "FIFO"
113134
}
114135

115136
/* logic of how caching works:

packages/client/lib/client/index.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,60 @@ export interface RedisClientOptions<
7878
*/
7979
pingInterval?: number;
8080
/**
81-
* TODO
81+
* Default command options to be applied to all commands executed through this client.
82+
*
83+
* These options can be overridden on a per-command basis when calling specific commands.
84+
*
85+
* @property {symbol} [chainId] - Identifier for chaining commands together
86+
* @property {boolean} [asap] - When true, the command is executed as soon as possible
87+
* @property {AbortSignal} [abortSignal] - AbortSignal to cancel the command
88+
* @property {TypeMapping} [typeMapping] - Custom type mappings between RESP and JavaScript types
89+
*
90+
* @example Setting default command options
91+
* ```
92+
* const client = createClient({
93+
* commandOptions: {
94+
* asap: true,
95+
* typeMapping: {
96+
* // Custom type mapping configuration
97+
* }
98+
* }
99+
* });
100+
* ```
82101
*/
83102
commandOptions?: CommandOptions<TYPE_MAPPING>;
84103
/**
85-
* TODO
104+
* Client Side Caching configuration.
105+
*
106+
* Enables Redis Servers and Clients to work together to cache results from commands
107+
* sent to a server. The server will notify the client when cached results are no longer valid.
108+
*
109+
* Note: Client Side Caching is only supported with RESP3.
110+
*
111+
* @example Anonymous cache configuration
112+
* ```
113+
* const client = createClient({
114+
* RESP: 3,
115+
* clientSideCache: {
116+
* ttl: 0,
117+
* maxEntries: 0,
118+
* evictPolicy: "LRU"
119+
* }
120+
* });
121+
* ```
122+
*
123+
* @example Using a controllable cache
124+
* ```
125+
* const cache = new BasicClientSideCache({
126+
* ttl: 0,
127+
* maxEntries: 0,
128+
* evictPolicy: "LRU"
129+
* });
130+
* const client = createClient({
131+
* RESP: 3,
132+
* clientSideCache: cache
133+
* });
134+
* ```
86135
*/
87136
clientSideCache?: ClientSideCacheProvider | ClientSideCacheConfig;
88137
}

packages/client/lib/client/pool.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,55 @@ export interface RedisPoolOptions {
2424
*/
2525
acquireTimeout: number;
2626
/**
27-
* TODO
27+
* The delay in milliseconds before a cleanup operation is performed on idle clients.
28+
*
29+
* After this delay, the pool will check if there are too many idle clients and destroy
30+
* excess ones to maintain optimal pool size.
2831
*/
2932
cleanupDelay: number;
3033
/**
31-
* TODO
34+
* Client Side Caching configuration for the pool.
35+
*
36+
* Enables Redis Servers and Clients to work together to cache results from commands
37+
* sent to a server. The server will notify the client when cached results are no longer valid.
38+
* In pooled mode, the cache is shared across all clients in the pool.
39+
*
40+
* Note: Client Side Caching is only supported with RESP3.
41+
*
42+
* @example Anonymous cache configuration
43+
* ```
44+
* const client = createClientPool({RESP: 3}, {
45+
* clientSideCache: {
46+
* ttl: 0,
47+
* maxEntries: 0,
48+
* evictPolicy: "LRU"
49+
* },
50+
* minimum: 5
51+
* });
52+
* ```
53+
*
54+
* @example Using a controllable cache
55+
* ```
56+
* const cache = new BasicPooledClientSideCache({
57+
* ttl: 0,
58+
* maxEntries: 0,
59+
* evictPolicy: "LRU"
60+
* });
61+
* const client = createClientPool({RESP: 3}, {
62+
* clientSideCache: cache,
63+
* minimum: 5
64+
* });
65+
* ```
3266
*/
3367
clientSideCache?: PooledClientSideCacheProvider | ClientSideCacheConfig;
3468
/**
35-
* TODO
69+
* Enable experimental support for RESP3 module commands.
70+
*
71+
* When enabled, allows the use of module commands that have been adapted
72+
* for the RESP3 protocol. This is an unstable feature and may change in
73+
* future versions.
74+
*
75+
* @default false
3676
*/
3777
unstableResp3Modules?: boolean;
3878
}

0 commit comments

Comments
 (0)