A simple cache that's good enough.
-
Add dependency to project
npm install --save lay-away
-
Create instance of lay-away
import Cache from 'lay-away'
const cache = Cache()
-
Write data to the cache
cache.set('a', 1)
-
Read data from the cache
cache.get('a')
import Cache from 'lay-away'
const cache = Cache()
cache.set('a', 1)
console.log(cache.get('a'))
// => 1
import Cache from 'lay-away'
import { setTimeout as sleep } from 'node:timers/promises'
const cache = Cache()
cache.set('a', 1, 1000)
console.log(cache.get('a'))
// => 1
await sleep(1000)
console.log(cache.get('a'))
// => undefined
import Cache from 'lay-away'
const cache = Cache()
cache.set('a', 1)
console.log(cache.get('a'))
// => 1
cache.del('a')
console.log(cache.get('a'))
// => undefined
import Cache from 'lay-away'
const cache = Cache()
cache.set('a', 1)
console.log(cache._data.size)
// => 1
cache.clear()
console.log(cache._data.size)
// => 0