cache.js 5.6 KB

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