UNPKG

2.89 kBMarkdownView Raw
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
5A 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
14Create an instance of the cache optionally giving it a folder location to store files within.
15
16```js
17import Cache from "file-system-cache";
18
19const 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
29The 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
31The optional `ttl` ("time to live") allows you to set a default expiration for the cache key, in seconds. For example if you
32set 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)
35Retrieves the contents of a cached file.
36
37```js
38cache.get("foo")
39 .then(result => /* Success */)
40 .catch(err => /* Fail */);
41```
42
43or use modern `async/await` syntactic sugar of course:
44
45```js
46const value = await cache.get("foo");
47```
48
49Use `getSync` for a synchronous version.
50Pass a `defaultValue` parameter to use if the key does not exist within the cache.
51
52
53### set(key, value, ttl)
54Write a value to the cache.
55
56```js
57/* This will apply the default TTL to this cache key */
58cache.set("foo", "...value...")
59 .then(result => /* Success */)
60
61/* This will set a specific TTL for this cache key */
62cache.set("bar", "...baz", 60)
63 .then(result => /* Success */)
64```
65
66Value types are stored and respected on subsequent calls to `get`. For examples, passing in Object will return that in its parsed object state.
67
68Use `setSync` for a synchronous version.
69
70### remove(key)
71Deletes the specified cache item from the file-system.
72```js
73cache.remove("foo")
74 .then(() => /* Removed */)
75```
76
77### clear()
78Clears all cached items from the file-system.
79```js
80cache.clear()
81 .then(() => /* All items deleted */)
82```
83
84
85### save()
86Saves (sets) several items to the cache in one operation.
87```js
88cache.save([{ key:"one", value:"hello" }, { key:"two", value:222 }])
89 .then(result => /* All items saved. */)
90```
91
92### load()
93Loads all files within the cache's namespace.
94```js
95cache.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