Skip to content

teff.tool.builtin.redis

teff.tool.builtin.redis

Redis-like store tools — Redis, KeyDB, Valkey (any RESP server).

A single tool with an action selector covering the common operations (get/set/delete/list/exists/ttl/expire/incr, plus lists, sets, hashes and pub/sub) against any RESP-compatible server. It uses the redis package, which speaks RESP to Redis, KeyDB and Valkey alike, so the same config works for all of them. The distributed lock tool reuses the shared client setup in :class:_RedisBase.

Classes:

Name Description
RedisTool

Read/write a Redis-compatible store (KeyDB/Valkey supported).

RedisTool

Bases: _RedisBase

Read/write a Redis-compatible store (KeyDB/Valkey supported).

Parameters:

Name Type Description Default
action

ping | get | set | delete | exists | list | ttl | expire | incr | rpush | lrange | sadd | smembers | hset | hget | hgetall | publish.

required
key

Key to operate on (not needed for ping/list).

required
value

Value for set/rpush/hset.

required
ttl

Expiry in seconds for set (ex) or expire.

required
pattern

Glob pattern for list (default *).

required
field

Hash field for hset/hget.

required
member

Set member for sadd.

required
channel

Channel for publish.

required
message

Message for publish.

required
start, stop

Range for lrange (default all).

required
Source code in teff/tool/builtin/redis.py
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class RedisTool(_RedisBase):
    """Read/write a Redis-compatible store (KeyDB/Valkey supported).

    Args:
        action: ``ping`` | ``get`` | ``set`` | ``delete`` | ``exists``
            | ``list`` | ``ttl`` | ``expire`` | ``incr`` | ``rpush``
            | ``lrange`` | ``sadd`` | ``smembers`` | ``hset`` | ``hget``
            | ``hgetall`` | ``publish``.
        key: Key to operate on (not needed for ``ping``/``list``).
        value: Value for ``set``/``rpush``/``hset``.
        ttl: Expiry in seconds for ``set`` (``ex``) or ``expire``.
        pattern: Glob pattern for ``list`` (default ``*``).
        field: Hash field for ``hset``/``hget``.
        member: Set member for ``sadd``.
        channel: Channel for ``publish``.
        message: Message for ``publish``.
        start, stop: Range for ``lrange`` (default all).
    """

    name = "redis"
    description = (
        "Read/write a Redis-compatible key-value store (get, set, delete, "
        "list, exists, ttl, expire, incr, lists, sets, hashes, publish)"
    )

    def run(  # type: ignore[override]
        self,
        action: str,
        key: str = "",
        value: str = "",
        ttl: int = -1,
        pattern: str = "*",
        field: str = "",
        member: str = "",
        channel: str = "",
        message: str = "",
        start: int = 0,
        stop: int = -1,
    ) -> str:
        if not action:
            raise ValueError("action is required (get, set, delete, list, ...)")
        client = self._client()
        try:
            a = action.lower()
            if a == "ping":
                return "PONG" if client.ping() else "no response"
            if a == "get":
                if not key:
                    raise ValueError("key is required")
                val = client.get(key)
                return "not found" if val is None else str(val)
            if a == "set":
                if not key:
                    raise ValueError("key is required")
                if int(ttl) > 0:
                    client.set(key, value, ex=int(ttl))
                else:
                    client.set(key, value)
                return f"set {key}"
            if a == "delete":
                if not key:
                    raise ValueError("key is required")
                return f"deleted {key}" if client.delete(key) else "not found"
            if a == "exists":
                if not key:
                    raise ValueError("key is required")
                return "yes" if client.exists(key) else "no"
            if a == "list":
                keys = [
                    k
                    for k in client.scan_iter(match=pattern, count=100)
                    if fnmatch.fnmatchcase(k, pattern)
                ]
                return "\n".join(sorted(keys)) if keys else "no keys"
            if a == "ttl":
                if not key:
                    raise ValueError("key is required")
                return str(client.ttl(key))
            if a == "expire":
                if not key:
                    raise ValueError("key is required")
                client.expire(key, int(ttl))
                return f"expire {key} {ttl}s"
            if a == "incr":
                if not key:
                    raise ValueError("key is required")
                return str(client.incr(key))
            if a == "rpush":
                if not key:
                    raise ValueError("key is required")
                return str(client.rpush(key, value))
            if a == "lrange":
                if not key:
                    raise ValueError("key is required")
                items = client.lrange(key, int(start), int(stop))
                return "\n".join(str(i) for i in items) if items else "empty"
            if a == "sadd":
                if not key:
                    raise ValueError("key is required")
                return str(client.sadd(key, member))
            if a == "smembers":
                if not key:
                    raise ValueError("key is required")
                members = client.smembers(key)
                return (
                    "\n".join(sorted(str(m) for m in members)) if members else "empty"
                )
            if a == "hset":
                if not key or not field:
                    raise ValueError("key and field are required")
                return str(client.hset(key, field, value))
            if a == "hget":
                if not key or not field:
                    raise ValueError("key and field are required")
                val = client.hget(key, field)
                return "not found" if val is None else str(val)
            if a == "hgetall":
                if not key:
                    raise ValueError("key is required")
                data = client.hgetall(key)
                return (
                    "\n".join(f"{k}={v}" for k, v in data.items()) if data else "empty"
                )
            if a == "publish":
                if not channel:
                    raise ValueError("channel is required")
                subs = client.publish(channel, message)
                return f"published to {channel} ({subs} subscriber(s))"
            raise ValueError(f"unknown action: {action}")
        finally:
            client.close()