From 3adb22556da2071dce6c90a973e2021b2072531e Mon Sep 17 00:00:00 2001 From: Gianluca-Casagrande-Stiga <81612076+Gianluca-Casagrande-Stiga@users.noreply.github.com> Date: Sat, 7 Dec 2024 00:06:41 +0100 Subject: [PATCH] add LRUCacheOptions (#102) * add LRUCacheOptions * typo --- mqemitter-redis.js | 4 ++-- types/index.d.ts | 9 +++++++-- types/index.test-d.ts | 7 +++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/mqemitter-redis.js b/mqemitter-redis.js index 681477a..3502e9c 100644 --- a/mqemitter-redis.js +++ b/mqemitter-redis.js @@ -25,8 +25,8 @@ function MQEmitterRedis (opts) { this._topics = {} this._cache = new LRUCache({ - max: 10000, - ttl: 60 * 1000 // one minute + max: opts.maxLRUCache || 10000, // default: 10k + ttl: opts.ttlLRUCache || 60 * 1000 // default: one minute }) this.state = new EE() diff --git a/types/index.d.ts b/types/index.d.ts index a6a5a6c..2a913ff 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -10,10 +10,15 @@ export interface MQEmitterOptions { connectionString?: string; } +export interface LRUCacheOptions { + ttlLRUCache?: number;// Time to live for the LRU cache in milliseconds + maxLRUCache?: number;// Maximum number of items in the LRU cache +} + export type Message = Record & { topic: string }; export interface MQEmitterRedis extends MQEmitter { - new (options?: MQEmitterOptions & RedisOptions): MQEmitterRedis; + new (options?: MQEmitterOptions & RedisOptions & LRUCacheOptions): MQEmitterRedis; current: number; concurrent: number; on( @@ -31,7 +36,7 @@ export interface MQEmitterRedis extends MQEmitter { } declare function MQEmitterRedis( - options?: MQEmitterOptions & RedisOptions + options?: MQEmitterOptions & RedisOptions & LRUCacheOptions ): MQEmitterRedis; export default MQEmitterRedis; diff --git a/types/index.test-d.ts b/types/index.test-d.ts index f9d83d2..f2be195 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -29,6 +29,13 @@ expectType( }) ); +expectType( + mqEmitterRedis({ + maxLRUCache: 100, + ttlLRUCache: 10000, + }) +); + function listener(message: Message, done: () => void) {} expectType(mqEmitterRedis().on('topic', listener));