Package Exports
- flat-cache
Readme
flat-cache
A simple key/value storage using files to persist the data
Features
- A simple key/value storage using files to persist the data
- Uses a in-memory cache (via
CacheableMemory
) as the primary storage and then persists the data to disk - Automatically saves the data to disk via
persistInterval
setting. Off By Default - Easily Loads the data from disk and into memory
- Uses
ttl
andlruSize
to manage the cache and persist the data - Only saves the data to disk if the data has changed even when using
persistInterval
or callingsave()
- Uses
flatted
to parse and stringify the data by default but can be overridden
Installation
npm install flat-cache
Getting Started
import { FlatCache } from 'flat-cache';
const cache = new FlatCache();
cache.setKey('key', 'value');
cache.save(); // Saves the data to disk
lets add it with ttl
, lruSize
, and persistInterval
import { FlatCache } from 'flat-cache';
const cache = new FlatCache({
ttl: 60 * 60 * 1000 , // 1 hour
lruSize: 10000, // 10,000 items
expirationInterval: 5 * 1000 * 60, // 5 minutes
persistInterval: 5 * 1000 * 60, // 5 minutes
});
cache.setKey('key', 'value');
This will save the data to disk every 5 minutes and will remove any data that has not been accessed in 1 hour or if the cache has more than 10,000 items.
FlatCache Options
ttl
- The time to live for the cache in milliseconds. Default is0
which means no expirationlruSize
- The number of items to keep in the cache. Default is0
which means no limituseClone
- Iftrue
it will clone the data before returning it. Default isfalse
expirationInterval
- The interval to check for expired items in the cache. Default is0
which means no expirationpersistInterval
- The interval to save the data to disk. Default is0
which means no persistencecacheDir
- The directory to save the cache files. Default is./cache
cacheId
- The id of the cache. Default iscache1
API
cache
- The in-memory cache as aCacheableMemory
instancecacheDir
- The directory to save the cache filescacheId
- The id of the cachecacheFilePath
- The full path to the cache filecacheDirPath
- The full path to the cache directorypersistInterval
- The interval to save the data to diskload(cacheId: string, cacheDir?: string)
- Loads the data from diskloadFile(pathToFile: string)
- Loads the data from diskall()
- Gets all the data in the cacheitems()
- Gets all the items in the cachekeys()
- Gets all the keys in the cachesetKey(key: string, value: any, ttl?: string | number)
- (legacy) Sets the key/value pair in the cacheset(key: string, value: any, ttl?: string | number)
- Sets the key/value pair in the cachegetKey<T>(key: string)
- Gets the value for the key or the default valueget<T>(key: string)
- Gets the value for the key or the default valueremoveKey(key: string)
- Removes the key from the cachedelete(key: string)
- Removes the key from the cacheclear()
- Clears the cachesave()
- Saves the data to diskdestroy()
- Destroys the cache and remove files
How to Contribute
You can contribute by forking the repo and submitting a pull request. Please make sure to add tests and update the documentation. To learn more about how to contribute go to our main README https://github.com/jaredwray/cacheable. This will talk about how to Open a Pull Request
, Ask a Question
, or Post an Issue
.