[d565449] | 1 | # flat-cache
|
---|
| 2 | > A stupidly simple key/value storage using files to persist the data
|
---|
| 3 |
|
---|
| 4 | [![NPM Version](https://img.shields.io/npm/v/flat-cache.svg?style=flat)](https://npmjs.org/package/flat-cache)
|
---|
| 5 | [![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)
|
---|
| 6 | [![codecov](https://codecov.io/github/jaredwray/flat-cache/branch/master/graph/badge.svg?token=KxR95XT3NF)](https://codecov.io/github/jaredwray/flat-cache)
|
---|
| 7 | [![npm](https://img.shields.io/npm/dm/flat-cache)](https://npmjs.com/package/flat-cache)
|
---|
| 8 |
|
---|
| 9 | ## install
|
---|
| 10 |
|
---|
| 11 | ```bash
|
---|
| 12 | npm i --save flat-cache
|
---|
| 13 | ```
|
---|
| 14 |
|
---|
| 15 | ## Usage
|
---|
| 16 |
|
---|
| 17 | ```js
|
---|
| 18 | var flatCache = require('flat-cache')
|
---|
| 19 | // loads the cache, if one does not exists for the given
|
---|
| 20 | // Id a new one will be prepared to be created
|
---|
| 21 | var cache = flatCache.load('cacheId');
|
---|
| 22 |
|
---|
| 23 | // sets a key on the cache
|
---|
| 24 | cache.setKey('key', { foo: 'var' });
|
---|
| 25 |
|
---|
| 26 | // get a key from the cache
|
---|
| 27 | cache.getKey('key') // { foo: 'var' }
|
---|
| 28 |
|
---|
| 29 | // fetch the entire persisted object
|
---|
| 30 | cache.all() // { 'key': { foo: 'var' } }
|
---|
| 31 |
|
---|
| 32 | // remove a key
|
---|
| 33 | cache.removeKey('key'); // removes a key from the cache
|
---|
| 34 |
|
---|
| 35 | // save it to disk
|
---|
| 36 | cache.save(); // very important, if you don't save no changes will be persisted.
|
---|
| 37 | // cache.save( true /* noPrune */) // can be used to prevent the removal of non visited keys
|
---|
| 38 |
|
---|
| 39 | // loads the cache from a given directory, if one does
|
---|
| 40 | // not exists for the given Id a new one will be prepared to be created
|
---|
| 41 | var cache = flatCache.load('cacheId', path.resolve('./path/to/folder'));
|
---|
| 42 |
|
---|
| 43 | // The following methods are useful to clear the cache
|
---|
| 44 | // delete a given cache
|
---|
| 45 | flatCache.clearCacheById('cacheId') // removes the cacheId document if one exists.
|
---|
| 46 |
|
---|
| 47 | // delete all cache
|
---|
| 48 | flatCache.clearAll(); // remove the cache directory
|
---|
| 49 | ```
|
---|
| 50 |
|
---|
| 51 | ## Motivation for this module
|
---|
| 52 |
|
---|
| 53 | I needed a super simple and dumb **in-memory cache** with optional disk persistance in order to make
|
---|
| 54 | a script that will beutify files with `esformatter` only execute on the files that were changed since the last run.
|
---|
| 55 | To make that possible we need to store the `fileSize` and `modificationTime` of the files. So a simple `key/value`
|
---|
| 56 | storage was needed and Bam! this module was born.
|
---|
| 57 |
|
---|
| 58 | ## Important notes
|
---|
| 59 | - If no directory is especified when the `load` method is called, a folder named `.cache` will be created
|
---|
| 60 | inside the module directory when `cache.save` is called. If you're committing your `node_modules` to any vcs, you
|
---|
| 61 | might want to ignore the default `.cache` folder, or specify a custom directory.
|
---|
| 62 | - The values set on the keys of the cache should be `stringify-able` ones, meaning no circular references
|
---|
| 63 | - All the changes to the cache state are done to memory
|
---|
| 64 | - I could have used a timer or `Object.observe` to deliver the changes to disk, but I wanted to keep this module
|
---|
| 65 | intentionally dumb and simple
|
---|
| 66 | - Non visited keys are removed when `cache.save()` is called. If this is not desired, you can pass `true` to the save call
|
---|
| 67 | like: `cache.save( true /* noPrune */ )`.
|
---|
| 68 |
|
---|
| 69 | ## License
|
---|
| 70 |
|
---|
| 71 | MIT
|
---|
| 72 |
|
---|
| 73 | ## Changelog
|
---|
| 74 |
|
---|
| 75 | [changelog](./changelog.md)
|
---|