You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 28, 2025. It is now read-only.
refactor: rewrite, use memcached-elasticache & drop Node 8
- [x] chore: drop Node 8 support. Min required version is [email protected]
- [x] refactor(deps): remove `lodash` and `bluebird`
- [x] refactor(cache): rewrite `Cache` with ES6 class syntax
- [x] refactor: `cache.set` no longer returns a value
- [x] refactor: make inofficial cache api private
- [x] fix: handle functions or promises as `cache.set` values as described in README.md
- [x] fix: remove deprecation message, that warned when backend `name` was not a string. It will throw a type error now.
- [x] deps: use `memcached-elasticache` instead of `memcached`
- [x] chore(deps): upgrade dependencies
- [x] test: add additional test to increase test coverage
- [x] docs(README): update API section
### BREAKING CHANGES:
#### General
- Dropped Node 8.x. Use Node 10.13 or higher.
- Uses `memcached-elasticache` with AWS support instead of `memcached`. See https://github.com/jkehres/memcached-elasticache#readme
- API functions no longer support callbacks.
- API functions use native Promises instead of `Bluebird`.
- `cached()` needs to be called with a string argument as name. If the `name` argument is undefined, it will fallback to `"default"`.
#### `Cache`
- `Cache.setBackend()` no longer returns the created backend.
- `Cache.getWrapped()` is removed.
- Cache backend APIs need to be promise based.
- Cache private function have been renamed.
- The following function are now private:
- `Cache.applyPrefix`
- `Cache.end`
- `Cache.prepareOptions`
- `Cache.setBackend`
#### `cache`
- `cache.set()`, `cache.get()`, `cache.getOrElse()`, `cache.unset()` no longer support the callback argument. Use them as promise.
- `cache.set()` no longer returns a value.
- `cache.set(key, value)` accepts functions and promises as value.
#### Backends
- Any client passed with the memcached backend options has to be an instance of `Memcached`.
- Backend setters no longer return a value.
- backend `addType()` does not return backend class anymore.
A simple caching library, inspired by the [Play cache API](http://www.playframework.com/documentation/2.2.x/ScalaCache) and biased towards [showing stale data instead of dog piling](http://highscalability.com/strategy-break-memcache-dog-pile).
3
+
A simple caching library, inspired by the [Play cache API](http://www.playframework.com/documentation/2.2.x/ScalaCache)
4
+
and biased towards [showing stale data instead of dog piling](http://highscalability.com/strategy-break-memcache-dog-pile).
4
5
The interface only exposes very limited functionality, there's no multi-get or deletion of cached data.
5
6
The library is designed to support different caching backends, though right now only memcached is implemented.
6
7
@@ -17,99 +18,97 @@ More detailed API docs are in the next section.
17
18
### Getting and setting
18
19
19
20
```js
20
-
cached =require('cached');
21
+
constcached=require('cached');
21
22
22
-
kittens =cached('kittens');
23
+
constkittens=cached('kittens');
23
24
24
-
// Set a key using a plain value
25
-
kittens.set('my.key', 'Hello World');
25
+
asyncfunctioncacheKittens() {
26
26
27
-
// Set a key using a lazily created promise (or value)
*__Caveat:__`get()` will return a reference to the stored value. Mutating the returned value will affect the value in the cache.*
91
+
*__Caveat:__`get()` will return a reference to the stored value. Mutating the returned value will affect the
92
+
value in the cache.*
95
93
96
94
#### Example
97
95
98
96
```js
99
97
cached('myStuff', { backend: {
100
-
type:'memory'
98
+
type:'memory',
101
99
}});
102
100
```
103
101
104
102
### Noop
105
103
106
-
Doesn't store data at all. All `set` operations succeed and `get` operations behave as if the value were not found in the cache.
104
+
Doesn't store data at all. All `set` operations succeed and `get` operations behave as if the value were not
105
+
found in the cache.
107
106
108
107
#### Examples
109
108
110
109
```js
111
110
cached('myStuff', { backend: {
112
-
type:'noop'
111
+
type:'noop',
113
112
}});
114
113
```
115
114
@@ -124,14 +123,18 @@ cached('myStuff');
124
123
125
124
Creates a new named cache or returns a previously initialized cache.
126
125
127
-
***name:** (required) A meaningful name for what is in the cache. This will also be used as a key-prefix. If the name is `"cars"`, all keys will be prefixed with `"cars:"`
126
+
***name:** (required) A meaningful name for what is in the cache. This will also be used as a key-prefix. If the
127
+
name is `"cars"`, all keys will be prefixed with `"cars:"`
128
128
***options:** (optional)
129
-
***backend:** An object that has at least a `type` property. If no backend is configured, the cache will run in "noop"-mode, not caching anything. All other properties are forwarded to the backend, see [using different backends](#supported-backends) for which backend types exist and what options they support.
129
+
***backend:** An object that has at least a `type` property. If no backend is configured, the cache will run in
130
+
"noop"-mode, not caching anything. All other properties are forwarded to the backend, see
131
+
[using different backends](#supported-backends) for which backend types exist and what options they support.
130
132
***defaults:** Defaults to apply for all cache operations. See `Cache.setDefaults`
131
133
132
134
### cached.createCache(options) -> Cache
133
135
134
-
This allows you to circumvent the global named caches. The options are the same as above, just `name` is also part of the `options` object when using this function.
136
+
This allows you to circumvent the global named caches. The options are the same as above, just `name` is also part
137
+
of the `options` object when using this function.
135
138
136
139
### cached.dropNamedCache(name: string) -> cached
137
140
@@ -143,67 +146,116 @@ Drop all named caches.
143
146
144
147
### cached.deferred(fn) -> () -> Promise
145
148
146
-
Convert a node-style function that takes a callback as its first parameter into a parameterless function that generates a promise.
147
-
In other words: this is what you'd want to wrap your node-style functions in when using them as value arguments to `set` or `getOrElse`.
148
-
149
-
#### Example:
149
+
Convert a node-style function that takes a callback as its first parameter into a parameterless function that
150
+
generates a promise. In other words: this is what you'd want to wrap your node-style functions in when using them
151
+
as value arguments to `set` or `getOrElse`.
150
152
153
+
**Example:**
151
154
```js
152
-
var f =cached.deferred(function(cb) {
153
-
var req =require('http').get(myUrl, function(res) {
155
+
consthttp=require('http');
156
+
157
+
constcache=cached('myStuff');
158
+
constf=cached.deferred(cb=> {
159
+
constreq=http.get(myUrl, res=> {
154
160
cb(null, res.statusCode);
155
161
});
156
162
req.once('error', cb);
157
163
});
164
+
158
165
// f can now be called and the return value will be a promise
// More importantly it can be passed into cache.set
161
-
cached('myStuff').set('someKey', f);
169
+
awaitcache.set('someKey', f);
162
170
```
163
171
164
172
### Cache.setDefaults(defaults) -> Cache.defaults
165
173
166
174
Extends the current defaults with the provided defaults.
167
175
The two important ones are `freshFor` and `expire`:
168
176
169
-
*`expire` is the time in seconds after which a value should be deleted from the cache (or whatever expiring natively means for the backend). Usually you'd want this to be `0` (never expire).
170
-
*`freshFor` is the time in seconds after which a value should be replaced. Replacing the value is done in the background and while the new value is generated (e.g. data is fetched from some service) the stale value is returned. Think of `freshFor` as a smarter `expire`.
177
+
*`expire` is the time in seconds after which a value should be deleted from the cache
178
+
(or whatever expiring natively means for the backend). Usually you'd want this to be `0` (never expire).
179
+
*`freshFor` is the time in seconds after which a value should be replaced. Replacing the value is done in
180
+
the background and while the new value is generated (e.g. data is fetched from some service) the stale
181
+
value is returned. Think of `freshFor` as a smarter `expire`.
171
182
*`timeout` is the maximum time in milliseconds to wait for cache operations to complete.
172
183
Configuring a timeout ensures that all `get`, `set`, and `unset` operations fail fast.
173
-
Otherwise there will be situations where one of the cache hosts goes down and reads hang for minutes while the memcached client retries to establish a connection.
184
+
Otherwise, there will be situations where one of the cache hosts goes down and reads hang for minutes while
185
+
the memcached client retries to establish a connection.
174
186
It's **highly** recommended to set a timeout.
175
-
If `timeout` is left `undefined`, no timeout will be set and the operations will only fail once the underlying client, e.g. [`memcached`](https://github.com/3rd-Eden/memcached), gave up.
187
+
If `timeout` is left `undefined`, no timeout will be set, and the operations will only fail once the
188
+
underlying client, e.g. [`memcached`](https://github.com/3rd-Eden/memcached), gave up.
189
+
190
+
### Cache.get(key) -> Promise\<value\>
191
+
192
+
Cache retrieve operation. `key` has to be a string.
193
+
Cache misses are generally treated the same as retrieving `null`, errors should only be caused by transport
194
+
errors and connection problems.
195
+
If you want to cache `null`/`undefined` (e.g. 404 responses), you may want to wrap it or choose a different
Cache store operation. `key` has to be a string, for possible `opts` see `Cache.setDefaults`.
180
232
The value can be any of the following:
181
233
182
-
a. Anything that can be converted to JSON
183
-
b. A Promise of (a)
184
-
c. A function returning (a) or (b)
234
+
a) Anything that can be converted to JSON<br>
235
+
b) A Promise of (a)<br>
236
+
c) A function returning (a) or (b)<br>
185
237
186
-
The callback will be called with the resolved value, following node conventions (error, value).
238
+
**Examples:**
239
+
```js
240
+
// with a value
241
+
awaitcache.set('foo', 'bar');
187
242
188
-
### Cache.get(key, cb) -> Promise[Value]
243
+
// with a function returning a value
244
+
awaitcache.set('foo', () => { return'bar' });
189
245
190
-
Cache retrieve operation.
191
-
`key` has to be a string.
192
-
Cache misses are generally treated the same as retrieving `null`, errors should only be caused by transport errors and connection problems.
193
-
If you want to cache `null`/`undefined` (e.g. 404 responses), you may want to wrap it or choose a different value, like `false`, to represent this condition.
0 commit comments