# Redis

> Read and write a Redis database: store and read values with expiries, count, queue with lists, keep records in hashes and sets, publish messages and add to streams. Start a workflow for each new stream entry.

App · 20 actions. Web page: https://docs.lodol.com/docs/integrations/redis

## Actions

### Add stream entry

Append an entry of named fields to a stream, a log that keeps every entry in order. Creates the stream if needed.

**Inputs**

- Stream (`stream`, text, required): The key of the stream, such as events:orders.
- Fields (`fields`, object, required): Each field's name with its value. Objects and lists are stored as JSON.
- Keep about (`max_length`, number): Trim the stream to roughly this many of its newest entries. Leave empty to keep every entry. At least 1.

**Outputs**

- `id` (text): The new entry's ID, such as 1712345678901-0
- `length` (number): How many entries the stream has now
- `stream` (text): The stream

**Example output**

```json
{
  "id": "1712345678901-0",
  "length": 12,
  "stream": "events:orders"
}
```

### Add to list

Add items to the end (or start) of a list, creating the list if needed. Handy as a simple queue.

**Inputs**

- List (`key`, text, required): The key of the list, such as jobs:pending.
- Items (`values`, list, required): What to add, one per item. Objects are stored as JSON.
- Where (`end`, text): Which end of the list to use. One of: `end` (End of the list), `start` (Start of the list).

**Outputs**

- `added_count` (number): How many items were added
- `key` (text): The key
- `length` (number): How many items the list has now

**Example output**

```json
{
  "added_count": 1,
  "key": "jobs:pending",
  "length": 3
}
```

### Add to set

Add members to a set, a collection without repeats. A member already in it is not added twice.

**Inputs**

- Set (`key`, text, required): The key of the set, such as tags:featured.
- Members (`members`, list, required): The values, one per item.

**Outputs**

- `added_count` (number): Members that were new
- `key` (text): The key
- `size` (number): How many members the set has now

**Example output**

```json
{
  "added_count": 1,
  "key": "tags:featured",
  "size": 4
}
```

### Check key

Find out whether a key exists, what kind of value it holds (string, list, hash, set, sorted set or stream) and when it expires.

**Inputs**

- Key (`key`, text, required): The key's name, such as order:1042.

**Outputs**

- `exists` (true/false): True when the key exists
- `expires_in_seconds` (number): Seconds until the key expires; empty when it never does
- `key` (text): The key
- `type` (text): string, list, hash, set, zset or stream

**Example output**

```json
{
  "exists": true,
  "expires_in_seconds": null,
  "key": "order:1042",
  "type": "string"
}
```

### Delete hash fields

Remove named fields from a hash. The hash is deleted once it has none.

**Inputs**

- Hash (`key`, text, required): The key of the hash, such as customer:42.
- Fields (`fields`, list, required): The fields to remove, one per item.

**Outputs**

- `deleted_count` (number): How many fields were removed
- `key` (text): The key

**Example output**

```json
{
  "deleted_count": 1,
  "key": "customer:42"
}
```

### Delete keys

Delete one or more keys, whatever they hold. Missing keys are skipped.

**Inputs**

- Keys (`keys`, list, required): The keys to delete, one per item.

**Outputs**

- `deleted_count` (number): How many of them existed and were deleted
- `keys` (list): The keys asked for

**Example output**

```json
{
  "deleted_count": 1,
  "keys": [
    "order:1042"
  ]
}
```

### Find keys

List the keys whose names match a pattern, such as order:* for every key starting order:. Safe on a busy server: Redis is read in small steps.

**Inputs**

- Pattern (`pattern`, text): * matches any text and ? one character. Leave empty for every key.
- Limit (`limit`, number): The most keys to return, up to 1000. Defaults to 100. From 1 to 1000.

**Outputs**

- `count` (number): How many were returned
- `has_more` (true/false): True when more keys match than were returned
- `keys` (list): The matching keys, in name order
- `pattern` (text): The pattern matched

**Example output**

```json
{
  "count": 2,
  "has_more": false,
  "keys": [
    "order:1041",
    "order:1042"
  ],
  "pattern": "order:*"
}
```

### Get hash fields

