@ohos-ports/redis 6.2.1-beta.1

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/README.md ADDED
@@ -0,0 +1,341 @@
1
+ # Node-Redis
2
+
3
+ [![Tests](https://img.shields.io/github/actions/workflow/status/redis/node-redis/tests.yml?branch=master)](https://github.com/redis/node-redis/actions/workflows/tests.yml)
4
+ [![Coverage](https://codecov.io/gh/redis/node-redis/branch/master/graph/badge.svg?token=xcfqHhJC37)](https://codecov.io/gh/redis/node-redis)
5
+ [![License](https://img.shields.io/github/license/redis/node-redis.svg)](https://github.com/redis/node-redis/blob/master/LICENSE)
6
+
7
+ [![Discord](https://img.shields.io/discord/697882427875393627.svg?style=social&logo=discord)](https://discord.gg/redis)
8
+ [![Twitch](https://img.shields.io/twitch/status/redisinc?style=social)](https://www.twitch.tv/redisinc)
9
+ [![YouTube](https://img.shields.io/youtube/channel/views/UCD78lHSwYqMlyetR0_P4Vig?style=social)](https://www.youtube.com/redisinc)
10
+ [![Twitter](https://img.shields.io/twitter/follow/redisinc?style=social)](https://twitter.com/redisinc)
11
+
12
+ node-redis is a modern, high performance [Redis](https://redis.io) client for Node.js.
13
+
14
+ ## How do I Redis?
15
+
16
+ [Learn for free at Redis University](https://university.redis.com/)
17
+
18
+ [Build faster with the Redis Launchpad](https://launchpad.redis.com/)
19
+
20
+ [Try the Redis Cloud](https://redis.com/try-free/)
21
+
22
+ [Dive in developer tutorials](https://developer.redis.com/)
23
+
24
+ [Join the Redis community](https://redis.com/community/)
25
+
26
+ [Work at Redis](https://redis.com/company/careers/jobs/)
27
+
28
+ ## Installation
29
+
30
+ Start a redis via docker:
31
+
32
+ ```bash
33
+ docker run -p 6379:6379 -d redis:8.0-rc1
34
+ ```
35
+
36
+ To install node-redis, simply:
37
+
38
+ ```bash
39
+ npm install redis
40
+ ```
41
+ > "redis" is the "whole in one" package that includes all the other packages. If you only need a subset of the commands,
42
+ > you can install the individual packages. See the list below.
43
+
44
+ ## Packages
45
+
46
+ | Name | Description |
47
+ | ---------------------------------------------- | ------------------------------------------------------------------------------------------- |
48
+ | [`redis`](https://github.com/redis/node-redis/tree/master/packages/redis) | The client with all the ["redis-stack"](https://github.com/redis-stack/redis-stack) modules |
49
+ | [`@redis/client`](https://github.com/redis/node-redis/tree/master/packages/client) | The base clients (i.e `RedisClient`, `RedisCluster`, etc.) |
50
+ | [`@redis/bloom`](https://github.com/redis/node-redis/tree/master/packages/bloom) | [Redis Bloom](https://redis.io/docs/data-types/probabilistic/) commands |
51
+ | [`@redis/json`](https://github.com/redis/node-redis/tree/master/packages/json) | [Redis JSON](https://redis.io/docs/data-types/json/) commands |
52
+ | [`@redis/search`](https://github.com/redis/node-redis/tree/master/packages/search) | [RediSearch](https://redis.io/docs/interact/search-and-query/) commands |
53
+ | [`@redis/time-series`](https://github.com/redis/node-redis/tree/master/packages/time-series) | [Redis Time-Series](https://redis.io/docs/data-types/timeseries/) commands |
54
+ | [`@redis/entraid`](https://github.com/redis/node-redis/tree/master/packages/entraid) | Secure token-based authentication for Redis clients using Microsoft Entra ID |
55
+
56
+ > Looking for a high-level library to handle object mapping?
57
+ > See [redis-om-node](https://github.com/redis/redis-om-node)!
58
+
59
+
60
+ ## Usage
61
+
62
+ ### Basic Example
63
+
64
+ ```typescript
65
+ import { createClient } from "redis";
66
+
67
+ const client = await createClient()
68
+ .on("error", (err) => console.log("Redis Client Error", err))
69
+ .connect();
70
+
71
+ await client.set("key", "value");
72
+ const value = await client.get("key");
73
+ client.destroy();
74
+ ```
75
+
76
+ The above code connects to localhost on port 6379. To connect to a different host or port, use a connection string in
77
+ the format `redis[s]://[[username][:password]@][host][:port][/db-number]`:
78
+
79
+ ```typescript
80
+ createClient({
81
+ url: "redis://alice:foobared@awesome.redis.server:6380",
82
+ });
83
+ ```
84
+
85
+ You can also use discrete parameters, UNIX sockets, and even TLS to connect. Details can be found in
86
+ the [client configuration guide](https://github.com/redis/node-redis/blob/master/docs/client-configuration.md).
87
+
88
+ To check if the the client is connected and ready to send commands, use `client.isReady` which returns a boolean.
89
+ `client.isOpen` is also available. This returns `true` when the client's underlying socket is open, and `false` when it
90
+ isn't (for example when the client is still connecting or reconnecting after a network error).
91
+
92
+ ### Redis Commands
93
+
94
+ There is built-in support for all of the [out-of-the-box Redis commands](https://redis.io/commands). They are exposed
95
+ using the raw Redis command names (`HSET`, `HGETALL`, etc.) and a friendlier camel-cased version (`hSet`, `hGetAll`,
96
+ etc.):
97
+
98
+ ```typescript
99
+ // raw Redis commands
100
+ await client.HSET("key", "field", "value");
101
+ await client.HGETALL("key");
102
+
103
+ // friendly JavaScript commands
104
+ await client.hSet("key", "field", "value");
105
+ await client.hGetAll("key");
106
+ ```
107
+
108
+ Modifiers to commands are specified using a JavaScript object:
109
+
110
+ ```typescript
111
+ await client.set("key", "value", {
112
+ EX: 10,
113
+ NX: true,
114
+ });
115
+ ```
116
+
117
+ Replies will be transformed into useful data structures:
118
+
119
+ ```typescript
120
+ await client.hGetAll("key"); // { field1: 'value1', field2: 'value2' }
121
+ await client.hVals("key"); // ['value1', 'value2']
122
+ ```
123
+
124
+ `Buffer`s are supported as well:
125
+
126
+ ```typescript
127
+ const client = createClient().withTypeMapping({
128
+ [RESP_TYPES.BLOB_STRING]: Buffer
129
+ });
130
+
131
+ await client.hSet("key", "field", Buffer.from("value")); // 'OK'
132
+ await client.hGet("key", "field"); // { field: <Buffer 76 61 6c 75 65> }
133
+
134
+ ```
135
+
136
+ For commands that return serialized binary payloads, such as `DUMP`, map blob strings to `Buffer` before using the result with commands like `RESTORE`:
137
+
138
+ ```typescript
139
+ const binaryClient = createClient().withTypeMapping({
140
+ [RESP_TYPES.BLOB_STRING]: Buffer
141
+ });
142
+
143
+ const dump = await binaryClient.dump("source");
144
+ await binaryClient.restore("destination", 0, dump);
145
+ ```
146
+
147
+ ### Unsupported Redis Commands
148
+
149
+ If you want to run commands and/or use arguments that Node Redis doesn't know about (yet!) use `.sendCommand()`:
150
+
151
+ ```typescript
152
+ await client.sendCommand(["SET", "key", "value", "NX"]); // 'OK'
153
+
154
+ await client.sendCommand(["HGETALL", "key"]); // ['key1', 'field1', 'key2', 'field2']
155
+ ```
156
+
157
+ ### Transactions (Multi/Exec)
158
+
159
+ Start a [transaction](https://redis.io/topics/transactions) by calling `.multi()`, then chaining your commands. When
160
+ you're done, call `.exec()` and you'll get an array back with your results:
161
+
162
+ ```typescript
163
+ await client.set("another-key", "another-value");
164
+
165
+ const [setKeyReply, otherKeyValue] = await client
166
+ .multi()
167
+ .set("key", "value")
168
+ .get("another-key")
169
+ .exec(); // ['OK', 'another-value']
170
+ ```
171
+
172
+ You can also [watch](https://redis.io/topics/transactions#optimistic-locking-using-check-and-set) keys by calling
173
+ `.watch()`. Your transaction will abort if any of the watched keys change.
174
+
175
+
176
+ ### Blocking Commands
177
+
178
+ In v4, `RedisClient` had the ability to create a pool of connections using an "Isolation Pool" on top of the "main"
179
+ connection. However, there was no way to use the pool without a "main" connection:
180
+
181
+ ```javascript
182
+ const client = await createClient()
183
+ .on("error", (err) => console.error(err))
184
+ .connect();
185
+
186
+ await client.ping(client.commandOptions({ isolated: true }));
187
+ ```
188
+
189
+ In v5 we've extracted this pool logic into its own class—`RedisClientPool`:
190
+
191
+ ```javascript
192
+ const pool = await createClientPool()
193
+ .on("error", (err) => console.error(err))
194
+ .connect();
195
+
196
+ await pool.ping();
197
+ ```
198
+
199
+
200
+ ### Pub/Sub
201
+
202
+ See the [Pub/Sub overview](https://github.com/redis/node-redis/blob/master/docs/pub-sub.md).
203
+
204
+ ### Scan Iterator
205
+
206
+ [`SCAN`](https://redis.io/commands/scan) results can be looped over
207
+ using [async iterators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator):
208
+
209
+ ```typescript
210
+ for await (const key of client.scanIterator()) {
211
+ // use the key!
212
+ await client.get(key);
213
+ }
214
+ ```
215
+
216
+ This works with `HSCAN`, `SSCAN`, and `ZSCAN` too:
217
+
218
+ ```typescript
219
+ for await (const { field, value } of client.hScanIterator("hash")) {
220
+ }
221
+ for await (const member of client.sScanIterator("set")) {
222
+ }
223
+ for await (const { score, value } of client.zScanIterator("sorted-set")) {
224
+ }
225
+ ```
226
+
227
+ You can override the default options by providing a configuration object:
228
+
229
+ ```typescript
230
+ client.scanIterator({
231
+ TYPE: "string", // `SCAN` only
232
+ MATCH: "patter*",
233
+ COUNT: 100,
234
+ });
235
+ ```
236
+
237
+ ### Disconnecting
238
+
239
+ The `QUIT` command has been deprecated in Redis 7.2 and should now also be considered deprecated in Node-Redis. Instead
240
+ of sending a `QUIT` command to the server, the client can simply close the network connection.
241
+
242
+ `client.QUIT/quit()` is replaced by `client.close()`. and, to avoid confusion, `client.disconnect()` has been renamed to
243
+ `client.destroy()`.
244
+
245
+ ```typescript
246
+ client.destroy();
247
+ ```
248
+ ### Client Side Caching
249
+
250
+ Node Redis v5 adds support for [Client Side Caching](https://redis.io/docs/manual/client-side-caching/), which enables clients to cache query results locally. The Redis server will notify the client when cached results are no longer valid.
251
+
252
+ ```typescript
253
+ // Enable client side caching with RESP3
254
+ const client = createClient({
255
+ RESP: 3,
256
+ clientSideCache: {
257
+ ttl: 0, // Time-to-live (0 = no expiration)
258
+ maxEntries: 0, // Maximum entries (0 = unlimited)
259
+ evictPolicy: "LRU" // Eviction policy: "LRU" or "FIFO"
260
+ }
261
+ });
262
+ ```
263
+
264
+ See the [V5 documentation](https://github.com/redis/node-redis/blob/master/docs/v5.md#client-side-caching) for more details and advanced usage.
265
+
266
+ ### Auto-Pipelining
267
+
268
+ Node Redis will automatically pipeline requests that are made during the same "tick".
269
+
270
+ ```typescript
271
+ client.set("Tm9kZSBSZWRpcw==", "users:1");
272
+ client.sAdd("users:1:tokens", "Tm9kZSBSZWRpcw==");
273
+ ```
274
+
275
+ Of course, if you don't do something with your Promises you're certain to
276
+ get [unhandled Promise exceptions](https://nodejs.org/api/process.html#process_event_unhandledrejection). To take
277
+ advantage of auto-pipelining and handle your Promises, use `Promise.all()`.
278
+
279
+ ```typescript
280
+ await Promise.all([
281
+ client.set("Tm9kZSBSZWRpcw==", "users:1"),
282
+ client.sAdd("users:1:tokens", "Tm9kZSBSZWRpcw=="),
283
+ ]);
284
+ ```
285
+
286
+ ### Programmability
287
+
288
+ See the [Programmability overview](https://github.com/redis/node-redis/blob/master/docs/programmability.md).
289
+
290
+ ### Clustering
291
+
292
+ Check out the [Clustering Guide](https://github.com/redis/node-redis/blob/master/docs/clustering.md) when using Node Redis to connect to a Redis Cluster.
293
+
294
+ ### Events
295
+
296
+ The Node Redis client class is an Nodejs EventEmitter and it emits an event each time the network status changes:
297
+
298
+ | Name | When | Listener arguments |
299
+ | ----------------------- | ---------------------------------------------------------------------------------- | --------------------------------------------------------- |
300
+ | `connect` | Initiating a connection to the server | _No arguments_ |
301
+ | `ready` | Client is ready to use | _No arguments_ |
302
+ | `end` | Connection has been closed (via `.disconnect()`) | _No arguments_ |
303
+ | `error` | An error has occurred—usually a network issue such as "Socket closed unexpectedly" | `(error: Error)` |
304
+ | `reconnecting` | Client is trying to reconnect to the server | _No arguments_ |
305
+ | `sharded-channel-moved` | See [here](https://github.com/redis/node-redis/blob/master/docs/pub-sub.md#sharded-channel-moved-event) | See [here](https://github.com/redis/node-redis/blob/master/docs/pub-sub.md#sharded-channel-moved-event) |
306
+
307
+ > :warning: You **MUST** listen to `error` events. If a client doesn't have at least one `error` listener registered and
308
+ > an `error` occurs, that error will be thrown and the Node.js process will exit. See the [ > `EventEmitter` docs](https://nodejs.org/api/events.html#events_error_events) for more details.
309
+
310
+ > The client will not emit [any other events](https://github.com/redis/node-redis/blob/master/docs/v3-to-v4.md#all-the-removed-events) beyond those listed above.
311
+
312
+ ## Supported Redis versions
313
+
314
+ Node Redis is supported with the following versions of Redis:
315
+
316
+ | Version | Supported |
317
+ | ------- | ------------------ |
318
+ | 8.0.z | :heavy_check_mark: |
319
+ | 7.4.z | :heavy_check_mark: |
320
+ | 7.2.z | :heavy_check_mark: |
321
+ | < 7.2 | :x: |
322
+
323
+ > Node Redis should work with older versions of Redis, but it is not fully tested and we cannot offer support.
324
+
325
+ ## Migration
326
+
327
+ - [From V3 to V4](https://github.com/redis/node-redis/blob/master/docs/v3-to-v4.md)
328
+ - [From V4 to V5](https://github.com/redis/node-redis/blob/master/docs/v4-to-v5.md)
329
+ - [V5](https://github.com/redis/node-redis/blob/master/docs/v5.md)
330
+
331
+ ## Contributing
332
+
333
+ If you'd like to contribute, check out the [contributing guide](https://github.com/redis/node-redis/blob/master/CONTRIBUTING.md).
334
+
335
+ Thank you to all the people who already contributed to Node Redis!
336
+
337
+ [![Contributors](https://contrib.rocks/image?repo=redis/node-redis)](https://github.com/redis/node-redis/graphs/contributors)
338
+
339
+ ## License
340
+
341
+ This repository is licensed under the "MIT" license. See [LICENSE](https://github.com/redis/node-redis/blob/master/LICENSE).