/cache

Key-Value Cache library

Primary LanguageTypeScriptMIT LicenseMIT

@tilfin/cache

npm Node License

Key-Value based cache map for sync or async

Features

  • Passive TTL(Time-To-Live) expiring
  • Hook when cached item is deleted

Install

$ npm install @tilfin/cache

How to use

Cache

new

constructor

  • defaultTtl number - default time-to-live millseconds. no expiration if zero (default: 0)
  • onDelete (item) => void - callback when cached item deleted

set

sets the value for the key with ttl for the expires

  • key string - a key to be assigned
  • value * - cached value
  • [ttl] number - time-to-live millseconds. no expiration if zero

get

returns the value specified by key or undefined if not found

  • key string - a key to be fetched

delete

deletes the value specified by key and returns true, or false if not found

  • key string - a key to be deleted

clear

deletes all values

const { Cache } = require('@tilfin/cache')

const cache = new Cache({
  defaultTtl: 60000 // Delete cached item if getting it after 60 seconds passed
})

export.set = (item) => {
  cache.set(item.id, item)
}

export.get = (id) => {
  return cache.get(id)
}

CacheAsync

new

constructor

  • defaultTtl number - default time-to-live millseconds. no expiration if zero (default: 0)
  • onDelete async (item) => void - callback when cached item deleted

set

sets the value for the key with ttl for the expires

  • key string - a key to be assigned
  • value * - cached value
  • [ttl] number - time-to-live millseconds. no expiration if zero

get

returns the value specified by key or undefined if not found

  • key <string> - a key to be fetched

delete

deletes the value specified by key and returns true, or false if not found

  • key <string> - a key to be deleted

clear

deletes all values

const { CacheAsync } = require('@tilfin/cache')

const cache = new CacheAsync({
  onDelete: async (item) => {
    await item.dispose()
  }
})

export.set = async (item) => {
  await cache.set(item.id, item, 30)
}

export.get = async (id) => {
  return await cache.get(id)
}