Read every field of a hash, or only the ones named.

**Inputs**

- Hash (`key`, text, required): The key of the hash, such as customer:42.
- Fields (`fields`, list): Only these fields, one per item. Leave empty for all of them.

**Outputs**

- `count` (number): How many fields were read
- `fields` (object): Each field's name and value
- `found` (true/false): True when any of the fields exist
- `key` (text): The key

**Example output**

```json
{
  "count": 2,
  "fields": {
    "name": "Ada",
    "plan": "pro"
  },
  "found": true,
  "key": "customer:42"
}
```

### Get list items

Read a list's items by position without removing them. Positions start at 0; -1 is the last item.

**Inputs**

- List (`key`, text, required): The key of the list, such as jobs:pending.
- From position (`start`, number): The first position to read. Defaults to 0, the first item.
- To position (`stop`, number): The last position to read. Defaults to -1, the last item.

**Outputs**

- `count` (number): How many items were read
- `items` (list): The items, in list order
- `key` (text): The key
- `length` (number): How many items the whole list has

**Example output**

```json
{
  "count": 2,
  "items": [
    "job-17",
    "job-18"
  ],
  "key": "jobs:pending",
  "length": 2
}
```

### Get set members

List a set's members, and check whether one value is among them.

**Inputs**

- Set (`key`, text, required): The key of the set, such as tags:featured.
- Check for (`member`, text): A value to look for. Leave empty to only list.

**Outputs**

- `count` (number): How many members the set has
- `is_member` (true/false): Whether 'Check for' is a member; empty when not asked
- `key` (text): The key
- `members` (list): The members, in name order

**Example output**

```json
{
  "count": 2,
  "is_member": true,
  "key": "tags:featured",
  "members": [
    "blue",
    "green"
  ]
}
```

### Get value

Read the text stored under a key, and how long until it expires. A missing key comes back with 'found' false rather than failing.

**Inputs**

- Key (`key`, text, required): The key's name, such as order:1042.

**Outputs**

- `expires_in_seconds` (number): Seconds until the key expires; empty when it never does
- `found` (true/false): True when the key holds a value
- `key` (text): The key
- `value` (text): The text stored; empty when the key is missing

**Example output**

```json
{
  "expires_in_seconds": 3540,
  "found": true,
  "key": "order:1042",
  "value": "{\"status\": \"paid\"}"
}
```

### Increment number

Add to the number stored under a key and get the new number. A missing key starts at 0. Use a negative amount to count down.

**Inputs**

- Key (`key`, text, required): The key's name, such as order:1042.
- By (`by`, number): How much to add, such as 1, -1 or 2.5. Defaults to 1.

**Outputs**

- `key` (text): The key
- `value` (number): The number after adding

**Example output**

```json
{
  "key": "visits:today",
  "value": 42
}
```

### Publish message

Send a message to a channel. Only programs subscribed at that moment receive it; nothing is kept. To keep messages, add them to a stream.

**Inputs**

- Channel (`channel`, text, required): The channel's name, such as notifications.
- Message (`message`, text, required): The message. An object is sent as JSON.

**Outputs**

- `channel` (text): The channel
- `receivers` (number): How many subscribers received it

**Example output**

```json
{
  "channel": "notifications",
  "receivers": 2
}
```

### Read stream entries

Read a stream's entries without removing them: from the oldest, or the newest first, and only those after an entry ID if given.

**Inputs**

- Stream (`stream`, text, required): The key of the stream, such as events:orders.
- After entry ID (`after_id`, text): Only entries added after this one, such as an earlier step's 'last_id'.
- Limit (`limit`, number): The most entries to read, up to 1000. Defaults to 100. From 1 to 1000.
- Newest first (`newest_first`, true/false): Read from the newest entry back. Off reads from the oldest. Default: `false`.

**Outputs**

- `count` (number): How many entries were read
- `entries` (list): The entries read
- `last_id` (text): The ID of the last entry read; empty when none
- `stream` (text): The stream

**Example output**

```json
{
  "count": 1,
  "entries": [
    {
      "added_at": "2024-04-05T19:34:38.901Z",
      "fields": {
        "order": "1042",
        "status": "paid"
      },
      "id": "1712345678901-0"
    }
  ],
  "last_id": "1712345678901-0",
  "stream": "events:orders"
}
```

