cache-manager-nats-store
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Nats store for cache manager

This is package use nats jetstream key value bucket

Install with cache-manager package

Install

npm install cache-manager

npm install cache-manager-nats-store

Usage Examples
const 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 with nestjs and cache-manager

Install

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)
  }

}

Package Sidebar

Install

npm i cache-manager-nats-store

Weekly Downloads

12

Version

1.1.0

License

ISC

Unpacked Size

11 kB

Total Files

4

Last publish

Collaborators

  • cn16a1