1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

prepare 4.1.0 (#2111)

* increase test coverage

* @node-redis to @redis

* ugprade deps

* fix benchmark

* use 7.0 docker (not rc), update readmes, clean code, fix @-redis import

* update readme

* fix function in cluster

* update docs

Co-authored-by: Chayim <chayim@users.noreply.github.com>

* Update clustering.md

* add subpackages move warning

* drop support for node 12

* upgrade deps

* fix tsconfig.base.json

Co-authored-by: Chayim <chayim@users.noreply.github.com>
This commit is contained in:
Leibale Eidelman
2022-05-02 11:48:12 -04:00
committed by GitHub
parent baf67fd87f
commit 0752f143a6
89 changed files with 3866 additions and 1862 deletions

View File

@@ -19,7 +19,7 @@ If `socket.write()` returns `false`—meaning that ["all or part of the data was
Redis has support for [modules](https://redis.io/modules) and running [Lua scripts](../README.md#lua-scripts) within the Redis context. To take advantage of typing within these scenarios, `RedisClient` and `RedisCluster` should be used with [typeof](https://www.typescriptlang.org/docs/handbook/2/typeof-types.html), rather than the base types `RedisClientType` and `RedisClusterType`.
```typescript
import { createClient } from '@node-redis/client';
import { createClient } from '@redis/client';
export const client = createClient();

View File

@@ -3,24 +3,25 @@
| Property | Default | Description |
|--------------------------|------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| url | | `redis[s]://[[username][:password]@][host][:port][/db-number]` (see [`redis`](https://www.iana.org/assignments/uri-schemes/prov/redis) and [`rediss`](https://www.iana.org/assignments/uri-schemes/prov/rediss) IANA registration for more details) |
| socket | | Object defining socket connection properties. Any [`net.createConnection`](https://nodejs.org/api/net.html#netcreateconnectionoptions-connectlistener) option that is not listed here is supported as well |
| socket.port | `6379` | Port to connect to |
| socket.host | `'localhost'` | Hostname to connect to |
| socket.family | `0` | Version of IP stack. Must be `4 \| 6 \| 0`. The value `0` indicates that both IPv4 and IPv6 addresses are allowed. |
| socket.path | | UNIX Socket to connect to |
| socket.connectTimeout | `5000` | The timeout for connecting to the Redis Server (in milliseconds) |
| socket.noDelay | `true` | Enable/disable the use of [`Nagle's algorithm`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay) |
| socket.keepAlive | `5000` | Enable/disable the [`keep-alive`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) functionality |
| socket | | Socket connection properties. Unlisted [`net.connect`](https://nodejs.org/api/net.html#socketconnectoptions-connectlistener) properties (and [`tls.connect`](https://nodejs.org/api/tls.html#tlsconnectoptions-callback)) are also supported |
| socket.port | `6379` | Redis server port |
| socket.host | `'localhost'` | Redis server hostname |
| socket.family | `0` | IP Stack version (one of `4 \| 6 \| 0`) |
| socket.path | | Path to the UNIX Socket |
| socket.connectTimeout | `5000` | Connection Timeout (in milliseconds) |
| socket.noDelay | `true` | Toggle [`Nagle's algorithm`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay) |
| socket.keepAlive | `5000` | Toggle [`keep-alive`](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay) functionality |
| socket.tls | | See explanation and examples [below](#TLS) |
| socket.reconnectStrategy | `retries => Math.min(retries * 50, 500)` | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
| username | | ACL username ([see ACL guide](https://redis.io/topics/acl)) |
| password | | ACL password or the old "--requirepass" password |
| name | | Connection name ([see `CLIENT SETNAME`](https://redis.io/commands/client-setname)) |
| database | | Database number to connect to (see [`SELECT`](https://redis.io/commands/select) command) |
| modules | | Object defining which [Redis Modules](../README.md#packages) to include |
| scripts | | Object defining Lua Scripts to use with this client (see [Lua Scripts](../README.md#lua-scripts)) |
| database | | Redis database number (see [`SELECT`](https://redis.io/commands/select) command) |
| modules | | Included [Redis Modules](../README.md#packages) |
| scripts | | Script definitions (see [Lua Scripts](../README.md#lua-scripts)) |
| functions | | Function definitions (see [Functions](../README.md#functions)) |
| commandsQueueMaxLength | | Maximum length of the client's internal command queue |
| disableOfflineQueue | `false` | Disables offline queuing, see the [FAQ](./FAQ.md#what-happens-when-the-network-goes-down) for details |
| disableOfflineQueue | `false` | Disables offline queuing, see [FAQ](./FAQ.md#what-happens-when-the-network-goes-down) |
| readonly | `false` | Connect in [`READONLY`](https://redis.io/commands/readonly) mode |
| legacyMode | `false` | Maintain some backwards compatibility (see the [Migration Guide](./v3-to-v4.md)) |
| isolationPoolOptions | | See the [Isolated Execution Guide](./isolated-execution.md) |
@@ -31,12 +32,12 @@ You can implement a custom reconnect strategy as a function:
- Receives the number of retries attempted so far.
- Returns `number | Error`:
- `number`: the wait time in milliseconds prior attempting to reconnect.
- `Error`: closes the client and flushes the internal command queues.
- `number`: wait time in milliseconds prior to attempting a reconnect.
- `Error`: closes the client and flushes internal command queues.
## TLS
When creating a client, set `socket.tls` to `true` to enable TLS. Below are some basic examples.
To enable TLS, set `socket.tls` to `true`. Below are some basic examples.
> For configuration options see [tls.connect](https://nodejs.org/api/tls.html#tlsconnectoptions-callback) and [tls.createSecureContext](https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions), as those are the underlying functions used by this library.

View File

@@ -7,25 +7,23 @@ Connecting to a cluster is a bit different. Create the client by specifying some
```typescript
import { createCluster } from 'redis';
(async () => {
const cluster = createCluster({
rootNodes: [
{
url: 'redis://10.0.0.1:30001'
},
{
url: 'redis://10.0.0.2:30002'
}
]
});
const cluster = createCluster({
rootNodes: [
{
url: 'redis://10.0.0.1:30001'
},
{
url: 'redis://10.0.0.2:30002'
}
]
});
cluster.on('error', (err) => console.log('Redis Cluster Error', err));
cluster.on('error', (err) => console.log('Redis Cluster Error', err));
await cluster.connect();
await cluster.connect();
await cluster.set('key', 'value');
const value = await cluster.get('key');
})();
await cluster.set('key', 'value');
const value = await cluster.get('key');
```
## `createCluster` configuration
@@ -38,15 +36,15 @@ import { createCluster } from 'redis';
| defaults | | The default configuration values for every client in the cluster. Use this for example when specifying an ACL user to connect with |
| useReplicas | `false` | When `true`, distribute load by executing readonly commands (such as `GET`, `GEOSEARCH`, etc.) across all cluster nodes. When `false`, only use master nodes |
| maxCommandRedirections | `16` | The maximum number of times a command will be redirected due to `MOVED` or `ASK` errors |
| nodeAddressMap | | Object defining the [node address mapping](#node-address-map) |
| modules | | Object defining which [Redis Modules](../README.md#modules) to include |
| scripts | | Object defining Lua Scripts to use with this client (see [Lua Scripts](../README.md#lua-scripts)) |
| nodeAddressMap | | Defines the [node address mapping](#node-address-map) |
| modules | | Included [Redis Modules](../README.md#packages) |
| scripts | | Script definitions (see [Lua Scripts](../README.md#lua-scripts)) |
| functions | | Function definitions (see [Functions](../README.md#functions)) |
## Node Address Map
Your cluster might be configured to work within an internal network that your local environment doesn't have access to. For example, your development machine could only have access to external addresses, but the cluster returns its internal addresses. In this scenario, it's useful to provide a map from those internal addresses to the external ones.
The configuration for this is a simple mapping. Just provide a `nodeAddressMap` property mapping the internal addresses and ports to the external addresses and ports. Then, any address provided to `rootNodes` or returned from the cluster will be mapped accordingly:
A node address map is required when a redis cluster is configured with addresses that are inaccessible by the machine running the redis client.
This is a mapping of addresses and ports, with the values being the accessible address/port combination. Example:
```javascript
createCluster({
@@ -68,12 +66,12 @@ createCluster({
### Commands that operate on Redis Keys
Commands such as `GET`, `SET`, etc. will be routed by the first key, for instance `MGET 1 2 3` will be routed by the key `1`.
Commands such as `GET`, `SET`, etc. are routed by the first key, for instance `MGET 1 2 3` will be routed by the key `1`.
### [Server Commands](https://redis.io/commands#server)
Admin commands such as `MEMORY STATS`, `FLUSHALL`, etc. are not attached to the cluster, and should be executed on a specific node using `.getSlot()` or `.getAllMasters()`.
Admin commands such as `MEMORY STATS`, `FLUSHALL`, etc. are not attached to the cluster, and must be executed on a specific node via `.getSlotMaster()`.
### "Forwarded Commands"
Some commands (e.g. `PUBLISH`) are forwarded to other cluster nodes by the Redis server. The client will send these commands to a random node in order to spread the load across the cluster.
Certain commands (e.g. `PUBLISH`) are forwarded to other cluster nodes by the Redis server. This client sends these commands to a random node in order to spread the load across the cluster.