@@ -104,78 +104,45 @@ $ npm install ioredis
104
104
## Basic Usage
105
105
106
106
``` javascript
107
+ // Import ioredis.
108
+ // You can also use `import Redis from "ioredis"`
109
+ // if your project is an ESM module or a TypeScript project.
107
110
const Redis = require (" ioredis" );
108
111
109
- // First, you need to create a Redis instance.
110
- // We are going to cover how to specify host, port, and other connection
111
- // options soon.
112
+ // Create a Redis instance.
113
+ // By default, it will connect to localhost:6379.
114
+ // We are going to cover how to specify connection options soon.
112
115
const redis = new Redis ();
113
116
114
- // Invoke the SET command. This is equivalent to the cli `redis> SET name Bob`:
115
- redis .set (" name" , " Bob" ); // Returns a Promise
117
+ redis .set (" mykey" , " value" ); // Returns a promise which resolves to "OK" when the command succeeds.
116
118
117
- // ioredis provides two kind of APIs, Node.js callback and Promise.
118
- // 1. You can pass a callback as the last parameter. It will be called when
119
- // we get a response from the Redis server:
120
- redis .get (" name" , (err , value ) => {
119
+ // ioredis supports the node.js callback style
120
+ redis .get (" mykey" , (err , result ) => {
121
121
if (err) {
122
122
console .error (err);
123
123
} else {
124
- console .log (value ); // "Bob "
124
+ console .log (result ); // Prints "value "
125
125
}
126
126
});
127
127
128
- // 2. Additionally, every command method returns a Promise
129
- // representing the server response:
130
- redis .get (" name" ).then (
131
- (value ) => {
132
- console .log (value);
133
- },
134
- (err ) => {
135
- console .error (err);
136
- }
137
- );
138
-
139
- //
140
-
141
- // Every
142
-
143
- async function main () {
144
- await redis .set (" mykey" , " Hello, World!" );
145
- const value = await redis .get (" mykey" ); // value === "Hello, World!"
146
- }
147
-
148
- main ();
149
- ```
150
-
151
- // ioredis supports all Redis commands:
152
- redis.set("foo", "bar"); // returns promise which resolves to string, "OK"
153
-
154
- // the format is: redis[ REDIS_COMMAND_NAME_IN_LOWERCASE] ( ARGUMENTS_ARE_JOINED_INTO_COMMAND_STRING )
155
- // the js: ` redis.set("mykey", "Hello") ` is equivalent to the cli: ` redis> SET mykey "Hello" `
156
-
157
- // ioredis supports the Node.js callback style
158
- redis.get("foo", (err, result) => {
159
- if (err) {
160
- console.error(err);
161
- } else {
162
- console.log(result); // Promise resolves to "bar"
163
- }
164
- });
165
-
166
128
// Or ioredis returns a promise if the last argument isn't a function
167
- redis.get("foo ").then((result) => {
168
- console.log(result); // Prints "bar "
129
+ redis .get (" mykey " ).then ((result ) => {
130
+ console .log (result); // Prints "value "
169
131
});
170
132
171
- // Most responses are strings, or arrays of strings
172
133
redis .zadd (" sortedSet" , 1 , " one" , 2 , " dos" , 4 , " quatro" , 3 , " three" );
173
- 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 `
134
+ redis .zrange (" sortedSet" , 0 , 2 , " WITHSCORES" ).then ((elements ) => {
135
+ // ["one", "1", "dos", "2", "three", "3"] as if the command was `redis> ZRANGE sortedSet 0 2 WITHSCORES`
136
+ console .log (elements);
137
+ });
174
138
175
- // All arguments are passed directly to the redis server:
176
- redis.set("key", 100, "EX", 10);
139
+ // All arguments are passed directly to the redis server,
140
+ // so technically ioredis supports all Redis commands.
141
+ // The format is: redis[SOME_REDIS_COMMAND_IN_LOWERCASE](ARGUMENTS_ARE_JOINED_INTO_COMMAND_STRING)
142
+ // so the following statement is equivalent to the CLI: `redis> SET mykey hello EX 10`
143
+ redis .set (" mykey" , " hello" , " EX" , 10 );
144
+ ```
177
145
178
- ````
179
146
180
147
See the ` examples/ ` folder for more examples.
181
148
@@ -193,9 +160,9 @@ new Redis("/tmp/redis.sock");
193
160
new Redis ({
194
161
port: 6379 , // Redis port
195
162
host: " 127.0.0.1" , // Redis host
196
- family: 4 , // 4 (IPv4) or 6 (IPv6)
197
- password: "auth ",
198
- db: 0,
163
+ username : " default " , // needs Redis >= 6
164
+ password: " my-top-secret " ,
165
+ db: 0 , // Defaults to 0
199
166
});
200
167
````
201
168
@@ -206,10 +173,8 @@ You can also specify connection options as a [`redis://` URL](http://www.iana.or
206
173
new Redis("redis://:authpassword@127.0.0.1:6380/4");
207
174
208
175
// Username can also be passed via URI.
209
- // It's worth to noticing that for compatibility reasons `allowUsernameInURI`
210
- // need to be provided, otherwise the username part will be ignored.
211
176
new Redis(
212
- " redis://username:authpassword@127.0.0.1:6380/4?allowUsernameInURI=true "
177
+ "redis://username:authpassword@127.0.0.1:6380/4"
213
178
);
214
179
` ` `
215
180
0 commit comments