simple-in-memory-cache 0.4.2 → 0.4.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/readme.md +71 -11
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "simple-in-memory-cache",
3
3
  "author": "ehmpathy",
4
4
  "description": "A simple in-memory cache, for nodejs and the browser, with time based expiration policies.",
5
- "version": "0.4.2",
5
+ "version": "0.4.3",
6
6
  "repository": "ehmpathy/simple-in-memory-cache",
7
7
  "homepage": "https://github.com/ehmpathy/simple-in-memory-cache",
8
8
  "keywords": [
package/readme.md CHANGED
@@ -3,36 +3,96 @@
3
3
  ![test](https://github.com/ehmpathy/simple-in-memory-cache/workflows/test/badge.svg)
4
4
  ![publish](https://github.com/ehmpathy/simple-in-memory-cache/workflows/publish/badge.svg)
5
5
 
6
- A simple in-memory cache, for nodejs and the browser, with time based expiration policies.
6
+ A simple, typed, in-memory cache for nodejs and the browser with time-based expiration policies.
7
7
 
8
- # Install
8
+ ## install
9
9
 
10
10
  ```sh
11
11
  npm install --save simple-in-memory-cache
12
12
  ```
13
13
 
14
- # Example
14
+ ## usage
15
15
 
16
- Quickly set and get from the cache:
16
+ ### set and get
17
17
 
18
18
  ```ts
19
19
  import { createCache } from 'simple-in-memory-cache';
20
20
 
21
21
  const { set, get } = createCache();
22
- set('meaning of life', 42);
23
- const meaningOfLife = get('meaning of life'); // returns 42
22
+ set('purpose of life', 42);
23
+ const purpose = get('purpose of life'); // returns 42
24
24
  ```
25
25
 
26
- Items in the cache live 5 minutes until expiration, by default.
26
+ ### expiration
27
27
 
28
- You can change this default when creating the cache:
28
+ items in the cache expire after 5 minutes by default.
29
+
30
+ change the default expiration on cache creation:
31
+
32
+ ```ts
33
+ const { set, get } = createCache({ expiration: { minutes: 10 } });
34
+ ```
35
+
36
+ override expiration per item:
29
37
 
30
38
  ```ts
31
- const { set, get } = createCache({ defaultSecondsUntilExpiration: 10 * 60 }); // updates the default seconds until expiration to 10 minutes
39
+ set('ice cream state', 'solid', { expiration: { seconds: 30 } });
32
40
  ```
33
41
 
34
- And you can also override this when setting an item:
42
+ set an item to never expire:
35
43
 
36
44
  ```ts
37
- set('acceleration due to gravity', 9.81, { secondsUntilExpiration: Infinity }); // gravity will not change, so we dont need to expire it
45
+ set('speed of light', 299792458, { expiration: null });
38
46
  ```
47
+
48
+ ### invalidation
49
+
50
+ invalidate a cached item with `set(key, undefined)`:
51
+
52
+ ```ts
53
+ set('purpose of life', 42);
54
+ get('purpose of life'); // returns 42
55
+
56
+ set('purpose of life', undefined);
57
+ get('purpose of life'); // returns undefined
58
+ ```
59
+
60
+ ### keys
61
+
62
+ list all non-expired keys in the cache:
63
+
64
+ ```ts
65
+ const { set, keys } = createCache();
66
+ set('a', 1);
67
+ set('b', 2);
68
+ keys(); // returns ['a', 'b']
69
+ ```
70
+
71
+ ## types
72
+
73
+ the cache is fully typed:
74
+
75
+ ```ts
76
+ import { createCache, SimpleInMemoryCache } from 'simple-in-memory-cache';
77
+
78
+ const cache: SimpleInMemoryCache<number> = createCache<number>();
79
+ cache.set('answer', 42);
80
+ const answer: number | undefined = cache.get('answer');
81
+ ```
82
+
83
+ ## api
84
+
85
+ ### `createCache<T>(options?)`
86
+
87
+ creates a new cache instance.
88
+
89
+ **options:**
90
+ - `expiration?: IsoDuration | null` — default expiration for items (default: `{ minutes: 5 }`)
91
+
92
+ **returns:** `SimpleInMemoryCache<T>`
93
+
94
+ ### `SimpleInMemoryCache<T>`
95
+
96
+ - `get(key: string): T | undefined` — retrieve an item (returns `undefined` if absent or expired)
97
+ - `set(key: string, value: T, options?): void` — store an item (or invalidate with `undefined`)
98
+ - `keys(): string[]` — list all non-expired keys