2.0.0 • Published 10 months ago

nestjs-cache-dependency v2.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

nestjs-cache-dependency

Description

Cache dependency for Nestjs.

A cache data item will associate with one or multiple dependency keys. By invalidating a dependency key, all cache data items that are associated with it will be invalidated either.

It is useful in case we have a lot of relevant data such as a list with pagination or user data.

Installation

$ npm i --save nestjs-cache-dependency

Usage

Register CacheDependencyModule:

import { CacheDependencyModule } from 'nestjs-cache-dependency';

@Module({
  imports: [
    CacheDependencyModule.register(),
    ...
  ],
  providers: [...],
})
export class AppModule {}

Inject CacheDependencyService:

@Injectable()
export class MyService {
  constructor(private readonly cacheDependencyService: CacheDependencyService) {}
}

Examples

// Set multiple cache with a dependency key 'user-123'
await this.cacheDependencyService.set('user_42_profile', 'myprofile', 30, [
  'user-123',
]);
await this.cacheDependencyService.set('user_42_stats', 'mystats', 30, [
  'user-123',
]);

// Get data from cache
console.log(await this.cacheDependencyService.get('user_42_profile')); //'myprofile'
console.log(await this.cacheDependencyService.get('user_42_stats')); //'mystats'

// invalidating cache data associate with 'user-123'
await this.cacheDependencyService.invalidate(['user-123']);

// Get data from cache
console.log(await this.cacheDependencyService.get('user_42_profile')); //null
console.log(await this.cacheDependencyService.get('user_42_stats')); //null
2.0.0

10 months ago

1.0.8

1 year ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago