Skip to content

Commit

Permalink
add LRUCacheOptions (#102)
Browse files Browse the repository at this point in the history
* add LRUCacheOptions

* typo
  • Loading branch information
Gianluca-Casagrande-Stiga authored Dec 6, 2024
1 parent a09ef9f commit 3adb225
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions mqemitter-redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> & { topic: string };

export interface MQEmitterRedis extends MQEmitter {
new (options?: MQEmitterOptions & RedisOptions): MQEmitterRedis;
new (options?: MQEmitterOptions & RedisOptions & LRUCacheOptions): MQEmitterRedis;
current: number;
concurrent: number;
on(
Expand All @@ -31,7 +36,7 @@ export interface MQEmitterRedis extends MQEmitter {
}

declare function MQEmitterRedis(
options?: MQEmitterOptions & RedisOptions
options?: MQEmitterOptions & RedisOptions & LRUCacheOptions
): MQEmitterRedis;

export default MQEmitterRedis;
7 changes: 7 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ expectType<MQEmitterRedis>(
})
);

expectType<MQEmitterRedis>(
mqEmitterRedis({
maxLRUCache: 100,
ttlLRUCache: 10000,
})
);

function listener(message: Message, done: () => void) {}

expectType<MQEmitterRedis>(mqEmitterRedis().on('topic', listener));
Expand Down

0 comments on commit 3adb225

Please sign in to comment.