In-Memory cache for node.js
To install cache in an existing project as a dependency:
Install with npm:
npm install @dancastillo/cache
Install with yarn:
yarn add @dancastillo/cache
Install with pnpm:
pnpm install @dancastillo/cache
// ESM
import Cache from '@dancastillo/cache'
const cache = new Cache()
cache.set('foo', 'bar')
// CommonJs
const Cache = require('@dancastillo/cache')
const cache = Cache()
cache.set('foo', 'bar')
scache(<options>)
accepts an options object.
const duration = {
days: 1,
hours: 12,
minutes: 10,
seconds: 30
}
const cache = cache({ duration })
duration
: This is used to add a time to live for the cache data. This option is optional.duration.days
: Number of days to hold the data in cache. This option is optional.duration.hours
: Number of hours to hold the data in cache. This option is optional.duration.minutes
: Number of minutes to hold the data in cache. This option is optional.duration.seconds
: Number of seconds to hold the data in cache. This option is optional.
get
const value = cache.get('foo') // bar
get(key): any
: Used to get a value stored in cachekey
: string
put
cache.put('foo', 'bar')
// with duration
const duration = { hours: 12 }
cache.put('foo', 'bar', duration)
put(key, value, DurationOptions): void
: Used to get a value stored in cache.key
: stringvalue
: anyDurationOptions
: here
del
cache.del('foo')
del(key): void
: Used to delete a value stored in cachekey
: string
clear
cache.clear()
clear(): void
: Used to delete all values stored in cache
keys
cache.keys()
keys(): string[]
: Used to retrieve all the keys in cache.
Licensed under MIT.