flat-cache 1.0.10 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,11 +24,15 @@ cache.setKey('key', { foo: 'var' });
24
24
  // get a key from the cache
25
25
  cache.getKey('key') // { foo: 'var' }
26
26
 
27
+ // fetch the entire persisted object
28
+ cache.all() // { 'key': { foo: 'var' } }
29
+
27
30
  // remove a key
28
31
  cache.removeKey('key'); // removes a key from the cache
29
32
 
30
33
  // save it to disk
31
34
  cache.save(); // very important, if you don't save no changes will be persisted.
35
+ // cache.save( true /* noPrune */) // can be used to prevent the removal of non visited keys
32
36
 
33
37
  // loads the cache from a given directory, if one does
34
38
  // not exists for the given Id a new one will be prepared to be created
@@ -57,6 +61,8 @@ storage was needed and Bam! this module was born.
57
61
  - All the changes to the cache state are done to memory
58
62
  - I could have used a timer or `Object.observe` to deliver the changes to disk, but I wanted to keep this module
59
63
  intentionally dumb and simple
64
+ - Non visited keys are removed when `cache.save()` is called. If this is not desired, you can pass `true` to the save call
65
+ like: `cache.save( true /* noPrune */ )`.
60
66
 
61
67
  ## License
62
68
 
@@ -65,4 +71,3 @@ MIT
65
71
  ## Changelog
66
72
 
67
73
  [changelog](./changelog.md)
68
-
package/cache.js CHANGED
@@ -1,8 +1,8 @@
1
1
  var path = require( 'path' );
2
2
  var fs = require( 'graceful-fs' );
3
- var readJSON = require( 'read-json-sync' );
4
- var write = require( 'write' );
5
3
  var del = require( 'del' ).sync;
4
+ var utils = require( './utils' );
5
+ var writeJSON = utils.writeJSON;
6
6
 
