source: imaps-frontend/node_modules/file-entry-cache/README.md@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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