You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.7 KiB

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