### Remove from set

Remove members from a set. The set is deleted once it is empty.

**Inputs**

- Set (`key`, text, required): The key of the set, such as tags:featured.
- Members (`members`, list, required): The values, one per item.

**Outputs**

- `key` (text): The key
- `removed_count` (number): How many members were removed

**Example output**

```json
{
  "key": "tags:featured",
  "removed_count": 1
}
```

### Run command

Send any other Redis command, such as ZADD for a leaderboard or GEOSEARCH, with its arguments. Commands that wipe or stop the server are refused.

**Inputs**

- Command (`command`, text, required): The command's name, such as ZADD.
- Arguments (`arguments`, list): Its arguments in order, one per item, such as leaderboard, 10, ada.

**Outputs**

- `command` (text): The command as sent
- `result` (text): Redis's answer: text, a number, or a list of them

**Example output**

```json
{
  "command": "ZADD leaderboard 10 ada",
  "result": 1
}
```

### Set expiry

Make a key expire after some seconds or at a moment. With neither, the key's expiry is removed so it is kept.

**Inputs**

- Key (`key`, text, required): The key's name, such as order:1042.
- Expire in (seconds) (`expire_in_seconds`, number): Delete the key this many seconds from now, such as 3600 for an hour. Leave empty, with no 'Expire at', to keep it. At least 1.
- Expire at (`expire_at`, date and time): Delete the key at this moment instead. Must be in the future.

**Outputs**

- `expires_in_seconds` (number): Seconds until the key expires; empty when it never does
- `found` (true/false): False when the key does not exist
- `key` (text): The key

**Example output**

```json
{
  "expires_in_seconds": 3600,
  "found": true,
  "key": "order:1042"
}
```

### Set hash fields

Set named fields of a hash, a record stored under one key, creating it if needed. Other fields are left as they are.

**Inputs**

- Hash (`key`, text, required): The key of the hash, such as customer:42.
- Fields (`fields`, object, required): Each field's name with its value. Objects and lists are stored as JSON.

**Outputs**

- `added_count` (number): Fields that were new
- `key` (text): The key
- `updated_count` (number): Fields that already existed and were replaced

**Example output**

```json
{
  "added_count": 2,
  "key": "customer:42",
  "updated_count": 0
}
```

### Set value

Store text, a number or JSON under a key, replacing what is there. It can expire after some seconds or at a moment, and can be stored only if the key is new (for a lock) or only if it already exists.

**Inputs**

- Key (`key`, text, required): The key's name, such as order:1042.
- Value (`value`, text, required): What to store. An object or list is stored as JSON.
- Expire in (seconds) (`expire_in_seconds`, number): Delete the key this many seconds from now, such as 3600 for an hour. Leave empty, with no 'Expire at', to keep it. At least 1.
- Expire at (`expire_at`, date and time): Delete the key at this moment instead. Must be in the future.
- Store (`condition`, text): When to store it. Defaults to always. One of: `always`, `if_missing` (Only if the key doesn't exist yet), `if_exists` (Only if the key already exists). Default: `always`.

**Outputs**

- `key` (text): The key
- `stored` (true/false): False when the condition kept the value from being stored
- `value` (text): The text stored

**Example output**

```json
{
  "key": "order:1042",
  "stored": true,
  "value": "{\"status\": \"paid\"}"
}
```

### Take from list

Take items off the start (or end) of a list, removing them. With items added at the end, this takes the oldest first.

**Inputs**

- List (`key`, text, required): The key of the list, such as jobs:pending.
- Where (`end`, text): Which end to take from. Defaults to the start. One of: `end` (End of the list), `start` (Start of the list). Default: `start`.
- How many (`count`, number): How many items to take. Defaults to 1. From 1 to 1000.

**Outputs**

- `count` (number): How many were taken; 0 when the list was empty
- `items` (list): The items taken, in the order taken
- `key` (text): The key

**Example output**

```json
{
  "count": 1,
  "items": [
    "job-17"
  ],
  "key": "jobs:pending"
}
```
