Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
flat-cache
Advanced tools
The flat-cache package is a simple disk-based key-value store that allows you to set and get data, cache it on disk, and clear the cache when necessary. It is useful for caching results of expensive operations, storing configuration, or any other use case where a simple persistent key-value store is needed.
Caching data to disk
This feature allows you to cache data to disk by setting a key-value pair and then saving the cache. The data can be retrieved later, even after the process is restarted.
const flatCache = require('flat-cache');
let cache = flatCache.load('myCache');
cache.setKey('key', 'value');
cache.save();
Loading cached data from disk
This feature allows you to load previously saved cache from disk and retrieve the value associated with a specific key.
const flatCache = require('flat-cache');
let cache = flatCache.load('myCache');
let value = cache.getKey('key');
Removing a specific key from the cache
This feature allows you to remove a specific key from the cache and then save the updated cache to disk.
const flatCache = require('flat-cache');
let cache = flatCache.load('myCache');
cache.removeKey('key');
cache.save();
Clearing the entire cache
This feature allows you to clear the entire cache and then save the empty cache to disk, effectively resetting it.
const flatCache = require('flat-cache');
let cache = flatCache.load('myCache');
cache.clearAll();
cache.save();
node-cache is an in-memory caching module. It is similar to flat-cache in providing simple key-value storage, but it does not persist the cache to disk by default.
memory-cache is another in-memory key-value store that is similar to flat-cache. It is designed for caching objects in memory and does not include built-in disk persistence.
localforage is a fast and simple storage library for JavaScript. It improves upon flat-cache by providing a more powerful API and support for IndexedDB, WebSQL, and localStorage, which can be used for client-side storage in web applications.
keyv is a simple key-value storage with support for multiple backends, including MongoDB, SQLite, PostgreSQL, and more. Unlike flat-cache, keyv is more versatile due to its support for various storage adapters.
A stupidly simple key/value storage using files to persist the data
npm i --save flat-cache
var flatCache = require('flat-cache')
// loads the cache, if one does not exists for the given
// Id a new one will be prepared to be created
var cache = flatCache.load('cacheId');
// sets a key on the cache
cache.setKey('key', { foo: 'var' });
// get a key from the cache
cache.getKey('key') // { foo: 'var' }
// remove a key
cache.removeKey('key'); // removes a key from the cache
// save it to disk
cache.save(); // very important, if you don't save no changes will be persisted.
// cache.save( true /* noPrune */) // can be used to prevent the removal of non visited keys
// loads the cache from a given directory, if one does
// not exists for the given Id a new one will be prepared to be created
var cache = flatCache.load('cacheId', path.resolve('./path/to/folder'));
// The following methods are useful to clear the cache
// delete a given cache
flatCache.clearCacheById('cacheId') // removes the cacheId document if one exists.
// delete all cache
flatCache.clearAll(); // remove the cache directory
I needed a super simple and dumb in-memory cache with optional disk persistance in order to make
a script that will beutify files with esformatter
only execute on the files that were changed since the last run.
To make that possible we need to store the fileSize
and modificationTime
of the files. So a simple key/value
storage was needed and Bam! this module was born.
load
method is called, a folder named .cache
will be created
inside the module directory when cache.save
is called. If you're committing your node_modules
to any vcs, you
might want to ignore the default .cache
folder, or specify a custom directory.stringify-able
ones, meaning no circular referencesObject.observe
to deliver the changes to disk, but I wanted to keep this module
intentionally dumb and simplecache.save()
is called. If this is not desired, you can pass true
to the save call
like: cache.save( true /* noPrune */ )
.MIT
FAQs
A stupidly simple key/value storage using files to persist some data
The npm package flat-cache receives a total of 37,415,956 weekly downloads. As such, flat-cache popularity was classified as popular.
We found that flat-cache demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.