1 var path = require('path');
2 var fs = require('fs');
3 var utils = require('./utils');
4 var del = require('./del');
5 var writeJSON = utils.writeJSON;
9 * Load a cache identified by the given Id. If the element does not exists, then initialize an empty
10 * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted
11 * then the cache module directory `./cache` will be used instead
14 * @param docId {String} the id of the cache, would also be used as the name of the file cache
15 * @param [cacheDir] {String} directory for the cache entry
17 load: function (docId, cacheDir) {
22 me._pathToFile = cacheDir ? path.resolve(cacheDir, docId) : path.resolve(__dirname, '../.cache/', docId);
24 if (fs.existsSync(me._pathToFile)) {
25 me._persisted = utils.tryParse(me._pathToFile, {});
30 * Load the cache from the provided file
32 * @param {String} pathToFile the path to the file containing the info for the cache
34 loadFile: function (pathToFile) {
36 var dir = path.dirname(pathToFile);
37 var fName = path.basename(pathToFile);
43 * Returns the entire persisted object
48 return this._persisted;
52 return Object.keys(this._persisted);
55 * sets a key to a given value
57 * @param key {string} the key to set
58 * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
60 setKey: function (key, value) {
61 this._visited[key] = true;
62 this._persisted[key] = value;
65 * remove a given key from the cache
67 * @param key {String} the key to remove from the object
69 removeKey: function (key) {
70 delete this._visited[key]; // esfmt-ignore-line
71 delete this._persisted[key]; // esfmt-ignore-line
74 * Return the value of the provided key
76 * @param key {String} the name of the key to retrieve
77 * @returns {*} the value from the key
79 getKey: function (key) {
80 this._visited[key] = true;
81 return this._persisted[key];
85 * Remove keys that were not accessed/set since the
86 * last time the `prune` method was called.
94 var keys = Object.keys(me._visited);
96 // no keys visited for either get or set value
97 if (keys.length === 0) {
101 keys.forEach(function (key) {
102 obj[key] = me._persisted[key];
110 * Save the state of the cache identified by the docId to disk
111 * as a JSON structure
112 * @param [noPrune=false] {Boolean} whether to remove from cache the non visited files
115 save: function (noPrune) {
118 !noPrune && me._prune();
119 writeJSON(me._pathToFile, me._persisted);
123 * remove the file where the cache is persisted
124 * @method removeCacheFile
125 * @return {Boolean} true or false if the file was successfully deleted
127 removeCacheFile: function () {
128 return del(this._pathToFile);
131 * Destroy the file cache and cache content.
134 destroy: function () {
139 me.removeCacheFile();
145 * Alias for create. Should be considered depreacted. Will be removed in next releases
148 * @param docId {String} the id of the cache, would also be used as the name of the file cache
149 * @param [cacheDir] {String} directory for the cache entry
150 * @returns {cache} cache instance
152 load: function (docId, cacheDir) {
153 return this.create(docId, cacheDir);
157 * Load a cache identified by the given Id. If the element does not exists, then initialize an empty
161 * @param docId {String} the id of the cache, would also be used as the name of the file cache
162 * @param [cacheDir] {String} directory for the cache entry
163 * @returns {cache} cache instance
165 create: function (docId, cacheDir) {
166 var obj = Object.create(cache);
167 obj.load(docId, cacheDir);
171 createFromFile: function (filePath) {
172 var obj = Object.create(cache);
173 obj.loadFile(filePath);
177 * Clear the cache identified by the given id. Caches stored in a different cache directory can be deleted directly
180 * @param docId {String} the id of the cache, would also be used as the name of the file cache
181 * @param cacheDir {String} the directory where the cache file was written
182 * @returns {Boolean} true if the cache folder was deleted. False otherwise
184 clearCacheById: function (docId, cacheDir) {
185 var filePath = cacheDir ? path.resolve(cacheDir, docId) : path.resolve(__dirname, '../.cache/', docId);
186 return del(filePath);
189 * Remove all cache stored in the cache directory
191 * @returns {Boolean} true if the cache folder was deleted. False otherwise
193 clearAll: function (cacheDir) {
194 var filePath = cacheDir ? path.resolve(cacheDir) : path.resolve(__dirname, '../.cache/');
195 return del(filePath);