file-entry-cache
Advanced tools
Comparing version 4.0.0 to 5.0.0
61
cache.js
@@ -5,9 +5,9 @@ var path = require( 'path' ); | ||
module.exports = { | ||
createFromFile: function ( filePath ) { | ||
createFromFile: function ( filePath, useChecksum ) { | ||
var fname = path.basename( filePath ); | ||
var dir = path.dirname( filePath ); | ||
return this.create( fname, dir ); | ||
return this.create( fname, dir, useChecksum ); | ||
}, | ||
create: function ( cacheId, _path ) { | ||
create: function ( cacheId, _path, useChecksum ) { | ||
var fs = require( 'fs' ); | ||
@@ -98,12 +98,53 @@ var flatCache = require( 'flat-cache' ); | ||
getFileDescriptor: function ( file ) { | ||
var fstat; | ||
try { | ||
fstat = fs.statSync( file ); | ||
} catch (ex) { | ||
this.removeEntry( file ); | ||
return { key: file, notFound: true, err: ex }; | ||
} | ||
if ( useChecksum ) { | ||
return this._getFileDescriptorUsingChecksum( file ); | ||
} | ||
return this._getFileDescriptorUsingMtimeAndSize( file, fstat ); | ||
}, | ||
_getFileDescriptorUsingMtimeAndSize: function ( file, fstat ) { | ||
var meta = cache.getKey( file ); | ||
var cacheExists = !!meta; | ||
var me = this; | ||
var cSize = fstat.size; | ||
var cTime = fstat.mtime.getTime(); | ||
var isDifferentDate; | ||
var isDifferentSize; | ||
if ( !meta ) { | ||
meta = { size: cSize, mtime: cTime }; | ||
} else { | ||
isDifferentDate = cTime !== meta.mtime; | ||
isDifferentSize = cSize !== meta.size; | ||
} | ||
var nEntry = normalizedEntries[ file ] = { | ||
key: file, | ||
changed: !cacheExists || isDifferentDate || isDifferentSize, | ||
meta: meta | ||
}; | ||
return nEntry; | ||
}, | ||
_getFileDescriptorUsingChecksum: function ( file ) { | ||
var meta = cache.getKey( file ); | ||
var cacheExists = !!meta; | ||
var contentBuffer; | ||
try { | ||
contentBuffer = fs.readFileSync( file ); | ||
} catch (ex) { | ||
me.removeEntry( file ); | ||
return { key: file, notFound: true, err: ex }; | ||
contentBuffer = ''; | ||
} | ||
@@ -197,5 +238,7 @@ | ||
*/ | ||
reconcile: function () { | ||
reconcile: function ( noPrune ) { | ||
removeNotFoundFiles(); | ||
noPrune = typeof noPrune === 'undefined' ? true : noPrune; | ||
var entries = normalizedEntries; | ||
@@ -227,3 +270,3 @@ var keys = Object.keys( entries ); | ||
cache.save( true ); | ||
cache.save( noPrune ); | ||
} | ||
@@ -230,0 +273,0 @@ }; |
# file-entry-cache - Changelog | ||
## v5.0.0 | ||
- **Refactoring** | ||
- Make checksum comparison optional - [b0f9ae0]( https://github.com/royriojas/file-entry-cache/commit/b0f9ae0 ), [Roy Riojas](https://github.com/Roy Riojas), 03/02/2019 21:17:39 | ||
To determine if a file has changed we were using the checksum in the newer versions, but eslint was relying on the old behavior where we use the mtime and file size to determine if a file changed. That's why we decided to make the checksum check optional. | ||
To use it: | ||
```js | ||
// to make the cache use the checkSum check do the following: | ||
var fCache = fileEntryCache.create(cacheName, dir, useCheckSum); // pass the third parameter as true | ||
var otherCache = fileEntryCache.createFromFile(cacheName, useCheckSum); // pass the second parameter as true | ||
``` | ||
## v4.0.0 | ||
@@ -4,0 +18,0 @@ - **Build Scripts Changes** |
{ | ||
"name": "file-entry-cache", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Super simple cache for file metadata, useful for process that work o a given series of files and that only need to repeat the job on the changed ones since the previous run of the process", | ||
@@ -5,0 +5,0 @@ "repository": "royriojas/file-entry-cache", |
@@ -16,2 +16,13 @@ # file-entry-cache | ||
The module exposes two functions `create` and `createFromFile`. | ||
## `create(cacheName, [directory, useCheckSum])` | ||
- **cacheName**: the name of the cache to be created | ||
- **directory**: Optional the directory to load the cache from | ||
- **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. | ||
## `createFromFile(pathToCache, [useCheckSum])` | ||
- **pathToCache**: the path to the cache file (this combines the cache name and directory) | ||
- **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. | ||
```js | ||
@@ -97,9 +108,3 @@ // loads the cache, if one does not exists for the given | ||
- All the changes to the cache state are done to memory first and only persisted after reconcile. | ||
- 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 | ||
you prefer to do a manual pruning of the cache files, you can pass `true` to the `reconcile` call. Like this: | ||
```javascript | ||
cache.reconcile( true /* noPrune */ ); | ||
``` | ||
## License | ||
@@ -106,0 +111,0 @@ |
23622
230
113