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.

112 lines
5.1 KiB

4 years ago
  1. # file-entry-cache
  2. > Super simple cache for file metadata, useful for process that work o a given series of files
  3. > and that only need to repeat the job on the changed ones since the previous run of the process — Edit
  4. [![NPM Version](http://img.shields.io/npm/v/file-entry-cache.svg?style=flat)](https://npmjs.org/package/file-entry-cache)
  5. [![Build Status](http://img.shields.io/travis/royriojas/file-entry-cache.svg?style=flat)](https://travis-ci.org/royriojas/file-entry-cache)
  6. ## install
  7. ```bash
  8. npm i --save file-entry-cache
  9. ```
  10. ## Usage
  11. The module exposes two functions `create` and `createFromFile`.
  12. ## `create(cacheName, [directory, useCheckSum])`
  13. - **cacheName**: the name of the cache to be created
  14. - **directory**: Optional the directory to load the cache from
  15. - **usecheckSum**: Whether to use md5 checksum to verify if file changed. If false the default will be to use the mtime and size of the file.
  16. ## `createFromFile(pathToCache, [useCheckSum])`
  17. - **pathToCache**: the path to the cache file (this combines the cache name and directory)
  18. - **useCheckSum**: Whether to use md5 checksum to verify if file changed. If false the default will be to use the mtime and size of the file.
  19. ```js
  20. // loads the cache, if one does not exists for the given
  21. // Id a new one will be prepared to be created
  22. var fileEntryCache = require('file-entry-cache');
  23. var cache = fileEntryCache.create('testCache');
  24. var files = expand('../fixtures/*.txt');
  25. // the first time this method is called, will return all the files
  26. var oFiles = cache.getUpdatedFiles(files);
  27. // this will persist this to disk checking each file stats and
  28. // updating the meta attributes `size` and `mtime`.
  29. // custom fields could also be added to the meta object and will be persisted
  30. // in order to retrieve them later
  31. cache.reconcile();
  32. // use this if you want the non visited file entries to be kept in the cache
  33. // for more than one execution
  34. //
  35. // cache.reconcile( true /* noPrune */)
  36. // on a second run
  37. var cache2 = fileEntryCache.create('testCache');
  38. // will return now only the files that were modified or none
  39. // if no files were modified previous to the execution of this function
  40. var oFiles = cache.getUpdatedFiles(files);
  41. // if you want to prevent a file from being considered non modified
  42. // something useful if a file failed some sort of validation
  43. // you can then remove the entry from the cache doing
  44. cache.removeEntry('path/to/file'); // path to file should be the same path of the file received on `getUpdatedFiles`
  45. // that will effectively make the file to appear again as modified until the validation is passed. In that
  46. // case you should not remove it from the cache
  47. // if you need all the files, so you can determine what to do with the changed ones
  48. // you can call
  49. var oFiles = cache.normalizeEntries(files);
  50. // oFiles will be an array of objects like the following
  51. entry = {
  52. key: 'some/name/file', the path to the file
  53. changed: true, // if the file was changed since previous run
  54. meta: {
  55. size: 3242, // the size of the file
  56. mtime: 231231231, // the modification time of the file
  57. data: {} // some extra field stored for this file (useful to save the result of a transformation on the file
  58. }
  59. }
  60. ```
  61. ## Motivation for this module
  62. I needed a super simple and dumb **in-memory cache** with optional disk persistence (write-back cache) in order to make
  63. a script that will beautify files with `esformatter` to execute only on the files that were changed since the last run.
  64. In doing so the process of beautifying files was reduced from several seconds to a small fraction of a second.
  65. This module uses [flat-cache](https://www.npmjs.com/package/flat-cache) a super simple `key/value` cache storage with
  66. optional file persistance.
  67. The main idea is to read the files when the task begins, apply the transforms required, and if the process succeed,
  68. then store the new state of the files. The next time this module request for `getChangedFiles` will return only
  69. the files that were modified. Making the process to end faster.
  70. This module could also be used by processes that modify the files applying a transform, in that case the result of the
  71. transform could be stored in the `meta` field, of the entries. Anything added to the meta field will be persisted.
  72. Those processes won't need to call `getChangedFiles` they will instead call `normalizeEntries` that will return the
  73. entries with a `changed` field that can be used to determine if the file was changed or not. If it was not changed
  74. the transformed stored data could be used instead of actually applying the transformation, saving time in case of only
  75. a few files changed.
  76. In the worst case scenario all the files will be processed. In the best case scenario only a few of them will be processed.
  77. ## Important notes
  78. - The values set on the meta attribute of the entries should be `stringify-able` ones if possible, flat-cache uses `circular-json` to try to persist circular structures, but this should be considered experimental. The best results are always obtained with non circular values
  79. - All the changes to the cache state are done to memory first and only persisted after reconcile.
  80. ## License
  81. MIT