@keyvhq/memoize 1.6.26 → 1.6.28
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/LICENSE.md +22 -0
- package/README.md +22 -21
- package/package.json +3 -2
package/LICENSE.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2017 Luke Childs <lukechilds123@gmail.com> (https://lukechilds.co)
|
|
4
|
+
Copyright © 2021 Microlink <hello@microlink.io> (https://microlink.io)
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
|
14
|
+
all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -11,20 +11,21 @@ npm install --save @keyvhq/memoize
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
-
const memoize = require('@keyvhq/memoize')
|
|
14
|
+
const memoize = require('@keyvhq/memoize')
|
|
15
15
|
|
|
16
|
-
const memoizedRequest = memoize(request)
|
|
16
|
+
const memoizedRequest = memoize(request)
|
|
17
17
|
|
|
18
|
-
memoizedRequest('http://example.com').then(
|
|
19
|
-
memoizedRequest('http://example.com').then(
|
|
18
|
+
memoizedRequest('http://example.com').then(res => { /* from request */ })
|
|
19
|
+
memoizedRequest('http://example.com').then(res => { /* from cache */ })
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
You can pass a [keyv](https://github.com/microlinkhq/keyv) instance or options to be used as argument
|
|
22
|
+
You can pass a [keyv](https://github.com/microlinkhq/keyv) instance or options to be used as argument:
|
|
23
23
|
|
|
24
24
|
```js
|
|
25
|
-
memoize
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
const memoize = require('@keyvhq/memoize')
|
|
26
|
+
const Keyv = require('@keyvhq/core')
|
|
27
|
+
|
|
28
|
+
memoize(request, { store: new Keyv({ namespace: 'ssr' }) })
|
|
28
29
|
```
|
|
29
30
|
|
|
30
31
|
### Defining the key
|
|
@@ -34,14 +35,14 @@ By default the first argument of your function call is used as cache key.
|
|
|
34
35
|
You can pass a function to define how key will be defined. The key function will be called with the same arguments as the function.
|
|
35
36
|
|
|
36
37
|
```js
|
|
37
|
-
const sum = (n1, n2) => n1 + n2
|
|
38
|
+
const sum = (n1, n2) => n1 + n2
|
|
38
39
|
|
|
39
40
|
const memoized = memoize(sum, new Keyv(), {
|
|
40
41
|
key: (n1, n2) => `${n1}+${n2}`
|
|
41
|
-
})
|
|
42
|
+
})
|
|
42
43
|
|
|
43
44
|
// cached as { '1+2': 3 }
|
|
44
|
-
memoized(1, 2)
|
|
45
|
+
memoized(1, 2)
|
|
45
46
|
```
|
|
46
47
|
|
|
47
48
|
The library uses flood protection internally based on the result of the key.
|
|
@@ -53,10 +54,10 @@ This means you can make as many requests as you want simultaneously while being
|
|
|
53
54
|
Set `ttl` to a `number` for a static TTL value.
|
|
54
55
|
|
|
55
56
|
```js
|
|
56
|
-
const memoizedRequest = memoize(request, new Keyv(), { ttl: 60000 })
|
|
57
|
+
const memoizedRequest = memoize(request, new Keyv(), { ttl: 60000 })
|
|
57
58
|
|
|
58
59
|
// cached for 60 seconds
|
|
59
|
-
memoizedRequest('http://example.com')
|
|
60
|
+
memoizedRequest('http://example.com')
|
|
60
61
|
```
|
|
61
62
|
|
|
62
63
|
Set `ttl` to a `function` for a dynamic TTL value.
|
|
@@ -64,10 +65,10 @@ Set `ttl` to a `function` for a dynamic TTL value.
|
|
|
64
65
|
```js
|
|
65
66
|
const memoizedRequest = memoize(request, new Keyv(), {
|
|
66
67
|
ttl: (res) => res.statusCode === 200 ? 60000 : 0
|
|
67
|
-
})
|
|
68
|
+
})
|
|
68
69
|
|
|
69
70
|
// cached for 60 seconds only if response was 200 OK
|
|
70
|
-
memoizedRequest('http://example.com')
|
|
71
|
+
memoizedRequest('http://example.com')
|
|
71
72
|
```
|
|
72
73
|
|
|
73
74
|
### Stale support
|
|
@@ -77,19 +78,19 @@ Set `staleTtl` to any `number` of milliseconds.
|
|
|
77
78
|
If the `ttl` of a requested resource is below this staleness threshold we will still return the stale value but meanwhile asynchronously refresh the value.
|
|
78
79
|
|
|
79
80
|
```js
|
|
80
|
-
const memoizedRequest = memoize(request, new Keyv(), {
|
|
81
|
+
const memoizedRequest = memoize(request, new Keyv(), {
|
|
81
82
|
ttl: 60000,
|
|
82
83
|
staleTtl: 10000
|
|
83
|
-
})
|
|
84
|
+
})
|
|
84
85
|
|
|
85
86
|
// cached for 60 seconds
|
|
86
|
-
memoizedRequest('http://example.com')
|
|
87
|
+
memoizedRequest('http://example.com')
|
|
87
88
|
|
|
88
89
|
// … 55 seconds later
|
|
89
90
|
// Our cache will expire in 5 seconds.
|
|
90
91
|
// This is below the staleness threshold of 10 seconds.
|
|
91
92
|
// returns cached result + refresh cache on background
|
|
92
|
-
memoizedRequest('http://example.com')
|
|
93
|
+
memoizedRequest('http://example.com')
|
|
93
94
|
```
|
|
94
95
|
|
|
95
96
|
When the `staleTtl` option is set we won't delete expired items either. The same logic as above applies.
|
|
@@ -123,13 +124,13 @@ It defines how the get will be obtained.
|
|
|
123
124
|
The signature of the function should be a `String` to be used as key associated with the cache copy:
|
|
124
125
|
|
|
125
126
|
```js
|
|
126
|
-
key: ({req}) => req.url
|
|
127
|
+
key: ({ req }) => req.url
|
|
127
128
|
```
|
|
128
129
|
|
|
129
130
|
Just in case you need a more granular control, you can return an `Array`, where the second value determines the expiration behavior:
|
|
130
131
|
|
|
131
132
|
```js
|
|
132
|
-
key: ({req}) => [req.url, req.query.forceExpiration]
|
|
133
|
+
key: ({ req }) => [req.url, req.query.forceExpiration]
|
|
133
134
|
```
|
|
134
135
|
|
|
135
136
|
##### objectMode
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@keyvhq/memoize",
|
|
3
3
|
"description": "Memoize any function using Keyv as storage backend.",
|
|
4
4
|
"homepage": "https://keyv.js.org",
|
|
5
|
-
"version": "1.6.
|
|
5
|
+
"version": "1.6.28",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "hello@microlink.io",
|
|
@@ -46,5 +46,6 @@
|
|
|
46
46
|
"license": "MIT",
|
|
47
47
|
"publishConfig": {
|
|
48
48
|
"access": "public"
|
|
49
|
-
}
|
|
49
|
+
},
|
|
50
|
+
"gitHead": "69f95c41789d2a7fb83fbcc80e120e9f2a15274c"
|
|
50
51
|
}
|