7
7
  var cache = {
8
8
  /**
@@ -22,7 +22,7 @@ var cache = {
22
22
  me._pathToFile = cacheDir ? path.resolve( cacheDir, docId ) : path.resolve( __dirname, './.cache/', docId );
23
23
 
24
24
  if ( fs.existsSync( me._pathToFile ) ) {
25
- me._persisted = readJSON( me._pathToFile );
25
+ me._persisted = utils.tryParse( me._pathToFile, { } );
26
26
  }
27
27
  },
28
28
 
@@ -39,6 +39,15 @@ var cache = {
39
39
  me.load( fName, dir );
40
40
  },
41
41
 
42
+ /**
43
+ * Returns the entire persisted object
44
+ * @method all
45
+ * @returns {*}
46
+ */
47
+ all: function () {
48
+ return this._persisted;
49
+ },
50
+
42
51
  keys: function () {
43
52
  return Object.keys( this._persisted );
44
53
  },
@@ -100,13 +109,14 @@ var cache = {
100
109
  /**
101
110
  * Save the state of the cache identified by the docId to disk
102
111
  * as a JSON structure
112
+ * @param [noPrune=false] {Boolean} whether to remove from cache the non visited files
103
113
  * @method save
104
114
  */
105
- save: function () {
115
+ save: function ( noPrune ) {
106
116
  var me = this;
107
117
 
108
- me._prune();
109
- write.sync( me._pathToFile, JSON.stringify( me._persisted ) );
118
+ (!noPrune) && me._prune();
119
+ writeJSON( me._pathToFile, me._persisted );
110
120
  },
111
121
 
112
122
  /**
package/changelog.md CHANGED
@@ -1,5 +1,86 @@
1
1
 
2
2
  # flat-cache - Changelog
3
+ ## v1.3.0
4
+ - **Other changes**
5
+ - Added #all method ([#16](https://github.com/royriojas/flat-cache/issues/16)) - [12293be]( https://github.com/royriojas/flat-cache/commit/12293be ), [Ozair Patel](https://github.com/Ozair Patel), 25/09/2017 16:46:38
6
+
7
+ * Added #all method
8
+
9
+ * Added #all method test
10
+
11
+ * Updated readme
12
+
13
+ * Added yarn.lock
14
+
15
+ * Added more keys for #all test
16
+
17
+ * Beautified file
18
+
19
+ - fix changelog title style ([#14](https://github.com/royriojas/flat-cache/issues/14)) - [af8338a]( https://github.com/royriojas/flat-cache/commit/af8338a ), [前端小武](https://github.com/前端小武), 19/12/2016 23:34:48
20
+
21
+
22
+ ## v1.2.2
23
+ - **Bug Fixes**
24
+ - Do not crash if cache file is invalid JSON. ([#13](https://github.com/royriojas/flat-cache/issues/13)) - [87beaa6]( https://github.com/royriojas/flat-cache/commit/87beaa6 ), [Roy Riojas](https://github.com/Roy Riojas), 19/12/2016 21:03:35
25
+
26
+ Fixes <a target="_blank" class="info-link" href="https://github.com/royriojas/flat-cache/issues/12"><span>#12</span></a>
27
+
28
+ Not sure under which situations a cache file might exist that does
29
+ not contain a valid JSON structure, but just in case to cover
30
+ the possibility of this happening a try catch block has been added
31
+
32
+ If the cache is somehow not valid the cache will be discarded an a
33
+ a new cache will be stored instead
34
+ - **Other changes**
35
+ - Added travis ci support for modern node versions ([#11](https://github.com/royriojas/flat-cache/issues/11)) - [1c2b1f7]( https://github.com/royriojas/flat-cache/commit/1c2b1f7 ), [Amila Welihinda](https://github.com/Amila Welihinda), 11/11/2016 02:47:52
36
+
37
+
38
+ - Bumping `circular-son` version ([#10](https://github.com/royriojas/flat-cache/issues/10)) - [4d5e861]( https://github.com/royriojas/flat-cache/commit/4d5e861 ), [Andrea Giammarchi](https://github.com/Andrea Giammarchi), 02/08/2016 09:13:52
39
+
40
+ As mentioned in https://github.com/WebReflection/circular-json/issues/25 `circular-json` wan't rightly implementing the license field.
41
+
42
+ Latest version bump changed only that bit so that ESLint should now be happy.
43
+ ## v1.2.1
44
+ - **Bug Fixes**
45
+ - Add missing utils.js file to the package. closes [#8](https://github.com/royriojas/flat-cache/issues/8) - [ec10cf2]( https://github.com/royriojas/flat-cache/commit/ec10cf2 ), [Roy Riojas](https://github.com/Roy Riojas), 01/08/2016 04:18:57
46
+
47
+
48
+ ## v1.2.0
49
+ - **Documentation**
50
+ - Add documentation about noPrune option - [23e11f9]( https://github.com/royriojas/flat-cache/commit/23e11f9 ), [Roy Riojas](https://github.com/Roy Riojas), 01/08/2016 04:06:49
51
+
52
+
53
+ ## v1.0.11
54
+ - **Features**
55
+ - Add noPrune option to cache.save() method. closes [#7](https://github.com/royriojas/flat-cache/issues/7) - [2c8016a]( https://github.com/royriojas/flat-cache/commit/2c8016a ), [Roy Riojas](https://github.com/Roy Riojas), 01/08/2016 04:00:29
56
+
57
+
58
+ - Add json read and write utility based on circular-json - [c31081e]( https://github.com/royriojas/flat-cache/commit/c31081e ), [Jean Ponchon](https://github.com/Jean Ponchon), 28/07/2016 10:58:17
59
+
60
+
61
+ - **Bug Fixes**
62
+ - Remove UTF16 BOM stripping - [4a41e22]( https://github.com/royriojas/flat-cache/commit/4a41e22 ), [Jean Ponchon](https://github.com/Jean Ponchon), 29/07/2016 04:18:06
63
+
64
+ Since we control both writing and reading of JSON stream, there no needs
65
+ to handle unicode BOM.
66
+ - Use circular-json to handle circular references (fix [#5](https://github.com/royriojas/flat-cache/issues/5)) - [cd7aeed]( https://github.com/royriojas/flat-cache/commit/cd7aeed ), [Jean Ponchon](https://github.com/Jean Ponchon), 25/07/2016 13:11:59
67
+
68
+
69
+ - **Tests Related fixes**
70
+ - Add missing file from eslint test - [d6fa3c3]( https://github.com/royriojas/flat-cache/commit/d6fa3c3 ), [Jean Ponchon](https://github.com/Jean Ponchon), 29/07/2016 04:15:51
71
+
72
+
73
+ - Add test for circular json serialization / deserialization - [07d2ddd]( https://github.com/royriojas/flat-cache/commit/07d2ddd ), [Jean Ponchon](https://github.com/Jean Ponchon), 28/07/2016 10:59:36
74
+
75
+
76
+ - **Refactoring**
77
+ - Remove unused read-json-sync - [2be1c24]( https://github.com/royriojas/flat-cache/commit/2be1c24 ), [Jean Ponchon](https://github.com/Jean Ponchon), 28/07/2016 10:59:18
78
+
79
+
80
+ - **Build Scripts Changes**
81
+ - travis tests on 0.12 and 4x - [3a613fd]( https://github.com/royriojas/flat-cache/commit/3a613fd ), [royriojas](https://github.com/royriojas), 15/11/2015 17:34:40
82
+
83
+
3
84
  ## v1.0.10
4
85
  - **Build Scripts Changes**
5
86
  - add eslint-fix task - [fd29e52]( https://github.com/royriojas/flat-cache/commit/fd29e52 ), [royriojas](https://github.com/royriojas), 01/11/2015 18:04:08
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flat-cache",
3
- "version": "1.0.10",
3
+ "version": "1.3.0",
4
4
  "description": "A stupidly simple key/value storage using files to persist some data",
5
5
  "repository": "royriojas/flat-cache",
6
6
  "license": "MIT",
@@ -10,7 +10,8 @@
10
10
  },
11
11
  "main": "cache.js",
12
12
  "files": [
13
- "cache.js"
13
+ "cache.js",
14
+ "utils.js"
14
15
  ],
15
16
  "engines": {
16
17
  "node": ">=0.10.0"
@@ -22,9 +23,9 @@
22
23
  "npm run verify --silent"
23
24
  ],
24
25
  "scripts": {
25
- "beautify": "esbeautifier 'cache.js' 'specs/**/*.js'",
26
+ "beautify": "esbeautifier 'cache.js' 'test/specs/**/*.js'",
26
27
  "beautify-check": "npm run beautify -- -k",
27
- "eslint": "eslinter 'cache.js' 'specs/**/*.js'",
28
+ "eslint": "eslinter 'cache.js' 'utils.js' 'specs/**/*.js'",
28
29
  "eslint-fix": "npm run eslint -- --fix",
29
30
  "autofix": "npm run beautify && npm run eslint-fix",
30
31
  "check": "npm run beautify-check && npm run eslint",
@@ -78,9 +79,9 @@
78
79
  "watch-run": "^1.2.2"
79
80
  },
80
81
  "dependencies": {
82
+ "circular-json": "^0.3.1",
81
83
  "del": "^2.0.2",
82
84
  "graceful-fs": "^4.1.2",
83
- "read-json-sync": "^1.1.0",
84
85
  "write": "^0.2.1"
85
86
  }
86
87
  }
package/utils.js ADDED
@@ -0,0 +1,39 @@
1
+ var fs = require( 'graceful-fs' );
2
+ var write = require( 'write' );
3
+ var circularJson = require( 'circular-json' );
4
+
5
+ module.exports = {
6
+
7
+ tryParse: function ( filePath, defaultValue) {
8
+ var result;
9
+ try {
10
+ result = this.readJSON( filePath );
11
+ } catch (ex) {
12
+ result = defaultValue;
13
+ }
14
+ return result;
15
+ },
16
+
17
+ /**
18
+ * Read json file synchronously using circular-json
19
+ *
20
+ * @method readJSON
21
+ * @param {String} filePath Json filepath
22
+ * @returns {*} parse result
23
+ */
24
+ readJSON: function ( filePath ) {
25
+ return circularJson.parse( fs.readFileSync( filePath ).toString() );
26
+ },
27
+
28
+ /**
29
+ * Write json file synchronously using circular-json
30
+ *
31
+ * @method writeJSON
32
+ * @param {String} filePath Json filepath
33
+ * @param {*} data Object to serialize
34
+ */
35
+ writeJSON: function (filePath, data ) {
36
+ write.sync( filePath, circularJson.stringify( data ) );
37
+ }
38
+
39
+ };