nestjs / cache-manager

Cache manager module for Nest framework (node.js) 🗃
MIT License
117 stars 22 forks source link

Dynamic cache key #309

Closed CSenshi closed 6 months ago

CSenshi commented 6 months ago

Is there an existing issue that is already proposing this?

Is your feature request related to a problem? Please describe it

Any time you want to cache key based on request you need to manually use cacheManager.

For example I have jwt token which is passed in the header and it has some value I need to use in cache key. For this I need to manually create cache object

Describe the solution you'd like

It would be nice @Cache to also accept callback

Example:

@Cache((ctx: ExecutionContext) => {
  const request = ctx.switchToHttp().getRequest();
  return request.user?.groupId;
})

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

Ease of use

kamilmysliwiec commented 6 months ago

Declare a class that inherits from CacheInterceptor and provides a custom trackBy method https://docs.nestjs.com/techniques/caching#adjust-tracking