flat-cache
Advanced tools
Comparing version 1.0.3 to 1.0.4
84
cache.js
@@ -5,3 +5,3 @@ var path = require( 'path' ); | ||
var write = require( 'write' ); | ||
var del = require('del').sync; | ||
var del = require( 'del' ).sync; | ||
@@ -23,10 +23,24 @@ var cache = { | ||
me._persisted = {}; | ||
me._pathToFile = cacheDir ? path.resolve(cacheDir, docId) : path.resolve( __dirname, './.cache/', docId ); | ||
me._pathToFile = cacheDir ? path.resolve( cacheDir, docId ) : path.resolve( __dirname, './.cache/', docId ); | ||
if ( fs.existsSync( me._pathToFile ) ) { | ||
this._persisted = readJSON( me._pathToFile ); | ||
me._persisted = readJSON( me._pathToFile ); | ||
} | ||
}, | ||
/** | ||
* Load the cache from the provided file | ||
* @method loadFile | ||
* @param {String} pathToFile the path to the file containing the info for the cache | ||
*/ | ||
loadFile: function ( pathToFile ) { | ||
var me = this; | ||
var dir = path.dirname( pathToFile ); | ||
var fName = path.basename( pathToFile ); | ||
me.load( fName, dir ); | ||
}, | ||
keys: function () { | ||
return Object.keys(this._persisted); | ||
return Object.keys( this._persisted ); | ||
}, | ||
@@ -97,3 +111,25 @@ /** | ||
me._prune(); | ||
write.sync( me._pathToFile, JSON.stringify( me._persisted) ); | ||
write.sync( me._pathToFile, JSON.stringify( me._persisted ) ); | ||
}, | ||
/** | ||
* remove the file where the cache is persisted | ||
* @method removeCacheFile | ||
* @return {Boolean} true or false if the file was successfully deleted | ||
*/ | ||
removeCacheFile: function () { | ||
return del( this._pathToFile, { | ||
force: true | ||
} ); | ||
}, | ||
/** | ||
* Destroy the file cache and cache content. | ||
* @method destroy | ||
*/ | ||
destroy: function () { | ||
var me = this; | ||
me._visited = {}; | ||
me._persisted = {}; | ||
me.removeCacheFile(); | ||
} | ||
@@ -104,4 +140,3 @@ }; | ||
/** | ||
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
* cache storage. | ||
* Alias for create. Should be considered depreacted. Will be removed in next releases | ||
* | ||
@@ -114,2 +149,15 @@ * @method load | ||
load: function ( docId, cacheDir ) { | ||
return this.create( docId, cacheDir ); | ||
}, | ||
/** | ||
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
* cache storage. | ||
* | ||
* @method create | ||
* @param docId {String} the id of the cache, would also be used as the name of the file cache | ||
* @param [cacheDir] {String} directory for the cache entry | ||
* @returns {cache} cache instance | ||
*/ | ||
create: function ( docId, cacheDir ) { | ||
var obj = Object.create( cache ); | ||
@@ -119,2 +167,8 @@ obj.load( docId, cacheDir ); | ||
}, | ||
createFromFile: function ( filePath ) { | ||
var obj = Object.create( cache ); | ||
obj.loadFile( filePath ); | ||
return obj; | ||
}, | ||
/** | ||
@@ -125,8 +179,10 @@ * Clear the cache identified by the given id. Caches stored in a different cache directory can be deleted directly | ||
* @param docId {String} the id of the cache, would also be used as the name of the file cache | ||
* @param cacheDir {String} the directory where the cache file was written | ||
* @returns {Boolean} true if the cache folder was deleted. False otherwise | ||
*/ | ||
clearCacheById: function (docId) { | ||
return del(path.resolve( __dirname, './.cache/', docId ), { | ||
force: true | ||
}).length > 0; | ||
clearCacheById: function ( docId, cacheDir ) { | ||
var filePath = cacheDir ? path.resolve( cacheDir, docId ) : path.resolve( __dirname, './.cache/', docId ); | ||
return del( filePath, { | ||
force: true | ||
} ).length > 0; | ||
}, | ||
@@ -139,6 +195,6 @@ /** | ||
clearAll: function () { | ||
return del(path.resolve( __dirname, './.cache/'), { | ||
force: true | ||
}).length > 0; | ||
return del( path.resolve( __dirname, './.cache/' ), { | ||
force: true | ||
} ).length > 0; | ||
} | ||
}; |
{ | ||
"name": "flat-cache", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A stupidly simple key/value storage using files to persist the data", | ||
@@ -5,0 +5,0 @@ "repository": "royriojas/flat-cache", |
9990
174