This is package use nats jetstream key value bucket Nats store for cache manager
Install Install with cache-manager package
npm install cache-manager
npm install cache-manager-nats-store
Usage Examplesconst config = {
url:'nats://0.0.0.0:4222',
ttl: 3600,
bucket: "test"
}
// Create nats-store
const natsStore = await cacheManager.caching(createBucketKeyValueStore, config);
// set key value
await natsStore.set("test1",{
id:1,
value:"Danh",
age:28
});
// get key
const value = await natsStore.get("test1");
// delete key
await natsStore.del("test1");
// delete all key in bucket
await natsStore.reset();
Install Install with nestjs and cache-manager
npm install cache-manager
npm install cache-manager-nats-store
Usage Examples// define in module
@Module({
imports: [CacheModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
const store = await createBucketKeyValueStore({
url: configService.get<string>('URL')
bucket: configService.get<string>('BUCKET'),
ttl: 86400 * 30
})
return {
store: store as unknown as CacheStore
}
},
inject: [ConfigService]
})],
controllers: [],
providers: [],
exports: []
})
export class AppModule {}
// app.service.ts
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
@Injectable()
export class Appservice {
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
del(key: string): Promise<void> {
return this.cacheManager.del(key)
}
get<T> (key: string): Promise<T> {
return this.cacheManager.get(key)
}
reset(): Promise<void> {
return this.cacheManager.reset()
}
set(key: string, data: any): Promise<void> {
return this.cacheManager.set(key, data)
}
}