Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jaredwray/flat-cache
A stupidly simple key/value storage using files to persist the data
https://github.com/jaredwray/flat-cache
Last synced: about 9 hours ago
JSON representation
A stupidly simple key/value storage using files to persist the data
- Host: GitHub
- URL: https://github.com/jaredwray/flat-cache
- Owner: jaredwray
- License: mit
- Created: 2015-02-26T09:12:16.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-06-25T18:12:17.000Z (8 months ago)
- Last Synced: 2024-10-29T13:10:40.736Z (4 months ago)
- Language: JavaScript
- Size: 380 KB
- Stars: 165
- Watchers: 8
- Forks: 30
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nodejs - flat-cache - A stupidly simple key/value storage using files to persist the data. ![](https://img.shields.io/github/stars/royriojas/flat-cache.svg?style=social&label=Star) (Repository / Cache)
README
# flat-cache
> A stupidly simple key/value storage using files to persist the data
[![NPM Version](https://img.shields.io/npm/v/flat-cache.svg?style=flat)](https://npmjs.org/package/flat-cache)
[![tests](https://github.com/jaredwray/flat-cache/actions/workflows/tests.yaml/badge.svg?branch=master)](https://github.com/jaredwray/flat-cache/actions/workflows/tests.yaml)
[![codecov](https://codecov.io/github/jaredwray/flat-cache/branch/master/graph/badge.svg?token=KxR95XT3NF)](https://codecov.io/github/jaredwray/flat-cache)
[![npm](https://img.shields.io/npm/dm/flat-cache)](https://npmjs.com/package/flat-cache)## install
```bash
npm i --save flat-cache
```## Usage
```js
const 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
const 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' }// fetch the entire persisted object
cache.all(); // { '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
const 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
```## Motivation for this module
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.## Important notes
- If no directory is especified when the `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.
- The values set on the keys of the cache should be `stringify-able` ones, meaning no circular references
- All the changes to the cache state are done to memory
- I could have used a timer or `Object.observe` to deliver the changes to disk, but I wanted to keep this module
intentionally dumb and simple
- Non visited keys are removed when `cache.save()` is called. If this is not desired, you can pass `true` to the save call
like: `cache.save( true /* noPrune */ )`.## License
[MIT](LICENSE) © [Jared Wray](https://jaredwray.com)