1 | [![ci(esm)](https://github.com/philcockfield/file-system-cache/actions/workflows/node.esm.yml/badge.svg)](https://github.com/philcockfield/file-system-cache/actions/workflows/node.esm.yml)
|
2 |
|
3 | # file-system-cache
|
4 |
|
5 | A super-fast, promise-based cache that reads and writes to the file-system.
|
6 |
|
7 |
|
8 | ## Installation
|
9 |
|
10 | npm install --save file-system-cache
|
11 |
|
12 | ## Usage (API)
|
13 |
|
14 | Create an instance of the cache optionally giving it a folder location to store files within.
|
15 |
|
16 | ```js
|
17 | import Cache from "file-system-cache";
|
18 |
|
19 | const cache = Cache({
|
20 | basePath: "./.cache", // Optional. Path where cache files are stored (default).
|
21 | ns: "my-namespace", // Optional. A grouping namespace for items.
|
22 | ttl: 60 // Optional. A time-to-live that determines how long the cache item is valid, in seconds.
|
23 | });
|
24 | ```
|
25 |
|
26 | > Reference `default` for CommonJS, e.g: `const Cache = require('file-system-cache').default
|
27 | `
|
28 |
|
29 | The optional `ns` ("namespace") allows for multiple groupings of files to reside within the one cache folder. When you have multiple caches with different namespaces you can re-use the same keys and they will not collide.
|
30 |
|
31 | The optional `ttl` ("time to live") allows you to set a default expiration for the cache key, in seconds. For example if you
|
32 | set this value to 60 then cache hits to any cache key made beyond the limit of that 60 seconds will result in cache misses.
|
33 |
|
34 | ### get(key, defaultValue)
|
35 | Retrieves the contents of a cached file.
|
36 |
|
37 | ```js
|
38 | cache.get("foo")
|
39 | .then(result => /* Success */)
|
40 | .catch(err => /* Fail */);
|
41 | ```
|
42 |
|
43 | or use modern `async/await` syntactic sugar of course:
|
44 |
|
45 | ```js
|
46 | const value = await cache.get("foo");
|
47 | ```
|
48 |
|
49 | Use `getSync` for a synchronous version.
|
50 | Pass a `defaultValue` parameter to use if the key does not exist within the cache.
|
51 |
|
52 |
|
53 | ### set(key, value, ttl)
|
54 | Write a value to the cache.
|
55 |
|
56 | ```js
|
57 | /* This will apply the default TTL to this cache key */
|
58 | cache.set("foo", "...value...")
|
59 | .then(result => /* Success */)
|
60 |
|
61 | /* This will set a specific TTL for this cache key */
|
62 | cache.set("bar", "...baz", 60)
|
63 | .then(result => /* Success */)
|
64 | ```
|
65 |
|
66 | Value types are stored and respected on subsequent calls to `get`. For examples, passing in Object will return that in its parsed object state.
|
67 |
|
68 | Use `setSync` for a synchronous version.
|
69 |
|
70 | ### remove(key)
|
71 | Deletes the specified cache item from the file-system.
|
72 | ```js
|
73 | cache.remove("foo")
|
74 | .then(() => /* Removed */)
|
75 | ```
|
76 |
|
77 | ### clear()
|
78 | Clears all cached items from the file-system.
|
79 | ```js
|
80 | cache.clear()
|
81 | .then(() => /* All items deleted */)
|
82 | ```
|
83 |
|
84 |
|
85 | ### save()
|
86 | Saves (sets) several items to the cache in one operation.
|
87 | ```js
|
88 | cache.save([{ key:"one", value:"hello" }, { key:"two", value:222 }])
|
89 | .then(result => /* All items saved. */)
|
90 | ```
|
91 |
|
92 | ### load()
|
93 | Loads all files within the cache's namespace.
|
94 | ```js
|
95 | cache.load()
|
96 | .then(result => /* The complete of cached files (for the ns). */)
|
97 | ```
|
98 |
|
99 |
|
100 |
|
101 | ## Test
|
102 | # Run tests.
|
103 | npm test
|
104 |
|