1.0.3 • Published 7 months ago

nestjs-cache-manager v1.0.3

Weekly downloads
-
License
-
Repository
-
Last release
7 months ago

how to install

npm i nestjs-cache-manager

why this lib

Most of the time when i use a cache-manager i need to do this

const key = 'key-1'
const cachedItem = await this.cacheManager.get(key)

if (cachedItem) {
    return cachedItem
}

const item = await this.service.get()

await this.cacheManager.set(key, item)

return item

It's annoying so i create this lib for doing something like this

return this.cacheManager('key-1', () => this.service.get())

The lib get the value, if no value found it's using the fallback to get the value, set it and return the value

how to init

On nest you need to do that, you need a Logger with at least an error function, this lib doesnt throw error, this module is global

    CacheManagerModule.register({ ttl: 300 }, LoggerService),

Outside nest

const cacheManager = new CacheManagerService({ ttl: 300 }, instanciedLogger)

options

  store?: string => set to redis if you want redis manager
  ttl: number => ttl in seconds
  host?: string => only if redis
  port?: number => only if redis
  keyPrefix?: string => prefix every set and get
  maxRetriesPerRequest?: number => only if redis