Skip to content

Commit 9e727ce

Browse files
committed
add emitInvalidate option
1 parent 6ad4c68 commit 9e727ce

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/client/lib/client/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ export interface RedisClientOptions<
144144
* Tag to append to library name that is sent to the Redis server
145145
*/
146146
clientInfoTag?: string;
147+
/**
148+
* When set to true, client tracking is turned on and the client emits `invalidate` events when it receives invalidation messages from the redis server.
149+
* Mutually exclusive with `clientSideCache` option.
150+
*/
151+
emitInvalidate?: boolean;
147152
}
148153

149154
export type WithCommands<
@@ -465,15 +470,20 @@ export default class RedisClient<
465470
this.#clientSideCache = new BasicClientSideCache(cscConfig);
466471
}
467472
this.#queue.setInvalidateCallback(this.#clientSideCache.invalidate.bind(this.#clientSideCache));
473+
} else if (options?.emitInvalidate) {
474+
this.#queue.setInvalidateCallback((key) => this.emit('invalidate', key));
468475
}
469476
}
470477

471478
#validateOptions(options?: RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>) {
472479
if (options?.clientSideCache && options?.RESP !== 3) {
473480
throw new Error('Client Side Caching is only supported with RESP3');
474481
}
475-
482+
if (options?.clientSideCache && options?.emitInvalidate) {
483+
throw new Error('emitInvalidate is not supported (or necessary) when clientSideCache is enabled');
484+
}
476485
}
486+
477487
#initiateOptions(options?: RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>): RedisClientOptions<M, F, S, RESP, TYPE_MAPPING> | undefined {
478488

479489
// Convert username/password to credentialsProvider if no credentialsProvider is already in place
@@ -674,11 +684,14 @@ export default class RedisClient<
674684
}
675685
});
676686
}
677-
687+
678688
if (this.#clientSideCache) {
679689
commands.push({cmd: this.#clientSideCache.trackingOn()});
680690
}
681691

692+
if (this.#options?.emitInvalidate) {
693+
commands.push({cmd: ['CLIENT', 'TRACKING', 'ON']});
694+
}
682695
return commands;
683696
}
684697

0 commit comments

Comments
 (0)