@krisell/file-cache
v2.0.1
Published
File based cache for fast access between invocations.
Downloads
5
Readme
File based cache for Node.js
This package provides a simple filesystem based caching layer for Node.js-applications.
Usage
import FileCache from '@krisell/file-cache'
const cache = new FileCache('/tmp')
cache.remember(key, ttl, () => {
// Do whatever you want, and return a promise which resolves to the data you like to cache.
// The data is kept in cache and this callback will not be executed again until the cache has expired.
// If your data can be retreived synchronously, you still need to return a promise and
// can do this using return Promise.resolve(data)
// ttl is given in seconds
})
// Example
cache.remember('tts-token', 30, () => {
return Promise.resolve('result-of-heavy-db-query-or-api-call')
}).then(data => console.log(data))
Example use cases
API-calls that require a call to issue a token that is valid for 10 minutes. Use this cache to only perform this request at most every 9 minutes.