ioredis 4.15.1 → 4.16.0
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/Changelog.md +7 -0
- package/README.md +34 -25
- package/built/cluster/index.js +3 -2
- package/built/command.js +3 -0
- package/built/commander.js +2 -1
- package/built/script.js +5 -1
- package/package.json +1 -1
package/Changelog.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [4.16.0](https://github.com/luin/ioredis/compare/v4.15.1...v4.16.0) (2020-02-19)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* ability force custom scripts to be readOnly and execute on slaves ([#1057](https://github.com/luin/ioredis/issues/1057)) ([a24c3ab](https://github.com/luin/ioredis/commit/a24c3abcf4013e74e25424d2f6b91a2ae0de12b5))
|
|
7
|
+
|
|
1
8
|
## [4.15.1](https://github.com/luin/ioredis/compare/v4.15.0...v4.15.1) (2019-12-25)
|
|
2
9
|
|
|
3
10
|
|
package/README.md
CHANGED
|
@@ -76,23 +76,32 @@ $ npm install ioredis
|
|
|
76
76
|
## Basic Usage
|
|
77
77
|
|
|
78
78
|
```javascript
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
const Redis = require("ioredis");
|
|
80
|
+
const redis = new Redis(); // uses defaults unless given configuration object
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
// ioredis supports all Redis commands:
|
|
83
|
+
redis.set("foo", "bar"); // returns promise which resolves to string, "OK"
|
|
84
|
+
|
|
85
|
+
// the format is: redis[SOME_REDIS_COMMAND_IN_LOWERCASE](ARGUMENTS_ARE_JOINED_INTO_COMMAND_STRING)
|
|
86
|
+
// the js: ` redis.set("mykey", "Hello") ` is equivalent to the cli: ` redis> SET mykey "Hello" `
|
|
87
|
+
|
|
88
|
+
// ioredis supports the node.js callback style
|
|
83
89
|
redis.get("foo", function(err, result) {
|
|
84
|
-
|
|
90
|
+
if (err) {
|
|
91
|
+
console.error(err);
|
|
92
|
+
} else {
|
|
93
|
+
console.log(result); // Promise resolves to "bar"
|
|
94
|
+
}
|
|
85
95
|
});
|
|
86
|
-
redis.del("foo");
|
|
87
96
|
|
|
88
|
-
// Or
|
|
97
|
+
// Or ioredis returns a promise if the last argument isn't a function
|
|
89
98
|
redis.get("foo").then(function(result) {
|
|
90
|
-
console.log(result);
|
|
99
|
+
console.log(result); // Prints "bar"
|
|
91
100
|
});
|
|
92
101
|
|
|
93
|
-
//
|
|
94
|
-
redis.
|
|
95
|
-
redis.
|
|
102
|
+
// Most responses are strings, or arrays of strings
|
|
103
|
+
redis.zadd("sortedSet", 1, "one", 2, "dos", 4, "quatro", 3, "three")
|
|
104
|
+
redis.zrange("sortedSet", 0, 2, "WITHSCORES").then(res => console.log(res)); // Promise resolves to ["one", "1", "dos", "2", "three", "3"] as if the command was ` redis> ZRANGE sortedSet 0 2 WITHSCORES `
|
|
96
105
|
|
|
97
106
|
// All arguments are passed directly to the redis server:
|
|
98
107
|
redis.set("key", 100, "EX", 10);
|
|
@@ -832,21 +841,21 @@ cluster.get("foo", function(err, res) {
|
|
|
832
841
|
ioredis will try to reconnect to the startup nodes from scratch after the specified delay (in ms). Otherwise, an error of "None of startup nodes is available" will be returned.
|
|
833
842
|
The default value of this option is:
|
|
834
843
|
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
844
|
+
```javascript
|
|
845
|
+
function (times) {
|
|
846
|
+
var delay = Math.min(100 + times * 2, 2000);
|
|
847
|
+
return delay;
|
|
848
|
+
}
|
|
849
|
+
```
|
|
850
|
+
|
|
851
|
+
It's possible to modify the `startupNodes` property in order to switch to another set of nodes here:
|
|
852
|
+
|
|
853
|
+
```javascript
|
|
854
|
+
function (times) {
|
|
855
|
+
this.startupNodes = [{ port: 6790, host: '127.0.0.1' }];
|
|
856
|
+
return Math.min(100 + times * 2, 2000);
|
|
857
|
+
}
|
|
858
|
+
```
|
|
850
859
|
|
|
851
860
|
- `dnsLookup`: Alternative DNS lookup function (`dns.lookup()` is used by default). It may be useful to override this in special cases, such as when AWS ElastiCache used with TLS enabled.
|
|
852
861
|
- `enableOfflineQueue`: Similar to the `enableOfflineQueue` option of `Redis` class.
|
package/built/cluster/index.js
CHANGED
|
@@ -432,8 +432,9 @@ class Cluster extends events_1.EventEmitter {
|
|
|
432
432
|
}
|
|
433
433
|
let to = this.options.scaleReads;
|
|
434
434
|
if (to !== "master") {
|
|
435
|
-
const isCommandReadOnly =
|
|
436
|
-
commands.
|
|
435
|
+
const isCommandReadOnly = command.isReadOnly ||
|
|
436
|
+
(commands.exists(command.name) &&
|
|
437
|
+
commands.hasFlag(command.name, "readonly"));
|
|
437
438
|
if (!isCommandReadOnly) {
|
|
438
439
|
to = "master";
|
|
439
440
|
}
|
package/built/command.js
CHANGED
package/built/commander.js
CHANGED
|
@@ -65,10 +65,11 @@ Commander.prototype.send_command = Commander.prototype.call;
|
|
|
65
65
|
* @param {object} definition
|
|
66
66
|
* @param {string} definition.lua - the lua code
|
|
67
67
|
* @param {number} [definition.numberOfKeys=null] - the number of keys.
|
|
68
|
+
* @param {boolean} [definition.readOnly=false] - force this script to be readonly so it executes on slaves as well.
|
|
68
69
|
* If omit, you have to pass the number of keys as the first argument every time you invoke the command
|
|
69
70
|
*/
|
|
70
71
|
Commander.prototype.defineCommand = function (name, definition) {
|
|
71
|
-
var script = new script_1.default(definition.lua, definition.numberOfKeys, this.options.keyPrefix);
|
|
72
|
+
var script = new script_1.default(definition.lua, definition.numberOfKeys, this.options.keyPrefix, definition.readOnly);
|
|
72
73
|
this.scriptsSet[name] = script;
|
|
73
74
|
this[name] = generateScriptingFunction(script, "utf8");
|
|
74
75
|
this[name + "Buffer"] = generateScriptingFunction(script, null);
|
package/built/script.js
CHANGED
|
@@ -5,10 +5,11 @@ const promiseContainer_1 = require("./promiseContainer");
|
|
|
5
5
|
const command_1 = require("./command");
|
|
6
6
|
const standard_as_callback_1 = require("standard-as-callback");
|
|
7
7
|
class Script {
|
|
8
|
-
constructor(lua, numberOfKeys = null, keyPrefix = "") {
|
|
8
|
+
constructor(lua, numberOfKeys = null, keyPrefix = "", readOnly = false) {
|
|
9
9
|
this.lua = lua;
|
|
10
10
|
this.numberOfKeys = numberOfKeys;
|
|
11
11
|
this.keyPrefix = keyPrefix;
|
|
12
|
+
this.readOnly = readOnly;
|
|
12
13
|
this.sha = crypto_1.createHash("sha1")
|
|
13
14
|
.update(lua)
|
|
14
15
|
.digest("hex");
|
|
@@ -20,6 +21,9 @@ class Script {
|
|
|
20
21
|
if (this.keyPrefix) {
|
|
21
22
|
options.keyPrefix = this.keyPrefix;
|
|
22
23
|
}
|
|
24
|
+
if (this.readOnly) {
|
|
25
|
+
options.readOnly = true;
|
|
26
|
+
}
|
|
23
27
|
const evalsha = new command_1.default("evalsha", [this.sha].concat(args), options);
|
|
24
28
|
evalsha.isCustomCommand = true;
|
|
25
29
|
const result = container.sendCommand(evalsha);
|