用工小程序前端代码
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.

75 lines
3.0 KiB

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