Ohm-Management - Projektarbeit B-ME
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.

README.md 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. ```js
  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 fileEntryCache = require('file-entry-cache');
  15. var cache = fileEntryCache.create('testCache');
  16. var files = expand('../fixtures/*.txt');
  17. // the first time this method is called, will return all the files
  18. var oFiles = cache.getUpdatedFiles(files);
  19. // this will persist this to disk checking each file stats and
  20. // updating the meta attributes `size` and `mtime`.
  21. // custom fields could also be added to the meta object and will be persisted
  22. // in order to retrieve them later
  23. cache.reconcile();
  24. // use this if you want the non visited file entries to be kept in the cache
  25. // for more than one execution
  26. //
  27. // cache.reconcile( true /* noPrune */)
  28. // on a second run
  29. var cache2 = fileEntryCache.create('testCache');
  30. // will return now only the files that were modified or none
  31. // if no files were modified previous to the execution of this function
  32. var oFiles = cache.getUpdatedFiles(files);
  33. // if you want to prevent a file from being considered non modified
  34. // something useful if a file failed some sort of validation
  35. // you can then remove the entry from the cache doing
  36. cache.removeEntry('path/to/file'); // path to file should be the same path of the file received on `getUpdatedFiles`
  37. // that will effectively make the file to appear again as modified until the validation is passed. In that
  38. // case you should not remove it from the cache
  39. // if you need all the files, so you can determine what to do with the changed ones
  40. // you can call
  41. var oFiles = cache.normalizeEntries(files);
  42. // oFiles will be an array of objects like the following
  43. entry = {
  44. key: 'some/name/file', the path to the file
  45. changed: true, // if the file was changed since previous run
  46. meta: {
  47. size: 3242, // the size of the file
  48. mtime: 231231231, // the modification time of the file
  49. data: {} // some extra field stored for this file (useful to save the result of a transformation on the file
  50. }
  51. }
  52. ```
  53. ## Motivation for this module
  54. I needed a super simple and dumb **in-memory cache** with optional disk persistence (write-back cache) in order to make
  55. a script that will beautify files with `esformatter` to execute only on the files that were changed since the last run.
  56. In doing so the process of beautifying files was reduced from several seconds to a small fraction of a second.
  57. This module uses [flat-cache](https://www.npmjs.com/package/flat-cache) a super simple `key/value` cache storage with
  58. optional file persistance.
  59. The main idea is to read the files when the task begins, apply the transforms required, and if the process succeed,
  60. then store the new state of the files. The next time this module request for `getChangedFiles` will return only
  61. the files that were modified. Making the process to end faster.
  62. This module could also be used by processes that modify the files applying a transform, in that case the result of the
  63. transform could be stored in the `meta` field, of the entries. Anything added to the meta field will be persisted.
  64. Those processes won't need to call `getChangedFiles` they will instead call `normalizeEntries` that will return the
  65. entries with a `changed` field that can be used to determine if the file was changed or not. If it was not changed
  66. the transformed stored data could be used instead of actually applying the transformation, saving time in case of only
  67. a few files changed.
  68. In the worst case scenario all the files will be processed. In the best case scenario only a few of them will be processed.
  69. ## Important notes
  70. - 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
  71. - All the changes to the cache state are done to memory first and only persisted after reconcile.
  72. - By default non visited entries are removed from the cache. This is done to prevent the file from growing too much. If this is not an issue and
  73. you prefer to do a manual pruning of the cache files, you can pass `true` to the `reconcile` call. Like this:
  74. ```javascript
  75. cache.reconcile( true /* noPrune */ );
  76. ```
  77. ## License
  78. MIT