@@ -11,7 +11,7 @@ A minimal JS cache. Like using an object to store keys and values, except it won
11
11
12
12
A plain Javascript object is often good enough for simple key-value caching.
13
13
14
- The problem is that a simple object cache can grow forever. This library adds a size limit, plus ` maxCacheTime ` and
14
+ The problem is that a plain object cache can grow forever. This library adds a size limit, plus ` maxCacheTime ` and
15
15
smarter removal of old items.
16
16
17
17
## Example
@@ -23,8 +23,9 @@ const recentResults = LimitedCache({
23
23
maxCacheSize: 100 ,
24
24
});
25
25
recentResults .set (' abc' , thingToSave);
26
- recentResults .get (' abc' ); // => thingToSave
27
- recentResults .has (' abc' ); // => true
26
+ recentResults .get (' abc' );
27
+ recentResults .has (' abc' );
28
+ recentResults .getAll ();
28
29
recentResults .reset ();
29
30
```
30
31
@@ -47,7 +48,7 @@ const cache = useLimitedCacheObject();
47
48
const cache2 = useLimitedCacheObject ({ maxCacheTime: 60000 }, [depsCanGoHere]);
48
49
```
49
50
50
- Low-level functions using plain objects, if you need to stay serializable:
51
+ Low-level functions using plain objects, if you need to stay serializable or want to store offline :
51
52
52
53
``` javascript
53
54
const reduxState = {
@@ -65,6 +66,14 @@ return {
65
66
};
66
67
```
67
68
69
+ Generics for Typescript:
70
+
71
+ ``` typescript
72
+ const stringCache = LimitedCache <string >();
73
+ const myClassCache = useLimitedCache <SomeClass >();
74
+ const offlineCacheMeta = lowLevelInit <SomeObjectShape >();
75
+ ```
76
+
68
77
## Install and Import
69
78
70
79
` npm install limited-cache ` or ` yarn add limited-cache `
@@ -109,7 +118,7 @@ These functions are grouped together as `limitedCacheUtil`. The other interfaces
109
118
- ` reset(cacheMeta) `
110
119
- ` setOptions(cacheMeta, options) ` - you can update options anytime
111
120
112
- You can also import these functions individually, if you want to optimize tree-shaking:
121
+ You can also import these functions individually, if you want to optimize tree-shaking and minification :
113
122
114
123
``` javascript
115
124
import { lowLevelInit , lowLevelGet , lowLevelSet } from ' limited-cache' ;
@@ -119,11 +128,11 @@ import { lowLevelInit, lowLevelGet, lowLevelSet } from 'limited-cache';
119
128
120
129
** Immutable?**
121
130
122
- The cache itself is, but the low-level cacheMeta is persistent.
131
+ The cache itself is, but the low-level cacheMeta is persistent/mutated .
123
132
124
133
** API for bulk operations?**
125
134
126
- Only ` reset ` . The other actions aren't as optimizable, so they're omitted to keep this small.
135
+ Only ` reset ` and ` getAll ` . The other actions aren't as optimizable, so they're omitted to keep this small.
127
136
128
137
** When are old items removed?**
129
138
@@ -134,4 +143,4 @@ When new items are added, or if you try to `get` an item that has expired.
134
143
No: For performance it only tracks by ` set ` time.
135
144
136
145
If you want items to expire based on when they were last accessed (instead of when they were set), you can ` set `
137
- the value that already exists: only the timestamp will be updated.
146
+ the value that already exists: only the timestamp will be updated, so performance won't suffer .
0 commit comments