[d565449] | 1 | var path = require('path');
|
---|
| 2 | var fs = require('fs');
|
---|
| 3 | var Keyv = require('keyv');
|
---|
| 4 | var utils = require('./utils');
|
---|
| 5 | var del = require('./del');
|
---|
| 6 | var writeJSON = utils.writeJSON;
|
---|
| 7 |
|
---|
| 8 | var cache = {
|
---|
| 9 | /**
|
---|
| 10 | * Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
---|
| 11 | * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted
|
---|
| 12 | * then the cache module directory `./cache` will be used instead
|
---|
| 13 | *
|
---|
| 14 | * @method load
|
---|
| 15 | * @param docId {String} the id of the cache, would also be used as the name of the file cache
|
---|
| 16 | * @param [cacheDir] {String} directory for the cache entry
|
---|
| 17 | */
|
---|
| 18 | load: function (docId, cacheDir) {
|
---|
| 19 | var me = this;
|
---|
| 20 |
|
---|
| 21 | me.keyv = new Keyv();
|
---|
| 22 |
|
---|
| 23 | me.__visited = {};
|
---|
| 24 | me.__persisted = {};
|
---|
| 25 | me._pathToFile = cacheDir ? path.resolve(cacheDir, docId) : path.resolve(__dirname, '../.cache/', docId);
|
---|
| 26 |
|
---|
| 27 | if (fs.existsSync(me._pathToFile)) {
|
---|
| 28 | me._persisted = utils.tryParse(me._pathToFile, {});
|
---|
| 29 | }
|
---|
| 30 | },
|
---|
| 31 |
|
---|
| 32 | get _persisted() {
|
---|
| 33 | return this.__persisted;
|
---|
| 34 | },
|
---|
| 35 |
|
---|
| 36 | set _persisted(value) {
|
---|
| 37 | this.__persisted = value;
|
---|
| 38 | this.keyv.set('persisted', value);
|
---|
| 39 | },
|
---|
| 40 |
|
---|
| 41 | get _visited() {
|
---|
| 42 | return this.__visited;
|
---|
| 43 | },
|
---|
| 44 |
|
---|
| 45 | set _visited(value) {
|
---|
| 46 | this.__visited = value;
|
---|
| 47 | this.keyv.set('visited', value);
|
---|
| 48 | },
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Load the cache from the provided file
|
---|
| 52 | * @method loadFile
|
---|
| 53 | * @param {String} pathToFile the path to the file containing the info for the cache
|
---|
| 54 | */
|
---|
| 55 | loadFile: function (pathToFile) {
|
---|
| 56 | var me = this;
|
---|
| 57 | var dir = path.dirname(pathToFile);
|
---|
| 58 | var fName = path.basename(pathToFile);
|
---|
| 59 |
|
---|
| 60 | me.load(fName, dir);
|
---|
| 61 | },
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Returns the entire persisted object
|
---|
| 65 | * @method all
|
---|
| 66 | * @returns {*}
|
---|
| 67 | */
|
---|
| 68 | all: function () {
|
---|
| 69 | return this._persisted;
|
---|
| 70 | },
|
---|
| 71 |
|
---|
| 72 | keys: function () {
|
---|
| 73 | return Object.keys(this._persisted);
|
---|
| 74 | },
|
---|
| 75 | /**
|
---|
| 76 | * sets a key to a given value
|
---|
| 77 | * @method setKey
|
---|
| 78 | * @param key {string} the key to set
|
---|
| 79 | * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
---|
| 80 | */
|
---|
| 81 | setKey: function (key, value) {
|
---|
| 82 | this._visited[key] = true;
|
---|
| 83 | this._persisted[key] = value;
|
---|
| 84 | },
|
---|
| 85 | /**
|
---|
| 86 | * remove a given key from the cache
|
---|
| 87 | * @method removeKey
|
---|
| 88 | * @param key {String} the key to remove from the object
|
---|
| 89 | */
|
---|
| 90 | removeKey: function (key) {
|
---|
| 91 | delete this._visited[key]; // esfmt-ignore-line
|
---|
| 92 | delete this._persisted[key]; // esfmt-ignore-line
|
---|
| 93 | },
|
---|
| 94 | /**
|
---|
| 95 | * Return the value of the provided key
|
---|
| 96 | * @method getKey
|
---|
| 97 | * @param key {String} the name of the key to retrieve
|
---|
| 98 | * @returns {*} the value from the key
|
---|
| 99 | */
|
---|
| 100 | getKey: function (key) {
|
---|
| 101 | this._visited[key] = true;
|
---|
| 102 | return this._persisted[key];
|
---|
| 103 | },
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * Remove keys that were not accessed/set since the
|
---|
| 107 | * last time the `prune` method was called.
|
---|
| 108 | * @method _prune
|
---|
| 109 | * @private
|
---|
| 110 | */
|
---|
| 111 | _prune: function () {
|
---|
| 112 | var me = this;
|
---|
| 113 | var obj = {};
|
---|
| 114 |
|
---|
| 115 | var keys = Object.keys(me._visited);
|
---|
| 116 |
|
---|
| 117 | // no keys visited for either get or set value
|
---|
| 118 | if (keys.length === 0) {
|
---|
| 119 | return;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | keys.forEach(function (key) {
|
---|
| 123 | obj[key] = me._persisted[key];
|
---|
| 124 | });
|
---|
| 125 |
|
---|
| 126 | me._visited = {};
|
---|
| 127 | me._persisted = obj;
|
---|
| 128 | },
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * Save the state of the cache identified by the docId to disk
|
---|
| 132 | * as a JSON structure
|
---|
| 133 | * @param [noPrune=false] {Boolean} whether to remove from cache the non visited files
|
---|
| 134 | * @method save
|
---|
| 135 | */
|
---|
| 136 | save: function (noPrune) {
|
---|
| 137 | var me = this;
|
---|
| 138 |
|
---|
| 139 | !noPrune && me._prune();
|
---|
| 140 | writeJSON(me._pathToFile, me._persisted);
|
---|
| 141 | },
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * remove the file where the cache is persisted
|
---|
| 145 | * @method removeCacheFile
|
---|
| 146 | * @return {Boolean} true or false if the file was successfully deleted
|
---|
| 147 | */
|
---|
| 148 | removeCacheFile: function () {
|
---|
| 149 | return del(this._pathToFile);
|
---|
| 150 | },
|
---|
| 151 | /**
|
---|
| 152 | * Destroy the file cache and cache content.
|
---|
| 153 | * @method destroy
|
---|
| 154 | */
|
---|
| 155 | destroy: function () {
|
---|
| 156 | var me = this;
|
---|
| 157 | me._visited = {};
|
---|
| 158 | me._persisted = {};
|
---|
| 159 |
|
---|
| 160 | me.removeCacheFile();
|
---|
| 161 | },
|
---|
| 162 | };
|
---|
| 163 |
|
---|
| 164 | module.exports = {
|
---|
| 165 | /**
|
---|
| 166 | * Alias for create. Should be considered depreacted. Will be removed in next releases
|
---|
| 167 | *
|
---|
| 168 | * @method load
|
---|
| 169 | * @param docId {String} the id of the cache, would also be used as the name of the file cache
|
---|
| 170 | * @param [cacheDir] {String} directory for the cache entry
|
---|
| 171 | * @returns {cache} cache instance
|
---|
| 172 | */
|
---|
| 173 | load: function (docId, cacheDir) {
|
---|
| 174 | return this.create(docId, cacheDir);
|
---|
| 175 | },
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
| 178 | * Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
---|
| 179 | * cache storage.
|
---|
| 180 | *
|
---|
| 181 | * @method create
|
---|
| 182 | * @param docId {String} the id of the cache, would also be used as the name of the file cache
|
---|
| 183 | * @param [cacheDir] {String} directory for the cache entry
|
---|
| 184 | * @returns {cache} cache instance
|
---|
| 185 | */
|
---|
| 186 | create: function (docId, cacheDir) {
|
---|
| 187 | var obj = Object.create(cache);
|
---|
| 188 | obj.load(docId, cacheDir);
|
---|
| 189 | return obj;
|
---|
| 190 | },
|
---|
| 191 |
|
---|
| 192 | createFromFile: function (filePath) {
|
---|
| 193 | var obj = Object.create(cache);
|
---|
| 194 | obj.loadFile(filePath);
|
---|
| 195 | return obj;
|
---|
| 196 | },
|
---|
| 197 | /**
|
---|
| 198 | * Clear the cache identified by the given id. Caches stored in a different cache directory can be deleted directly
|
---|
| 199 | *
|
---|
| 200 | * @method clearCache
|
---|
| 201 | * @param docId {String} the id of the cache, would also be used as the name of the file cache
|
---|
| 202 | * @param cacheDir {String} the directory where the cache file was written
|
---|
| 203 | * @returns {Boolean} true if the cache folder was deleted. False otherwise
|
---|
| 204 | */
|
---|
| 205 | clearCacheById: function (docId, cacheDir) {
|
---|
| 206 | var filePath = cacheDir ? path.resolve(cacheDir, docId) : path.resolve(__dirname, '../.cache/', docId);
|
---|
| 207 | return del(filePath);
|
---|
| 208 | },
|
---|
| 209 | /**
|
---|
| 210 | * Remove all cache stored in the cache directory
|
---|
| 211 | * @method clearAll
|
---|
| 212 | * @returns {Boolean} true if the cache folder was deleted. False otherwise
|
---|
| 213 | */
|
---|
| 214 | clearAll: function (cacheDir) {
|
---|
| 215 | var filePath = cacheDir ? path.resolve(cacheDir) : path.resolve(__dirname, '../.cache/');
|
---|
| 216 | return del(filePath);
|
---|
| 217 | },
|
---|
| 218 | };
|
---|