Flat-cache is a remarkably simplistic yet effective key/value storage system that uses files to persist data. It works as an in-memory cache with optional disk persistence. The purpose of flat-cache is to deliver a straightforward and "dumb" storage system that can be used for various applications, from storing file sizes and modification times to managing simple key/value storage. This NPM package offers a range of features to manage your data cache including loading cache, setting keys, removing keys, saving changes, and clearing the cache entirely.
Using flat-cache is a straightforward process involving a few simple functions. To install it, first, run npm i --save flat-cache
in your terminal.
Below is a basic example of how to use flat-cache in your JavaScript code:
var flatCache = require('flat-cache')
// Load the cache
var cache = flatCache.load('cacheId');
// Set a key on the cache
cache.setKey('key', { foo: 'var' });
// Get a key from the cache
console.log(cache.getKey('key')) // Outputs: { foo: 'var' }
// Fetch the entire persisted object
console.log(cache.all()) // Outputs: { 'key': { foo: 'var' } }
// Remove a key
cache.removeKey('key');
// Save it to disk
cache.save();
Please note, it is essential that you save your changes using cache.save()
, else no changes will be persisted.
The flat-cache documentation can be found within the README content on the package's GitHub page. Visit https://github.com/jaredwray/flat-cache to view the various functions and usage examples of the flat-cache package. For a more in-depth guide to its functions, including more complex usage scenarios, refer to this documentation.