Skip to content

Function: CacheKey()

ts
function CacheKey(key): MethodDecorator & ClassDecorator;

Defined in: packages/cache/src/decorators/cache-key.decorator.ts:59

Decorator that sets the caching key used to store/retrieve cached items.

This decorator is useful for WebSocket or Microservice-based applications where automatic URL-based key generation is not available.

Parameters

ParameterTypeDescription
keystring | CacheKeyFactoryA static string key or a factory function that generates a key.

Returns

MethodDecorator & ClassDecorator

A decorator function that sets the metadata.

Examples

Static key:

typescript
@CacheKey('events')
@Get('events')
async getEvents() {
  return this.eventsService.findAll();
}

Dynamic key with factory:

typescript
@CacheKey((ctx) => {
  const request = ctx.switchToHttp().getRequest();
  return `user:${request.params.id}`;
})
@Get(':id')
async getUser(@Param('id') id: string) {
  return this.usersService.findOne(id);
}

Released under the MIT License.