# Redis - Command

> 在 Redis 键值存储上执行命令。

Source: https://docs.sommatic.ai/zh/nodes/integrations.redis.command/

- 标识: `integrations.redis.command`
- 类别: 集成
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

在 Redis 键值存储上执行命令。

连接你的 Redis 实例，自动化缓存和数据操作。可发送任意带参数的 Redis 命令，从简单的键值读取到复杂的数据结构操作。

**操作：**
- **GET / SET / MGET / MSET：** 读写字符串值，支持批量操作。
- **HGET / HSET / HGETALL：** 操作哈希表（字段-值对）。
- **LPUSH / RPUSH / LPOP / LRANGE：** 管理有序列表。
- **SADD / SMEMBERS / SINTER：** 操作集合及求交集。
- **ZADD / ZRANGE / ZRANGEBYSCORE：** 带分值的有序集合。
- **DEL / EXISTS / TTL / EXPIRE：** 键的生命周期与过期管理。
- **INCR / DECR：** 原子计数器。
- **PUBLISH：** 向频道发布消息。

## 配置

- `connection_id` (string, 必填): Redis 实例的凭据。接受 `connection-string`（redis:// URI）或 `basic-auth`（密码 + 主机 + 端口）类型的凭据。
- `command` (string, 必填, 支持表达式): 要执行的 Redis 命令（例如 GET、SET、HGET、LPUSH、DEL）。
- `args` (array, 可选): 命令参数（例如 key、value、字段名）。

## 输出端口

- `out` (默认): 命令结果
- `error`: 命令执行错误

## 输入契约

- `connection_id` (string, 必填)
- `command` (string, 必填)
- `args` (array, 可选)

## 输出契约

- `result` (object, 可选): Redis 命令响应

## 在表达式中

后续节点可通过 `$("Redis - Command").item.json.<路径>` 读取此节点输出中的内容（请把 `Redis - Command` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("Redis - Command").item.json.result` (object) = `"{\"status\":\"open\",\"total\":129.99}"`: Redis 命令响应


## 工作原理

发送一条原始 Redis 命令（GET、SET、HGET、LPUSH、DEL 等）及其参数，并将 Redis 回复放在 `result` 中返回。

1. 解析节点配置中的表达式。`connection_id`、`command` 为空时从输入项读取；`args` 缺失或为空数组时同样从输入项读取。
2. 解析连接：`connection_id` 中的 `redis://` URL、`connection-string` 凭据（客户端支持的任意 URL，包括 `rediss://`），或包含 `host`、`port`（默认 6379）和可选 `password` 的 `basic-auth` / `object` 凭据。
3. 将 `command` 转为大写，丢弃 `null` / `undefined` 参数，其余参数用 `String()` 转换后原样发送命令。
4. 断开连接，并经 `out` 返回包含回复的 `{ result }`：根据命令不同为字符串、数字、`null` 或数组。

## 在工作流中

### 节点

```json
{
  "node_id": "node-cache-order",
  "label": "Read cached order",
  "operator_slug": "integrations.redis.command",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "command": "GET",
    "args": [
      "order:{{ $json.order_id }}"
    ]
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": "{\"status\":\"open\",\"total\":129.99}"
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

Redis 已回复。对不存在的键执行 GET 也从这里输出，`result` 为 `null`。

```json
{
  "result": "{\"status\":\"open\",\"total\":129.99}"
}
```

### `error`

校验、凭据解析、连接或命令执行失败。

```json
{
  "error": "WRONGTYPE Operation against a key holding the wrong kind of value"
}
```

## 代码中的行为

- `args`: 每个参数都会用 `String()` 转换：对象会变成 `[object Object]`。传入 JSON 值前请先序列化为字符串。
- `connection_id`: 结构化凭据会生成不含 TLS 和用户名的 `redis://` URL。需要 TLS 或 ACL 用户时，请将完整 URL 保存为 `connection-string` 凭据。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `connection_id is required` | `error` | 配置和输入项中都没有凭据。 |
| `command is required` | `error` | `command` 为空。 |
| `Credential <connection_id> (type '<type>') missing host` | `error` | 结构化凭据缺少 `host`。 |
| `ERR <Redis error message>` | `error` | Redis 拒绝了该命令（未知命令、参数数量错误等），错误信息原样传递。 |

## 说明与限制

- 每次运行都会新建并关闭连接，节点不会重试。INCR、LPUSH 等非幂等命令每次执行只运行一次。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "integrations.redis.command",
  "name": "Redis - Command",
  "category": "integrations",
  "version": "1.0.0",
  "description": "Execute commands on a Redis key-value store.\n\n  Connect your Redis instance and automate cache and data operations. Send any Redis command with arguments — from simple key-value reads to complex data structure manipulation.\n\n  **Operations:**\n  - **GET / SET / MGET / MSET:** Read and write string values, bulk operations.\n  - **HGET / HSET / HGETALL:** Work with hash maps (field-value pairs).\n  - **LPUSH / RPUSH / LPOP / LRANGE:** Manage ordered lists.\n  - **SADD / SMEMBERS / SINTER:** Work with sets and intersections.\n  - **ZADD / ZRANGE / ZRANGEBYSCORE:** Sorted sets with scoring.\n  - **DEL / EXISTS / TTL / EXPIRE:** Key lifecycle and expiration management.\n  - **INCR / DECR:** Atomic counters.\n  - **PUBLISH:** Publish messages to channels.",
  "capabilities": [
    "kv",
    "cache",
    "redis",
    "database"
  ],
  "group": [
    {
      "key": "redis",
      "name": "Redis",
      "icon_url": "/icons/integrations/redis/redis.svg"
    }
  ],
  "icon_url": "/icons/integrations/redis/redis.svg",
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "Redis Credential",
        "description": "Credential for your Redis instance. Accepts credentials of type `connection-string` (redis:// URI) or `basic-auth` (password + host + port).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "connection-string",
            "basic-auth"
          ],
          "types": [
            "redis"
          ]
        }
      },
      "command": {
        "type": "string",
        "title": "Command",
        "description": "Redis command to execute (e.g., GET, SET, HGET, LPUSH, DEL).",
        "format": "expression"
      },
      "args": {
        "type": "array",
        "title": "Arguments",
        "description": "Command arguments (e.g., key, value, field name)."
      }
    },
    "required": [
      "connection_id",
      "command"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "command": {
        "type": "string"
      },
      "args": {
        "type": "array"
      }
    },
    "required": [
      "connection_id",
      "command"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "object",
        "description": "Redis command response"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Result",
      "description": "Command result",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Command execution error",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "fast-io"
  ],
  "tags": [
    "cache",
    "kv"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://sommatic.ai",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "Redis Credential",
      "description": "Credential for your Redis instance. Accepts credentials of type `connection-string` (redis:// URI) or `basic-auth` (password + host + port).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "connection-string",
          "basic-auth"
        ],
        "types": [
          "redis"
        ]
      }
    },
    "command": {
      "type": "string",
      "title": "Command",
      "description": "Redis command to execute (e.g., GET, SET, HGET, LPUSH, DEL).",
      "format": "expression"
    },
    "args": {
      "type": "array",
      "title": "Arguments",
      "description": "Command arguments (e.g., key, value, field name)."
    }
  },
  "required": [
    "connection_id",
    "command"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "command": {
      "type": "string"
    },
    "args": {
      "type": "array"
    }
  },
  "required": [
    "connection_id",
    "command"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "object",
      "description": "Redis command response"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Result",
    "description": "Command result",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Command execution error",
    "is_default": false,
    "supports_condition": false
  }
]
```
