From 241cb36feec95d2c53e838327aace99ff05c1936 Mon Sep 17 00:00:00 2001 From: "gianluca.casagrande" Date: Thu, 5 Dec 2024 13:47:46 +0100 Subject: [PATCH 1/2] add LRUCacheOptions --- mqemitter-redis.js | 4 ++-- types/index.d.ts | 9 +++++++-- types/index.test-d.ts | 9 ++++++++- 3 files changed, 17 insertions(+), 5 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..be61126 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -29,13 +29,20 @@ expectType( }) ); +expectType( + mqEmitterRedis({ + maxLRUCache: 100, + ttlLRUCache: 10000, + }) +); + function listener(message: Message, done: () => void) {} expectType(mqEmitterRedis().on('topic', listener)); expectType(mqEmitterRedis().removeListener('topic', listener)); -expectError(mqEmitterRedis().emit(null)); +expectError(mqEmitterRedis().emit(null)) expectType(mqEmitterRedis().emit({ topic: 'test', prop1: 'prop1' })); From 5cf5940442fc15802e075cd783363ebb9c4f65cf Mon Sep 17 00:00:00 2001 From: "gianluca.casagrande" Date: Thu, 5 Dec 2024 13:52:03 +0100 Subject: [PATCH 2/2] typo --- types/index.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.test-d.ts b/types/index.test-d.ts index be61126..f2be195 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -42,7 +42,7 @@ expectType(mqEmitterRedis().on('topic', listener)); expectType(mqEmitterRedis().removeListener('topic', listener)); -expectError(mqEmitterRedis().emit(null)) +expectError(mqEmitterRedis().emit(null)); expectType(mqEmitterRedis().emit({ topic: 'test', prop1: 'prop1' }));