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
Copy file name to clipboardexpand all lines: README.md
+95-26
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,18 @@
3
3
* A [redis](https://github.com/mranney/node_redis) plugin for [cache-service](https://github.com/jpodwys/cache-service)
4
4
* AND a standalone redis wrapper
5
5
6
+
#### Features
7
+
8
+
* Background refresh
9
+
* Cache objects--automatic serialization/deserialization of values
10
+
* Easy config handling--pass the name of an ENV OR the `redis` connection config string OR all `redis` connection params
11
+
* Robust API
12
+
* Built-in logging with a `verbose` flag.
13
+
* Compatible with `cache-service` and `superagent-cache`
14
+
* Public access to the underlying `node-cache` instance
15
+
* A more logical API--`.mset()` takes an object of keys and values rather than a comma-separated argument list
16
+
*`.mset()` allows you to set expirations on a per key, per function call, and/or per `cache-service-redis` instance basis (Vanilla `redis` does not let `.mset()` set expirations at all)
17
+
6
18
# Basic Usage
7
19
8
20
Require and instantiate
@@ -18,18 +30,10 @@ Cache!
18
30
redisCache.set('key', 'value');
19
31
```
20
32
21
-
# Benefits of Using `cache-service-redis`
22
-
23
-
If you're using `cache-service-redis` with `cache-service`, the benefits are obvious. However, it's also a great standalone `redis` wrapper! Here's why:
24
-
25
-
* You can set JavaScript objects as values and `cache-service-redis` automatically handles serialization/deserialization.
26
-
* A more logical API--`.mset()` takes an object of keys and values rather than a comma-separated argument list.
27
-
*`.mset()` allows you to set expirations on a per key, per function call, and/or per `cache-service-redis` instance basis (vanilla `redis` does not let `.mset()` set expirations at all).
28
-
* Built-in logging with a `verbose` flag.
29
-
* Easy config handling--pass the name of an ENV OR the `redis` connection config string OR all `redis` connection params.
30
-
31
33
# Cache Module Configuration Options
32
34
35
+
`cache-service-redis`'s constructor takes an optional config object with any number of the following properties:
36
+
33
37
## type
34
38
35
39
An arbitrary identifier you can assign so you know which cache is responsible for logs and errors.
@@ -45,15 +49,6 @@ The expiration to include when executing cache set commands. Can be overridden v
45
49
* default: 900
46
50
* measure: seconds
47
51
48
-
## verbose
49
-
50
-
> When used with `cache-service`, this property is overridden by `cache-service`'s `verbose` value.
51
-
52
-
When false, `cache-service-redis` will log only errors. When true, `cache-service-redis` will log all activity (useful for testing and debugging).
53
-
54
-
* type: boolean
55
-
* default: false
56
-
57
52
## redisData
58
53
59
54
This is the most generic way to pass in your redis configuraiton options.
@@ -82,6 +77,38 @@ If you have a redis URL contained in an env variable (in process.env[redisEnv]),
82
77
83
78
* type: string
84
79
80
+
## backgroundRefreshInterval
81
+
82
+
How frequently should all background refresh-enabled keys be scanned to determine whether they should be refreshed. For a more thorough explanation on `background refresh`, see the [Using Background Refresh](#using-background-refresh) section.
83
+
84
+
* type: int
85
+
* default: 60000
86
+
* measure: milliseconds
87
+
88
+
## backgroundRefreshMinTtl
89
+
90
+
The maximum ttl a scanned background refresh-enabled key can have without triggering a refresh. This number should always be greater than `backgroundRefreshInterval`.
91
+
92
+
* type: int
93
+
* default: 70000
94
+
* measure: milliseconds
95
+
96
+
## backgroundRefreshIntervalCheck
97
+
98
+
Whether to throw an exception if `backgroundRefreshInterval` is greater than `backgroundRefreshMinTtl`. Setting this property to false is highly discouraged.
99
+
100
+
* type: boolean
101
+
* default: true
102
+
103
+
## verbose
104
+
105
+
> When used with `cache-service`, this property is overridden by `cache-service`'s `verbose` value.
106
+
107
+
When false, `cache-service-redis` will log only errors. When true, `cache-service-redis` will log all activity (useful for testing and debugging).
108
+
109
+
* type: boolean
110
+
* default: false
111
+
85
112
# API
86
113
87
114
Although this is a redis wrapper, its API differs in some small cases from redis's own API both because the redis API is sometimes dumb and because all `cache-service`-compatible cache modules match [`cache-service`'s API](https://github.com/jpodwys/cache-service#api).
@@ -104,16 +131,19 @@ Retrieve the values belonging to a series of keys. If a key is not found, it wil
> See the [Using Background Refresh](#using-background-refresh) section for more about the `refresh` and `callback` params.
108
137
109
138
Set a value by a given key.
110
139
111
140
* key: type: string
112
141
* callback: type: function
113
142
* expiration: type: int, measure: seconds
143
+
* refresh: type: function
114
144
* callback: type: function
115
145
116
-
## .mset(obj[, expiration, callback])
146
+
## .mset(obj, [expiration], [callback])
117
147
118
148
Set multiple values to multiple keys
119
149
@@ -125,7 +155,7 @@ This function exposes a heirarchy of expiration values as follows:
125
155
* If an object with both `cacheValue` and `expiration` as properties is not present, the `expiration` provided to the `.mset()` argument list will be used.
126
156
* If neither of the above is provided, each cache's `defaultExpiration` will be applied.
127
157
128
-
## .del(keys[, callback (err, count)])
158
+
## .del(keys, [callback (err, count)])
129
159
130
160
Delete a key or an array of keys and their associated values.
131
161
@@ -140,11 +170,50 @@ Flush all keys and values.
140
170
141
171
* callback: type: function
142
172
143
-
# More Redis Methods
173
+
## .db
174
+
175
+
This is the underlying [`node_redis` instance](https://github.com/mranney/node_redis). If needed, you can access `redis` functions I haven't abstracted.
176
+
177
+
# Using Background Refresh
178
+
179
+
With a typical cache setup, you're left to find the perfect compromise between having a long expiration so that users don't have to suffer through the worst case load time, and a short expiration so data doesn't get stale. `cache-service-redis` eliminates the need to worry about users suffering through the longest wait time by automatically refreshing keys for you. Here's how it works:
180
+
181
+
> `cache-service-redis` employs an intelligent background refresh algorithm that makes it so only one dyno executes a background refresh for any given key. You should feel confident that you will not encounter multiple dynos refreshing a single key.
182
+
183
+
#### How do I turn it on?
144
184
145
-
If you need access to one of redis's other functions, you can get at the underlying [`node_redis` instance](https://github.com/mranney/node_redis) by tapping into the `.db` property like so:
185
+
By default, background refresh is off. It will turn itself on the first time you pass a `refresh` param to `.set()`.
186
+
187
+
#### Configure
188
+
189
+
There are three options you can manipulate. See the API section for more information about them.
190
+
191
+
*`backgroundRefreshInterval`
192
+
*`backgroundRefreshMinTtl`
193
+
*`backgroundRefreshIntervalCheck`
194
+
195
+
#### Use
196
+
197
+
Background refresh is exposed via the `.set()` command as follows:
If you want to pass `refresh`, you must also pass `cb` because if only four params are passed, `cache-service-redis` will assume the fourth param is `cb`.
204
+
205
+
#### The Refresh Param
206
+
207
+
###### refresh(key, cb(err, response))
208
+
209
+
* key: type: string: this is the key that is being refreshed
210
+
* cb: type: function: you must trigger this function to pass the data that should replace the current key's value
211
+
212
+
The `refresh` param MUST be a function that accepts `key` and a callback function that accepts `err` and `response` as follows:
0 commit comments