Papooch / nestjs-cls

A continuation-local storage (async context) module compatible with NestJS's dependency injection.
https://papooch.github.io/nestjs-cls/
MIT License
393 stars 23 forks source link

Additional ClsService methods #37

Closed agmoss closed 1 year ago

agmoss commented 2 years ago

Hi, @Papooch thanks for this great library!

As I use this library, I find myself equating the API to that of Redis or node-lru-cache. While I know nestjs-cls is not a general cache solution, I can see myself benefiting from further functions typically present in a cache manager. Here are some simple examples:

export interface ClsService {
    // Check if a key is in the cache
    has(key?: string | symbol): boolean
    // Returns the values of all specified keys
    mget<T>(...args: [string | symbol]): Promise<(T | null)[]>
    // Sets the given keys to their respective values
    mset<T>(args: [string, T][]): Promise<never[] | undefined>
    // perhaps others...
}

Many properties and methods typically found on cache strategies are not relevant (delete, clear, TTL, etc.).

I would be happy to open a PR with this functionality if you think it is an appropriate inclusion!

Cheers,

Andrew

Papooch commented 2 years ago

Hi there, I don't think it was the original intent of the library, but I can see that there could exist a use case for a request-scoped cache.

I am not opposed to adding the methods, if you can manage to make the typings work.

If anything, I guess the has method is worth adding.