valkey-glide 2.2.1rc1__cp313-cp313-macosx_10_7_x86_64.whl
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.
Potentially problematic release.
This version of valkey-glide might be problematic. Click here for more details.
- glide/__init__.py +388 -0
- glide/async_commands/__init__.py +5 -0
- glide/async_commands/cluster_commands.py +1476 -0
- glide/async_commands/core.py +7818 -0
- glide/async_commands/ft.py +465 -0
- glide/async_commands/glide_json.py +1269 -0
- glide/async_commands/standalone_commands.py +1001 -0
- glide/glide.cpython-313-darwin.so +0 -0
- glide/glide.pyi +61 -0
- glide/glide_client.py +821 -0
- glide/logger.py +97 -0
- glide/opentelemetry.py +185 -0
- glide/py.typed +0 -0
- glide_shared/__init__.py +330 -0
- glide_shared/commands/__init__.py +0 -0
- glide_shared/commands/batch.py +5997 -0
- glide_shared/commands/batch_options.py +261 -0
- glide_shared/commands/bitmap.py +320 -0
- glide_shared/commands/command_args.py +103 -0
- glide_shared/commands/core_options.py +407 -0
- glide_shared/commands/server_modules/ft_options/ft_aggregate_options.py +300 -0
- glide_shared/commands/server_modules/ft_options/ft_constants.py +84 -0
- glide_shared/commands/server_modules/ft_options/ft_create_options.py +423 -0
- glide_shared/commands/server_modules/ft_options/ft_profile_options.py +113 -0
- glide_shared/commands/server_modules/ft_options/ft_search_options.py +139 -0
- glide_shared/commands/server_modules/json_batch.py +820 -0
- glide_shared/commands/server_modules/json_options.py +93 -0
- glide_shared/commands/sorted_set.py +412 -0
- glide_shared/commands/stream.py +449 -0
- glide_shared/config.py +975 -0
- glide_shared/constants.py +124 -0
- glide_shared/exceptions.py +88 -0
- glide_shared/protobuf/command_request_pb2.py +56 -0
- glide_shared/protobuf/connection_request_pb2.py +56 -0
- glide_shared/protobuf/response_pb2.py +32 -0
- glide_shared/protobuf_codec.py +110 -0
- glide_shared/routes.py +161 -0
- valkey_glide-2.2.1rc1.dist-info/METADATA +210 -0
- valkey_glide-2.2.1rc1.dist-info/RECORD +40 -0
- valkey_glide-2.2.1rc1.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,1476 @@
|
|
|
1
|
+
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Dict, List, Mapping, Optional, Union, cast
|
|
6
|
+
|
|
7
|
+
from glide.glide import ClusterScanCursor, Script
|
|
8
|
+
from glide_shared.commands.batch import ClusterBatch
|
|
9
|
+
from glide_shared.commands.batch_options import ClusterBatchOptions
|
|
10
|
+
from glide_shared.commands.command_args import ObjectType
|
|
11
|
+
from glide_shared.commands.core_options import (
|
|
12
|
+
FlushMode,
|
|
13
|
+
FunctionRestorePolicy,
|
|
14
|
+
InfoSection,
|
|
15
|
+
)
|
|
16
|
+
from glide_shared.constants import (
|
|
17
|
+
TOK,
|
|
18
|
+
TClusterResponse,
|
|
19
|
+
TEncodable,
|
|
20
|
+
TFunctionListResponse,
|
|
21
|
+
TFunctionStatsSingleNodeResponse,
|
|
22
|
+
TResult,
|
|
23
|
+
)
|
|
24
|
+
from glide_shared.exceptions import RequestError
|
|
25
|
+
from glide_shared.protobuf.command_request_pb2 import RequestType
|
|
26
|
+
from glide_shared.routes import Route
|
|
27
|
+
|
|
28
|
+
from .core import CoreCommands
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ClusterCommands(CoreCommands):
|
|
32
|
+
async def custom_command(
|
|
33
|
+
self, command_args: List[TEncodable], route: Optional[Route] = None
|
|
34
|
+
) -> TClusterResponse[TResult]:
|
|
35
|
+
"""
|
|
36
|
+
Executes a single command, without checking inputs.
|
|
37
|
+
See the [Valkey GLIDE Wiki](https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#custom-command)
|
|
38
|
+
for details on the restrictions and limitations of the custom command API.
|
|
39
|
+
|
|
40
|
+
This function should only be used for single-response commands. Commands that don't return complete response and awaits
|
|
41
|
+
(such as SUBSCRIBE), or that return potentially more than a single response (such as XREAD), or that change the
|
|
42
|
+
client's behavior (such as entering pub/sub mode on RESP2 connections) shouldn't be called using this function.
|
|
43
|
+
|
|
44
|
+
For example - Return a list of all pub/sub clients from all nodes::
|
|
45
|
+
|
|
46
|
+
await client.customCommand(["CLIENT", "LIST","TYPE", "PUBSUB"], AllNodes())
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
command_args (List[TEncodable]): List of the command's arguments, where each argument is either a string or bytes.
|
|
50
|
+
Every part of the command, including the command name and subcommands, should be added as a separate value in args.
|
|
51
|
+
route (Optional[Route]): The command will be routed automatically based on the passed command's default request
|
|
52
|
+
policy, unless `route` is provided, in which
|
|
53
|
+
case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
TClusterResponse[TResult]: The returning value depends on the executed command and the route.
|
|
57
|
+
"""
|
|
58
|
+
return cast(
|
|
59
|
+
TClusterResponse[TResult],
|
|
60
|
+
await self._execute_command(RequestType.CustomCommand, command_args, route),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
async def info(
|
|
64
|
+
self,
|
|
65
|
+
sections: Optional[List[InfoSection]] = None,
|
|
66
|
+
route: Optional[Route] = None,
|
|
67
|
+
) -> TClusterResponse[bytes]:
|
|
68
|
+
"""
|
|
69
|
+
Get information and statistics about the server.
|
|
70
|
+
|
|
71
|
+
Starting from server version 7, command supports multiple section arguments.
|
|
72
|
+
|
|
73
|
+
See [valkey.io](https://valkey.io/commands/info/) for details.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
sections (Optional[List[InfoSection]]): A list of InfoSection values specifying which sections of
|
|
77
|
+
information to retrieve. When no parameter is provided, the default option is assumed.
|
|
78
|
+
route (Optional[Route]): The command will be routed to all primaries, unless `route` is provided, in
|
|
79
|
+
which case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
TClusterResponse[bytes]: If a single node route is requested, returns a bytes string containing the information for
|
|
83
|
+
the required sections. Otherwise, returns a dict of bytes strings, with each key containing the address of
|
|
84
|
+
the queried node and value containing the information regarding the requested sections.
|
|
85
|
+
"""
|
|
86
|
+
args: List[TEncodable] = (
|
|
87
|
+
[section.value for section in sections] if sections else []
|
|
88
|
+
)
|
|
89
|
+
return cast(
|
|
90
|
+
TClusterResponse[bytes],
|
|
91
|
+
await self._execute_command(RequestType.Info, args, route),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
async def exec(
|
|
95
|
+
self,
|
|
96
|
+
batch: ClusterBatch,
|
|
97
|
+
raise_on_error: bool,
|
|
98
|
+
options: Optional[ClusterBatchOptions] = None,
|
|
99
|
+
) -> Optional[List[TResult]]:
|
|
100
|
+
"""
|
|
101
|
+
Executes a batch by processing the queued commands.
|
|
102
|
+
|
|
103
|
+
**Routing Behavior:**
|
|
104
|
+
|
|
105
|
+
- If a `route` is specified in `ClusterBatchOptions`, the entire batch is sent
|
|
106
|
+
to the specified node.
|
|
107
|
+
- If no `route` is specified:
|
|
108
|
+
- **Atomic batches (Transactions):** Routed to the slot owner of the
|
|
109
|
+
first key in the batch. If no key is found, the request is sent to a random node.
|
|
110
|
+
- **Non-atomic batches (Pipelines):** Each command is routed to the node
|
|
111
|
+
owning the corresponding key's slot. If no key is present, routing follows the
|
|
112
|
+
command's request policy. Multi-node commands are automatically split and
|
|
113
|
+
dispatched to the appropriate nodes.
|
|
114
|
+
|
|
115
|
+
**Behavior notes:**
|
|
116
|
+
|
|
117
|
+
- **Atomic Batches (Transactions):** All key-based commands must map to the
|
|
118
|
+
same hash slot. If keys span different slots, the transaction will fail. If the
|
|
119
|
+
transaction fails due to a `WATCH` command, `EXEC` will return `None`.
|
|
120
|
+
|
|
121
|
+
**Retry and Redirection:**
|
|
122
|
+
|
|
123
|
+
- If a redirection error occurs:
|
|
124
|
+
- **Atomic batches (Transactions):** The entire transaction will be
|
|
125
|
+
redirected.
|
|
126
|
+
- **Non-atomic batches:** Only commands that encountered redirection
|
|
127
|
+
errors will be redirected.
|
|
128
|
+
- Retries for failures will be handled according to the configured `BatchRetryStrategy`.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
batch (ClusterBatch): A `ClusterBatch` containing the commands to execute.
|
|
132
|
+
raise_on_error (bool): Determines how errors are handled within the batch response.
|
|
133
|
+
When set to `True`, the first encountered error in the batch will be raised as an
|
|
134
|
+
exception of type `RequestError` after all retries and reconnections have been
|
|
135
|
+
executed.
|
|
136
|
+
When set to `False`, errors will be included as part of the batch response,
|
|
137
|
+
allowing the caller to process both successful and failed commands together. In this case,
|
|
138
|
+
error details will be provided as instances of `RequestError`.
|
|
139
|
+
options (Optional[ClusterBatchOptions]): A `ClusterBatchOptions` object containing execution options.
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
Optional[List[TResult]]: An array of results, where each entry
|
|
143
|
+
corresponds to a command's execution result.
|
|
144
|
+
|
|
145
|
+
See Also:
|
|
146
|
+
[Valkey Transactions (Atomic Batches)](https://valkey.io/docs/topics/transactions/)
|
|
147
|
+
[Valkey Pipelines (Non-Atomic Batches)](https://valkey.io/docs/topics/pipelining/)
|
|
148
|
+
|
|
149
|
+
Examples:
|
|
150
|
+
# Atomic batch (transaction): all keys must share the same hash slot
|
|
151
|
+
>>> options = ClusterBatchOptions(timeout=1000) # Set a timeout of 1000 milliseconds
|
|
152
|
+
>>> atomic_batch = ClusterBatch(is_atomic=True)
|
|
153
|
+
>>> atomic_batch.set("key", "1")
|
|
154
|
+
>>> atomic_batch.incr("key")
|
|
155
|
+
>>> atomic_batch.get("key")
|
|
156
|
+
>>> atomic_result = await cluster_client.exec(atomic_batch, False, options)
|
|
157
|
+
>>> print(f"Atomic Batch Result: {atomic_result}")
|
|
158
|
+
# Output: Atomic Batch Result: [OK, 2, 2]
|
|
159
|
+
|
|
160
|
+
# Non-atomic batch (pipeline): keys may span different hash slots
|
|
161
|
+
>>> retry_strategy = BatchRetryStrategy(retry_server_error=True, retry_connection_error=False)
|
|
162
|
+
>>> pipeline_options = ClusterBatchOptions(retry_strategy=retry_strategy)
|
|
163
|
+
>>> non_atomic_batch = ClusterBatch(is_atomic=False)
|
|
164
|
+
>>> non_atomic_batch.set("key1", "value1")
|
|
165
|
+
>>> non_atomic_batch.set("key2", "value2")
|
|
166
|
+
>>> non_atomic_batch.get("key1")
|
|
167
|
+
>>> non_atomic_batch.get("key2")
|
|
168
|
+
>>> non_atomic_result = await cluster_client.exec(non_atomic_batch, False, pipeline_options)
|
|
169
|
+
>>> print(f"Non-Atomic Batch Result: {non_atomic_result}")
|
|
170
|
+
# Output: Non-Atomic Batch Result: [OK, OK, value1, value2]
|
|
171
|
+
"""
|
|
172
|
+
commands = batch.commands[:]
|
|
173
|
+
|
|
174
|
+
if (
|
|
175
|
+
batch.is_atomic
|
|
176
|
+
and options
|
|
177
|
+
and options.retry_strategy
|
|
178
|
+
and (
|
|
179
|
+
options.retry_strategy.retry_server_error
|
|
180
|
+
or options.retry_strategy.retry_connection_error
|
|
181
|
+
)
|
|
182
|
+
):
|
|
183
|
+
raise RequestError(
|
|
184
|
+
"Retry strategies are not supported for atomic batches (transactions). "
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
# Extract values to make the _execute_batch call cleaner
|
|
188
|
+
retry_server_error = (
|
|
189
|
+
options.retry_strategy.retry_server_error
|
|
190
|
+
if options and options.retry_strategy
|
|
191
|
+
else False
|
|
192
|
+
)
|
|
193
|
+
retry_connection_error = (
|
|
194
|
+
options.retry_strategy.retry_connection_error
|
|
195
|
+
if options and options.retry_strategy
|
|
196
|
+
else False
|
|
197
|
+
)
|
|
198
|
+
route = options.route if options else None
|
|
199
|
+
timeout = options.timeout if options else None
|
|
200
|
+
|
|
201
|
+
return await self._execute_batch(
|
|
202
|
+
commands,
|
|
203
|
+
batch.is_atomic,
|
|
204
|
+
raise_on_error,
|
|
205
|
+
retry_server_error,
|
|
206
|
+
retry_connection_error,
|
|
207
|
+
route,
|
|
208
|
+
timeout,
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
async def config_resetstat(
|
|
212
|
+
self,
|
|
213
|
+
route: Optional[Route] = None,
|
|
214
|
+
) -> TOK:
|
|
215
|
+
"""
|
|
216
|
+
Resets the statistics reported by the server using the INFO and LATENCY HISTOGRAM commands.
|
|
217
|
+
|
|
218
|
+
See [valkey.io](https://valkey.io/commands/config-resetstat/) for details.
|
|
219
|
+
|
|
220
|
+
Args:
|
|
221
|
+
route (Optional[Route]): The command will be routed automatically to all nodes, unless `route` is provided, in
|
|
222
|
+
which case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
223
|
+
|
|
224
|
+
Returns:
|
|
225
|
+
OK: Returns "OK" to confirm that the statistics were successfully reset.
|
|
226
|
+
"""
|
|
227
|
+
return cast(
|
|
228
|
+
TOK, await self._execute_command(RequestType.ConfigResetStat, [], route)
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
async def config_rewrite(
|
|
232
|
+
self,
|
|
233
|
+
route: Optional[Route] = None,
|
|
234
|
+
) -> TOK:
|
|
235
|
+
"""
|
|
236
|
+
Rewrite the configuration file with the current configuration.
|
|
237
|
+
|
|
238
|
+
See [valkey.io](https://valkey.io/commands/config-rewrite/) for details.
|
|
239
|
+
|
|
240
|
+
Args:
|
|
241
|
+
route (Optional[TRoute]): The command will be routed automatically to all nodes, unless `route` is provided, in
|
|
242
|
+
which case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
243
|
+
|
|
244
|
+
Returns:
|
|
245
|
+
OK: OK is returned when the configuration was rewritten properly. Otherwise an error is raised.
|
|
246
|
+
|
|
247
|
+
Example:
|
|
248
|
+
>>> await client.config_rewrite()
|
|
249
|
+
'OK'
|
|
250
|
+
"""
|
|
251
|
+
return cast(
|
|
252
|
+
TOK, await self._execute_command(RequestType.ConfigRewrite, [], route)
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
async def client_id(
|
|
256
|
+
self,
|
|
257
|
+
route: Optional[Route] = None,
|
|
258
|
+
) -> TClusterResponse[int]:
|
|
259
|
+
"""
|
|
260
|
+
Returns the current connection id.
|
|
261
|
+
|
|
262
|
+
See [valkey.io](https://valkey.io/commands/client-id/) for more information.
|
|
263
|
+
|
|
264
|
+
Args:
|
|
265
|
+
route (Optional[Route]): The command will be sent to a random node, unless `route` is provided, in which
|
|
266
|
+
case the client will route the command to the nodes defined by `route`.
|
|
267
|
+
|
|
268
|
+
Returns:
|
|
269
|
+
TClusterResponse[int]: The id of the client.
|
|
270
|
+
|
|
271
|
+
If a single node route is requested, returns a int representing the client's id.
|
|
272
|
+
|
|
273
|
+
Otherwise, returns a dict of [byte , int] where each key contains the address of
|
|
274
|
+
the queried node and the value contains the client's id.
|
|
275
|
+
"""
|
|
276
|
+
return cast(
|
|
277
|
+
TClusterResponse[int],
|
|
278
|
+
await self._execute_command(RequestType.ClientId, [], route),
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
async def ping(
|
|
282
|
+
self, message: Optional[TEncodable] = None, route: Optional[Route] = None
|
|
283
|
+
) -> bytes:
|
|
284
|
+
"""
|
|
285
|
+
Ping the server.
|
|
286
|
+
|
|
287
|
+
See [valkey.io](https://valkey.io/commands/ping/) for more details.
|
|
288
|
+
|
|
289
|
+
Args:
|
|
290
|
+
message (Optional[TEncodable]): An optional message to include in the PING command. If not provided,
|
|
291
|
+
the server will respond with b"PONG". If provided, the server will respond with a copy of the message.
|
|
292
|
+
route (Optional[Route]): The command will be sent to all primaries, unless `route` is provided, in which
|
|
293
|
+
case the client will route the command to the nodes defined by `route`
|
|
294
|
+
|
|
295
|
+
Returns:
|
|
296
|
+
bytes: b'PONG' if `message` is not provided, otherwise return a copy of `message`.
|
|
297
|
+
|
|
298
|
+
Examples:
|
|
299
|
+
>>> await client.ping()
|
|
300
|
+
b"PONG"
|
|
301
|
+
>>> await client.ping("Hello")
|
|
302
|
+
b"Hello"
|
|
303
|
+
"""
|
|
304
|
+
argument = [] if message is None else [message]
|
|
305
|
+
return cast(
|
|
306
|
+
bytes, await self._execute_command(RequestType.Ping, argument, route)
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
async def config_get(
|
|
310
|
+
self, parameters: List[TEncodable], route: Optional[Route] = None
|
|
311
|
+
) -> TClusterResponse[Dict[bytes, bytes]]:
|
|
312
|
+
"""
|
|
313
|
+
Get the values of configuration parameters.
|
|
314
|
+
Starting from server version 7, command supports multiple parameters.
|
|
315
|
+
|
|
316
|
+
See [valkey.io](https://valkey.io/commands/config-get/) for details.
|
|
317
|
+
|
|
318
|
+
Args:
|
|
319
|
+
parameters (List[TEncodable]): A list of configuration parameter names to retrieve values for.
|
|
320
|
+
route (Optional[Route]): The command will be routed to a random node, unless `route` is provided,
|
|
321
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
322
|
+
|
|
323
|
+
Returns:
|
|
324
|
+
TClusterResponse[Dict[bytes, bytes]]: A dictionary of values corresponding to the
|
|
325
|
+
configuration parameters.
|
|
326
|
+
When specifying a route other than a single node, response will be::
|
|
327
|
+
|
|
328
|
+
{Address (bytes) : response (Dict[bytes, bytes]) , ... }
|
|
329
|
+
|
|
330
|
+
with type of Dict[bytes, Dict[bytes, bytes]].
|
|
331
|
+
|
|
332
|
+
Examples:
|
|
333
|
+
>>> await client.config_get(["timeout"] , RandomNode())
|
|
334
|
+
{b'timeout': b'1000'}
|
|
335
|
+
>>> await client.config_get(["timeout" , b"maxmemory"])
|
|
336
|
+
{b'timeout': b'1000', b"maxmemory": b"1GB"}
|
|
337
|
+
"""
|
|
338
|
+
return cast(
|
|
339
|
+
TClusterResponse[Dict[bytes, bytes]],
|
|
340
|
+
await self._execute_command(RequestType.ConfigGet, parameters, route),
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
async def config_set(
|
|
344
|
+
self,
|
|
345
|
+
parameters_map: Mapping[TEncodable, TEncodable],
|
|
346
|
+
route: Optional[Route] = None,
|
|
347
|
+
) -> TOK:
|
|
348
|
+
"""
|
|
349
|
+
Set configuration parameters to the specified values.
|
|
350
|
+
Starting from server version 7, command supports multiple parameters.
|
|
351
|
+
|
|
352
|
+
See [valkey.io](https://valkey.io/commands/config-set/) for details.
|
|
353
|
+
|
|
354
|
+
Args:
|
|
355
|
+
parameters_map (Mapping[TEncodable, TEncodable]): A map consisting of configuration
|
|
356
|
+
parameters and their respective values to set.
|
|
357
|
+
route (Optional[Route]): The command will be routed to all nodes, unless `route` is provided,
|
|
358
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
359
|
+
|
|
360
|
+
Returns:
|
|
361
|
+
OK: Returns OK if all configurations have been successfully set. Otherwise, raises an error.
|
|
362
|
+
|
|
363
|
+
Examples:
|
|
364
|
+
>>> await client.config_set({"timeout": "1000", b"maxmemory": b"1GB"})
|
|
365
|
+
OK
|
|
366
|
+
"""
|
|
367
|
+
parameters: List[TEncodable] = []
|
|
368
|
+
for pair in parameters_map.items():
|
|
369
|
+
parameters.extend(pair)
|
|
370
|
+
return cast(
|
|
371
|
+
TOK,
|
|
372
|
+
await self._execute_command(RequestType.ConfigSet, parameters, route),
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
async def client_getname(
|
|
376
|
+
self, route: Optional[Route] = None
|
|
377
|
+
) -> TClusterResponse[Optional[bytes]]:
|
|
378
|
+
"""
|
|
379
|
+
Get the name of the connection to which the request is routed.
|
|
380
|
+
|
|
381
|
+
See [valkey.io](https://valkey.io/commands/client-getname/) for more details.
|
|
382
|
+
|
|
383
|
+
Args:
|
|
384
|
+
route (Optional[Route]): The command will be routed to a random node, unless `route` is provided,
|
|
385
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
386
|
+
|
|
387
|
+
Returns:
|
|
388
|
+
TClusterResponse[Optional[bytes]]: The name of the client connection as a bytes string if a name is set,
|
|
389
|
+
or None if no name is assigned.
|
|
390
|
+
|
|
391
|
+
When specifying a route other than a single node, response will be::
|
|
392
|
+
|
|
393
|
+
{Address (bytes) : response (Optional[bytes]) , ... }
|
|
394
|
+
|
|
395
|
+
with type of Dict[str, Optional[str]].
|
|
396
|
+
|
|
397
|
+
Examples:
|
|
398
|
+
>>> await client.client_getname()
|
|
399
|
+
b'Connection Name'
|
|
400
|
+
>>> await client.client_getname(AllNodes())
|
|
401
|
+
{b'addr': b'Connection Name', b'addr2': b'Connection Name', b'addr3': b'Connection Name'}
|
|
402
|
+
"""
|
|
403
|
+
return cast(
|
|
404
|
+
TClusterResponse[Optional[bytes]],
|
|
405
|
+
await self._execute_command(RequestType.ClientGetName, [], route),
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
async def dbsize(self, route: Optional[Route] = None) -> int:
|
|
409
|
+
"""
|
|
410
|
+
Returns the number of keys in the database.
|
|
411
|
+
|
|
412
|
+
See [valkey.io](https://valkey.io/commands/dbsize) for more details.
|
|
413
|
+
|
|
414
|
+
Args:
|
|
415
|
+
route (Optional[Route]): The command will be routed to all primaries, unless `route` is provided,
|
|
416
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
417
|
+
|
|
418
|
+
Returns:
|
|
419
|
+
int: The number of keys in the database.
|
|
420
|
+
|
|
421
|
+
In the case of routing the query to multiple nodes, returns the aggregated number of keys across the
|
|
422
|
+
different nodes.
|
|
423
|
+
|
|
424
|
+
Examples:
|
|
425
|
+
>>> await client.dbsize()
|
|
426
|
+
10 # Indicates there are 10 keys in the cluster.
|
|
427
|
+
"""
|
|
428
|
+
return cast(int, await self._execute_command(RequestType.DBSize, [], route))
|
|
429
|
+
|
|
430
|
+
async def echo(
|
|
431
|
+
self, message: TEncodable, route: Optional[Route] = None
|
|
432
|
+
) -> TClusterResponse[bytes]:
|
|
433
|
+
"""
|
|
434
|
+
Echoes the provided `message` back.
|
|
435
|
+
|
|
436
|
+
See [valkey.io](https://valkey.io/commands/echo) for more details.
|
|
437
|
+
|
|
438
|
+
Args:
|
|
439
|
+
message (TEncodable): The message to be echoed back.
|
|
440
|
+
route (Optional[Route]): The command will be routed to a random node, unless `route` is provided,
|
|
441
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
442
|
+
|
|
443
|
+
Returns:
|
|
444
|
+
TClusterResponse[bytes]: The provided `message`.
|
|
445
|
+
|
|
446
|
+
When specifying a route other than a single node, response will be::
|
|
447
|
+
|
|
448
|
+
{Address (bytes) : response (bytes) , ... }
|
|
449
|
+
|
|
450
|
+
with type of Dict[bytes, bytes].
|
|
451
|
+
|
|
452
|
+
Examples:
|
|
453
|
+
>>> await client.echo(b"Valkey GLIDE")
|
|
454
|
+
b'Valkey GLIDE'
|
|
455
|
+
>>> await client.echo("Valkey GLIDE", AllNodes())
|
|
456
|
+
{b'addr': b'Valkey GLIDE', b'addr2': b'Valkey GLIDE', b'addr3': b'Valkey GLIDE'}
|
|
457
|
+
"""
|
|
458
|
+
return cast(
|
|
459
|
+
TClusterResponse[bytes],
|
|
460
|
+
await self._execute_command(RequestType.Echo, [message], route),
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
async def function_load(
|
|
464
|
+
self,
|
|
465
|
+
library_code: TEncodable,
|
|
466
|
+
replace: bool = False,
|
|
467
|
+
route: Optional[Route] = None,
|
|
468
|
+
) -> bytes:
|
|
469
|
+
"""
|
|
470
|
+
Loads a library to Valkey.
|
|
471
|
+
|
|
472
|
+
See [valkey.io](https://valkey.io/commands/function-load/) for more details.
|
|
473
|
+
|
|
474
|
+
Args:
|
|
475
|
+
library_code (TEncodable): The source code that implements the library.
|
|
476
|
+
replace (bool): Whether the given library should overwrite a library with the same name if
|
|
477
|
+
it already exists.
|
|
478
|
+
route (Optional[Route]): The command will be routed to all primaries, unless `route` is provided,
|
|
479
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
480
|
+
|
|
481
|
+
Returns:
|
|
482
|
+
bytes: The library name that was loaded.
|
|
483
|
+
|
|
484
|
+
Examples:
|
|
485
|
+
>>> code = "#!lua name=mylib \\n redis.register_function('myfunc', function(keys, args) return args[1] end)"
|
|
486
|
+
>>> await client.function_load(code, True, RandomNode())
|
|
487
|
+
b"mylib"
|
|
488
|
+
|
|
489
|
+
Since: Valkey 7.0.0.
|
|
490
|
+
"""
|
|
491
|
+
return cast(
|
|
492
|
+
bytes,
|
|
493
|
+
await self._execute_command(
|
|
494
|
+
RequestType.FunctionLoad,
|
|
495
|
+
["REPLACE", library_code] if replace else [library_code],
|
|
496
|
+
route,
|
|
497
|
+
),
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
async def function_list(
|
|
501
|
+
self,
|
|
502
|
+
library_name_pattern: Optional[TEncodable] = None,
|
|
503
|
+
with_code: bool = False,
|
|
504
|
+
route: Optional[Route] = None,
|
|
505
|
+
) -> TClusterResponse[TFunctionListResponse]:
|
|
506
|
+
"""
|
|
507
|
+
Returns information about the functions and libraries.
|
|
508
|
+
|
|
509
|
+
See [valkey.io](https://valkey.io/commands/function-list/) for more details.
|
|
510
|
+
|
|
511
|
+
Args:
|
|
512
|
+
library_name_pattern (Optional[TEncodable]): A wildcard pattern for matching library names.
|
|
513
|
+
with_code (bool): Specifies whether to request the library code from the server or not.
|
|
514
|
+
route (Optional[Route]): The command will be routed to a random node, unless `route` is provided,
|
|
515
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
516
|
+
|
|
517
|
+
Returns:
|
|
518
|
+
TClusterResponse[TFunctionListResponse]: Info
|
|
519
|
+
about all or selected libraries and their functions.
|
|
520
|
+
|
|
521
|
+
Examples:
|
|
522
|
+
>>> response = await client.function_list("myLib?_backup", True)
|
|
523
|
+
[{
|
|
524
|
+
b"library_name": b"myLib5_backup",
|
|
525
|
+
b"engine": b"LUA",
|
|
526
|
+
b"functions": [{
|
|
527
|
+
b"name": b"myfunc",
|
|
528
|
+
b"description": None,
|
|
529
|
+
b"flags": {b"no-writes"},
|
|
530
|
+
}],
|
|
531
|
+
b"library_code":
|
|
532
|
+
b"#!lua name=mylib \\n redis.register_function('myfunc', function(keys, args) " \\
|
|
533
|
+
b"return args[1] end)"
|
|
534
|
+
}]
|
|
535
|
+
|
|
536
|
+
Since: Valkey 7.0.0.
|
|
537
|
+
"""
|
|
538
|
+
args = []
|
|
539
|
+
if library_name_pattern is not None:
|
|
540
|
+
args.extend(["LIBRARYNAME", library_name_pattern])
|
|
541
|
+
if with_code:
|
|
542
|
+
args.append("WITHCODE")
|
|
543
|
+
return cast(
|
|
544
|
+
TClusterResponse[TFunctionListResponse],
|
|
545
|
+
await self._execute_command(
|
|
546
|
+
RequestType.FunctionList,
|
|
547
|
+
args,
|
|
548
|
+
route,
|
|
549
|
+
),
|
|
550
|
+
)
|
|
551
|
+
|
|
552
|
+
async def function_flush(
|
|
553
|
+
self, mode: Optional[FlushMode] = None, route: Optional[Route] = None
|
|
554
|
+
) -> TOK:
|
|
555
|
+
"""
|
|
556
|
+
Deletes all function libraries.
|
|
557
|
+
|
|
558
|
+
See [valkey.io](https://valkey.io/commands/function-flush/) for more details.
|
|
559
|
+
|
|
560
|
+
Args:
|
|
561
|
+
mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
|
|
562
|
+
route (Optional[Route]): The command will be routed to all primaries, unless `route` is provided,
|
|
563
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
564
|
+
|
|
565
|
+
Returns:
|
|
566
|
+
TOK: A simple `OK`.
|
|
567
|
+
|
|
568
|
+
Examples:
|
|
569
|
+
>>> await client.function_flush(FlushMode.SYNC)
|
|
570
|
+
"OK"
|
|
571
|
+
|
|
572
|
+
Since: Valkey 7.0.0.
|
|
573
|
+
"""
|
|
574
|
+
return cast(
|
|
575
|
+
TOK,
|
|
576
|
+
await self._execute_command(
|
|
577
|
+
RequestType.FunctionFlush,
|
|
578
|
+
[mode.value] if mode else [],
|
|
579
|
+
route,
|
|
580
|
+
),
|
|
581
|
+
)
|
|
582
|
+
|
|
583
|
+
async def function_delete(
|
|
584
|
+
self, library_name: TEncodable, route: Optional[Route] = None
|
|
585
|
+
) -> TOK:
|
|
586
|
+
"""
|
|
587
|
+
Deletes a library and all its functions.
|
|
588
|
+
|
|
589
|
+
See [valkey.io](https://valkey.io/commands/function-delete/) for more details.
|
|
590
|
+
|
|
591
|
+
Args:
|
|
592
|
+
library_name (TEncodable): The library name to delete
|
|
593
|
+
route (Optional[Route]): The command will be routed to all primaries, unless `route` is provided,
|
|
594
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
595
|
+
|
|
596
|
+
Returns:
|
|
597
|
+
TOK: A simple `OK`.
|
|
598
|
+
|
|
599
|
+
Examples:
|
|
600
|
+
>>> await client.function_delete("my_lib")
|
|
601
|
+
"OK"
|
|
602
|
+
|
|
603
|
+
Since: Valkey 7.0.0.
|
|
604
|
+
"""
|
|
605
|
+
return cast(
|
|
606
|
+
TOK,
|
|
607
|
+
await self._execute_command(
|
|
608
|
+
RequestType.FunctionDelete,
|
|
609
|
+
[library_name],
|
|
610
|
+
route,
|
|
611
|
+
),
|
|
612
|
+
)
|
|
613
|
+
|
|
614
|
+
async def function_kill(self, route: Optional[Route] = None) -> TOK:
|
|
615
|
+
"""
|
|
616
|
+
Kills a function that is currently executing.
|
|
617
|
+
This command only terminates read-only functions.
|
|
618
|
+
|
|
619
|
+
See [valkey.io](https://valkey.io/commands/function-kill/) for more details.
|
|
620
|
+
|
|
621
|
+
Args:
|
|
622
|
+
route (Optional[Route]): The command will be routed to all nodes, unless `route` is provided,
|
|
623
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
624
|
+
|
|
625
|
+
Returns:
|
|
626
|
+
TOK: A simple `OK`.
|
|
627
|
+
|
|
628
|
+
Examples:
|
|
629
|
+
>>> await client.function_kill()
|
|
630
|
+
"OK"
|
|
631
|
+
|
|
632
|
+
Since: Valkey 7.0.0.
|
|
633
|
+
"""
|
|
634
|
+
return cast(
|
|
635
|
+
TOK,
|
|
636
|
+
await self._execute_command(
|
|
637
|
+
RequestType.FunctionKill,
|
|
638
|
+
[],
|
|
639
|
+
route,
|
|
640
|
+
),
|
|
641
|
+
)
|
|
642
|
+
|
|
643
|
+
async def fcall_route(
|
|
644
|
+
self,
|
|
645
|
+
function: TEncodable,
|
|
646
|
+
arguments: Optional[List[TEncodable]] = None,
|
|
647
|
+
route: Optional[Route] = None,
|
|
648
|
+
) -> TClusterResponse[TResult]:
|
|
649
|
+
"""
|
|
650
|
+
Invokes a previously loaded function.
|
|
651
|
+
|
|
652
|
+
See [valkey.io](https://valkey.io/commands/fcall/) for more details.
|
|
653
|
+
|
|
654
|
+
Args:
|
|
655
|
+
function (TEncodable): The function name.
|
|
656
|
+
arguments (Optional[List[TEncodable]]): A list of `function` arguments. `Arguments`
|
|
657
|
+
should not represent names of keys.
|
|
658
|
+
route (Optional[Route]): The command will be routed to a random primary node, unless `route` is provided, in which
|
|
659
|
+
case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
660
|
+
|
|
661
|
+
Returns:
|
|
662
|
+
TClusterResponse[TResult]: If a single node route is requested,
|
|
663
|
+
returns a Optional[TResult] representing the function's return value.
|
|
664
|
+
|
|
665
|
+
Otherwise, returns a dict of [bytes , Optional[TResult]] where each key contains the address of
|
|
666
|
+
the queried node and the value contains the function's return value.
|
|
667
|
+
|
|
668
|
+
Example:
|
|
669
|
+
>>> await client.fcall(
|
|
670
|
+
... "Deep_Thought",
|
|
671
|
+
... ["Answer", "to", "the", "Ultimate", "Question", "of", "Life,", "the", "Universe,", "and", "Everything"],
|
|
672
|
+
... RandomNode()
|
|
673
|
+
... )
|
|
674
|
+
b'new_value' # Returns the function's return value.
|
|
675
|
+
|
|
676
|
+
Since: Valkey version 7.0.0.
|
|
677
|
+
"""
|
|
678
|
+
args = [function, "0"]
|
|
679
|
+
if arguments is not None:
|
|
680
|
+
args.extend(arguments)
|
|
681
|
+
return cast(
|
|
682
|
+
TClusterResponse[TResult],
|
|
683
|
+
await self._execute_command(RequestType.FCall, args, route),
|
|
684
|
+
)
|
|
685
|
+
|
|
686
|
+
async def fcall_ro_route(
|
|
687
|
+
self,
|
|
688
|
+
function: TEncodable,
|
|
689
|
+
arguments: Optional[List[TEncodable]] = None,
|
|
690
|
+
route: Optional[Route] = None,
|
|
691
|
+
) -> TClusterResponse[TResult]:
|
|
692
|
+
"""
|
|
693
|
+
Invokes a previously loaded read-only function.
|
|
694
|
+
|
|
695
|
+
See [valkey.io](https://valkey.io/commands/fcall_ro) for more details.
|
|
696
|
+
|
|
697
|
+
Args:
|
|
698
|
+
function (TEncodable): The function name.
|
|
699
|
+
arguments (List[TEncodable]): An `array` of `function` arguments. `arguments` should not
|
|
700
|
+
represent names of keys.
|
|
701
|
+
route (Optional[Route]): Specifies the routing configuration of the command. The client
|
|
702
|
+
will route the command to the nodes defined by `route`.
|
|
703
|
+
|
|
704
|
+
Returns:
|
|
705
|
+
TClusterResponse[TResult]: The return value depends on the function that was executed.
|
|
706
|
+
|
|
707
|
+
Examples:
|
|
708
|
+
>>> await client.fcall_ro_route("Deep_Thought", ALL_NODES)
|
|
709
|
+
42 # The return value on the function that was executed
|
|
710
|
+
|
|
711
|
+
Since: Valkey version 7.0.0.
|
|
712
|
+
"""
|
|
713
|
+
args: List[TEncodable] = [function, "0"]
|
|
714
|
+
if arguments is not None:
|
|
715
|
+
args.extend(arguments)
|
|
716
|
+
return cast(
|
|
717
|
+
TClusterResponse[TResult],
|
|
718
|
+
await self._execute_command(RequestType.FCallReadOnly, args, route),
|
|
719
|
+
)
|
|
720
|
+
|
|
721
|
+
async def function_stats(
|
|
722
|
+
self, route: Optional[Route] = None
|
|
723
|
+
) -> TClusterResponse[TFunctionStatsSingleNodeResponse]:
|
|
724
|
+
"""
|
|
725
|
+
Returns information about the function that's currently running and information about the
|
|
726
|
+
available execution engines.
|
|
727
|
+
|
|
728
|
+
See [valkey.io](https://valkey.io/commands/function-stats/) for more details
|
|
729
|
+
|
|
730
|
+
Args:
|
|
731
|
+
route (Optional[Route]): The command will be routed automatically to all nodes, unless `route` is provided, in
|
|
732
|
+
which case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
733
|
+
|
|
734
|
+
Returns:
|
|
735
|
+
TClusterResponse[TFunctionStatsSingleNodeResponse]: A `Mapping` with two keys:
|
|
736
|
+
|
|
737
|
+
- `running_script` with information about the running script.
|
|
738
|
+
- `engines` with information about available engines and their stats.
|
|
739
|
+
|
|
740
|
+
See example for more details.
|
|
741
|
+
|
|
742
|
+
Examples:
|
|
743
|
+
>>> await client.function_stats(RandomNode())
|
|
744
|
+
{
|
|
745
|
+
'running_script': {
|
|
746
|
+
'name': 'foo',
|
|
747
|
+
'command': ['FCALL', 'foo', '0', 'hello'],
|
|
748
|
+
'duration_ms': 7758
|
|
749
|
+
},
|
|
750
|
+
'engines': {
|
|
751
|
+
'LUA': {
|
|
752
|
+
'libraries_count': 1,
|
|
753
|
+
'functions_count': 1,
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
Since: Valkey version 7.0.0.
|
|
759
|
+
"""
|
|
760
|
+
return cast(
|
|
761
|
+
TClusterResponse[TFunctionStatsSingleNodeResponse],
|
|
762
|
+
await self._execute_command(RequestType.FunctionStats, [], route),
|
|
763
|
+
)
|
|
764
|
+
|
|
765
|
+
async def function_dump(
|
|
766
|
+
self, route: Optional[Route] = None
|
|
767
|
+
) -> TClusterResponse[bytes]:
|
|
768
|
+
"""
|
|
769
|
+
Returns the serialized payload of all loaded libraries.
|
|
770
|
+
|
|
771
|
+
See [valkey.io](https://valkey.io/commands/function-dump/) for more details.
|
|
772
|
+
|
|
773
|
+
Args:
|
|
774
|
+
route (Optional[Route]): The command will be routed to a random node, unless
|
|
775
|
+
`route` is provided, in which case the client will route the command to the
|
|
776
|
+
nodes defined by `route`.
|
|
777
|
+
|
|
778
|
+
Returns:
|
|
779
|
+
TClusterResponse[bytes]: The serialized payload of all loaded libraries.
|
|
780
|
+
|
|
781
|
+
Examples:
|
|
782
|
+
>>> payload = await client.function_dump()
|
|
783
|
+
# The serialized payload of all loaded libraries. This response can
|
|
784
|
+
# be used to restore loaded functions on any Valkey instance.
|
|
785
|
+
>>> await client.function_restore(payload)
|
|
786
|
+
"OK" # The serialized dump response was used to restore the libraries.
|
|
787
|
+
|
|
788
|
+
Since: Valkey 7.0.0.
|
|
789
|
+
"""
|
|
790
|
+
return cast(
|
|
791
|
+
TClusterResponse[bytes],
|
|
792
|
+
await self._execute_command(RequestType.FunctionDump, [], route),
|
|
793
|
+
)
|
|
794
|
+
|
|
795
|
+
async def function_restore(
|
|
796
|
+
self,
|
|
797
|
+
payload: TEncodable,
|
|
798
|
+
policy: Optional[FunctionRestorePolicy] = None,
|
|
799
|
+
route: Optional[Route] = None,
|
|
800
|
+
) -> TOK:
|
|
801
|
+
"""
|
|
802
|
+
Restores libraries from the serialized payload returned by the `function_dump` command.
|
|
803
|
+
|
|
804
|
+
See [valkey.io](https://valkey.io/commands/function-restore/) for more details.
|
|
805
|
+
|
|
806
|
+
Args:
|
|
807
|
+
payload (bytes): The serialized data from the `function_dump` command.
|
|
808
|
+
policy (Optional[FunctionRestorePolicy]): A policy for handling existing libraries.
|
|
809
|
+
route (Optional[Route]): The command will be sent to all primaries, unless
|
|
810
|
+
`route` is provided, in which case the client will route the command to the
|
|
811
|
+
nodes defined by `route`.
|
|
812
|
+
|
|
813
|
+
Returns:
|
|
814
|
+
TOK: OK.
|
|
815
|
+
|
|
816
|
+
Examples:
|
|
817
|
+
>>> payload = await client.function_dump()
|
|
818
|
+
# The serialized payload of all loaded libraries. This response can
|
|
819
|
+
# be used to restore loaded functions on any Valkey instance.
|
|
820
|
+
>>> await client.function_restore(payload, AllPrimaries())
|
|
821
|
+
"OK" # The serialized dump response was used to restore the libraries with the specified route.
|
|
822
|
+
>>> await client.function_restore(payload, FunctionRestorePolicy.FLUSH, AllPrimaries())
|
|
823
|
+
"OK" # The serialized dump response was used to restore the libraries with the specified route and policy.
|
|
824
|
+
|
|
825
|
+
Since: Valkey 7.0.0.
|
|
826
|
+
"""
|
|
827
|
+
args: List[TEncodable] = [payload]
|
|
828
|
+
if policy is not None:
|
|
829
|
+
args.append(policy.value)
|
|
830
|
+
|
|
831
|
+
return cast(
|
|
832
|
+
TOK, await self._execute_command(RequestType.FunctionRestore, args, route)
|
|
833
|
+
)
|
|
834
|
+
|
|
835
|
+
async def time(
|
|
836
|
+
self, route: Optional[Route] = None
|
|
837
|
+
) -> TClusterResponse[List[bytes]]:
|
|
838
|
+
"""
|
|
839
|
+
Returns the server time.
|
|
840
|
+
|
|
841
|
+
See [valkey.io](https://valkey.io/commands/time/) for more details.
|
|
842
|
+
|
|
843
|
+
Args:
|
|
844
|
+
route (Optional[Route]): The command will be routed to a random node, unless `route` is provided,
|
|
845
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
846
|
+
|
|
847
|
+
Returns:
|
|
848
|
+
TClusterResponse[Optional[bytes]]: The current server time as a two items `array`:
|
|
849
|
+
A Unix timestamp and the amount of microseconds already elapsed in the current second.
|
|
850
|
+
The returned `array` is in a [Unix timestamp, Microseconds already elapsed] format.
|
|
851
|
+
|
|
852
|
+
When specifying a route other than a single node, response will be::
|
|
853
|
+
|
|
854
|
+
{Address (bytes) : response (List[bytes]) , ... }
|
|
855
|
+
|
|
856
|
+
with type of Dict[bytes, List[bytes]].
|
|
857
|
+
|
|
858
|
+
Examples:
|
|
859
|
+
>>> await client.time()
|
|
860
|
+
[b'1710925775', b'913580']
|
|
861
|
+
>>> await client.time(AllNodes())
|
|
862
|
+
{
|
|
863
|
+
b'addr': [b'1710925775', b'913580'],
|
|
864
|
+
b'addr2': [b'1710925775', b'913580'],
|
|
865
|
+
b'addr3': [b'1710925775', b'913580']
|
|
866
|
+
}
|
|
867
|
+
"""
|
|
868
|
+
return cast(
|
|
869
|
+
TClusterResponse[List[bytes]],
|
|
870
|
+
await self._execute_command(RequestType.Time, [], route),
|
|
871
|
+
)
|
|
872
|
+
|
|
873
|
+
async def lastsave(self, route: Optional[Route] = None) -> TClusterResponse[int]:
|
|
874
|
+
"""
|
|
875
|
+
Returns the Unix time of the last DB save timestamp or startup timestamp if no save was made since then.
|
|
876
|
+
|
|
877
|
+
See [valkey.io](https://valkey.io/commands/lastsave) for more details.
|
|
878
|
+
|
|
879
|
+
Args:
|
|
880
|
+
route (Optional[Route]): The command will be routed to a random node, unless `route` is provided,
|
|
881
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
882
|
+
|
|
883
|
+
Returns:
|
|
884
|
+
TClusterResponse[int]: The Unix time of the last successful DB save.
|
|
885
|
+
|
|
886
|
+
If no route is provided, or a single node route is requested, returns an int representing the Unix time
|
|
887
|
+
of the last successful DB save.
|
|
888
|
+
|
|
889
|
+
Otherwise, returns a dict of [bytes , int] where each key contains the
|
|
890
|
+
address of the queried node and the value contains the Unix time of the last successful DB save.
|
|
891
|
+
|
|
892
|
+
Examples:
|
|
893
|
+
>>> await client.lastsave()
|
|
894
|
+
1710925775 # Unix time of the last DB save
|
|
895
|
+
>>> await client.lastsave(AllNodes())
|
|
896
|
+
{b'addr1': 1710925775, b'addr2': 1710925775, b'addr3': 1710925775} # Unix time of the last DB save on
|
|
897
|
+
# each node
|
|
898
|
+
"""
|
|
899
|
+
return cast(
|
|
900
|
+
TClusterResponse[int],
|
|
901
|
+
await self._execute_command(RequestType.LastSave, [], route),
|
|
902
|
+
)
|
|
903
|
+
|
|
904
|
+
async def publish(
|
|
905
|
+
self,
|
|
906
|
+
message: TEncodable,
|
|
907
|
+
channel: TEncodable,
|
|
908
|
+
sharded: bool = False,
|
|
909
|
+
) -> int:
|
|
910
|
+
"""
|
|
911
|
+
Publish a message on pubsub channel.
|
|
912
|
+
This command aggregates PUBLISH and SPUBLISH commands functionalities.
|
|
913
|
+
The mode is selected using the 'sharded' parameter.
|
|
914
|
+
For both sharded and non-sharded mode, request is routed using hashed channel as key.
|
|
915
|
+
|
|
916
|
+
See [PUBLISH](https://valkey.io/commands/publish) and [SPUBLISH](https://valkey.io/commands/spublish)
|
|
917
|
+
for more details.
|
|
918
|
+
|
|
919
|
+
Args:
|
|
920
|
+
message (TEncodable): Message to publish.
|
|
921
|
+
channel (TEncodable): Channel to publish the message on.
|
|
922
|
+
sharded (bool): Use sharded pubsub mode. Available since Valkey version 7.0.
|
|
923
|
+
|
|
924
|
+
Returns:
|
|
925
|
+
int: Number of subscriptions in that node that received the message.
|
|
926
|
+
|
|
927
|
+
Examples:
|
|
928
|
+
>>> await client.publish("Hi all!", "global-channel", False)
|
|
929
|
+
1 # Published 1 instance of "Hi all!" message on global-channel channel using non-sharded mode
|
|
930
|
+
>>> await client.publish(b"Hi to sharded channel1!", b"channel1", True)
|
|
931
|
+
2 # Published 2 instances of "Hi to sharded channel1!" message on channel1 using sharded mode
|
|
932
|
+
"""
|
|
933
|
+
result = await self._execute_command(
|
|
934
|
+
RequestType.SPublish if sharded else RequestType.Publish, [channel, message]
|
|
935
|
+
)
|
|
936
|
+
return cast(int, result)
|
|
937
|
+
|
|
938
|
+
async def pubsub_shardchannels(
|
|
939
|
+
self, pattern: Optional[TEncodable] = None
|
|
940
|
+
) -> List[bytes]:
|
|
941
|
+
"""
|
|
942
|
+
Lists the currently active shard channels.
|
|
943
|
+
The command is routed to all nodes, and aggregates the response to a single array.
|
|
944
|
+
|
|
945
|
+
See [valkey.io](https://valkey.io/commands/pubsub-shardchannels) for more details.
|
|
946
|
+
|
|
947
|
+
Args:
|
|
948
|
+
pattern (Optional[TEncodable]): A glob-style pattern to match active shard channels.
|
|
949
|
+
If not provided, all active shard channels are returned.
|
|
950
|
+
|
|
951
|
+
Returns:
|
|
952
|
+
List[bytes]: A list of currently active shard channels matching the given pattern.
|
|
953
|
+
If no pattern is specified, all active shard channels are returned.
|
|
954
|
+
|
|
955
|
+
Examples:
|
|
956
|
+
>>> await client.pubsub_shardchannels()
|
|
957
|
+
[b'channel1', b'channel2']
|
|
958
|
+
|
|
959
|
+
>>> await client.pubsub_shardchannels("channel*")
|
|
960
|
+
[b'channel1', b'channel2']
|
|
961
|
+
"""
|
|
962
|
+
command_args = [pattern] if pattern is not None else []
|
|
963
|
+
return cast(
|
|
964
|
+
List[bytes],
|
|
965
|
+
await self._execute_command(RequestType.PubSubShardChannels, command_args),
|
|
966
|
+
)
|
|
967
|
+
|
|
968
|
+
async def pubsub_shardnumsub(
|
|
969
|
+
self, channels: Optional[List[TEncodable]] = None
|
|
970
|
+
) -> Mapping[bytes, int]:
|
|
971
|
+
"""
|
|
972
|
+
Returns the number of subscribers (exclusive of clients subscribed to patterns) for the specified shard channels.
|
|
973
|
+
|
|
974
|
+
Note that it is valid to call this command without channels. In this case, it will just return an empty map.
|
|
975
|
+
The command is routed to all nodes, and aggregates the response to a single map of the channels and their number of
|
|
976
|
+
subscriptions.
|
|
977
|
+
|
|
978
|
+
See [valkey.io](https://valkey.io/commands/pubsub-shardnumsub) for more details.
|
|
979
|
+
|
|
980
|
+
Args:
|
|
981
|
+
channels (Optional[List[TEncodable]]): The list of shard channels to query for the number of subscribers.
|
|
982
|
+
If not provided, returns an empty map.
|
|
983
|
+
|
|
984
|
+
Returns:
|
|
985
|
+
Mapping[bytes, int]: A map where keys are the shard channel names and values are the number of subscribers.
|
|
986
|
+
|
|
987
|
+
Examples:
|
|
988
|
+
>>> await client.pubsub_shardnumsub(["channel1", "channel2"])
|
|
989
|
+
{b'channel1': 3, b'channel2': 5}
|
|
990
|
+
|
|
991
|
+
>>> await client.pubsub_shardnumsub()
|
|
992
|
+
{}
|
|
993
|
+
"""
|
|
994
|
+
return cast(
|
|
995
|
+
Mapping[bytes, int],
|
|
996
|
+
await self._execute_command(
|
|
997
|
+
RequestType.PubSubShardNumSub, channels if channels else []
|
|
998
|
+
),
|
|
999
|
+
)
|
|
1000
|
+
|
|
1001
|
+
async def flushall(
|
|
1002
|
+
self, flush_mode: Optional[FlushMode] = None, route: Optional[Route] = None
|
|
1003
|
+
) -> TOK:
|
|
1004
|
+
"""
|
|
1005
|
+
Deletes all the keys of all the existing databases. This command never fails.
|
|
1006
|
+
|
|
1007
|
+
See [valkey.io](https://valkey.io/commands/flushall) for more details.
|
|
1008
|
+
|
|
1009
|
+
Args:
|
|
1010
|
+
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
|
|
1011
|
+
route (Optional[Route]): The command will be routed to all primary nodes, unless `route` is provided,
|
|
1012
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
1013
|
+
|
|
1014
|
+
Returns:
|
|
1015
|
+
TOK: A simple OK response.
|
|
1016
|
+
|
|
1017
|
+
Examples:
|
|
1018
|
+
>>> await client.flushall(FlushMode.ASYNC)
|
|
1019
|
+
OK # This command never fails.
|
|
1020
|
+
>>> await client.flushall(FlushMode.ASYNC, AllNodes())
|
|
1021
|
+
OK # This command never fails.
|
|
1022
|
+
"""
|
|
1023
|
+
args: List[TEncodable] = []
|
|
1024
|
+
if flush_mode is not None:
|
|
1025
|
+
args.append(flush_mode.value)
|
|
1026
|
+
|
|
1027
|
+
return cast(
|
|
1028
|
+
TOK,
|
|
1029
|
+
await self._execute_command(RequestType.FlushAll, args, route),
|
|
1030
|
+
)
|
|
1031
|
+
|
|
1032
|
+
async def flushdb(
|
|
1033
|
+
self, flush_mode: Optional[FlushMode] = None, route: Optional[Route] = None
|
|
1034
|
+
) -> TOK:
|
|
1035
|
+
"""
|
|
1036
|
+
Deletes all the keys of the currently selected database. This command never fails.
|
|
1037
|
+
|
|
1038
|
+
See [valkey.io](https://valkey.io/commands/flushdb) for more details.
|
|
1039
|
+
|
|
1040
|
+
Args:
|
|
1041
|
+
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
|
|
1042
|
+
route (Optional[Route]): The command will be routed to all primary nodes, unless `route` is provided,
|
|
1043
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
1044
|
+
|
|
1045
|
+
Returns:
|
|
1046
|
+
TOK: A simple OK response.
|
|
1047
|
+
|
|
1048
|
+
Examples:
|
|
1049
|
+
>>> await client.flushdb()
|
|
1050
|
+
OK # The keys of the currently selected database were deleted.
|
|
1051
|
+
>>> await client.flushdb(FlushMode.ASYNC)
|
|
1052
|
+
OK # The keys of the currently selected database were deleted asynchronously.
|
|
1053
|
+
>>> await client.flushdb(FlushMode.ASYNC, AllNodes())
|
|
1054
|
+
OK # The keys of the currently selected database were deleted asynchronously on all nodes.
|
|
1055
|
+
"""
|
|
1056
|
+
args: List[TEncodable] = []
|
|
1057
|
+
if flush_mode is not None:
|
|
1058
|
+
args.append(flush_mode.value)
|
|
1059
|
+
|
|
1060
|
+
return cast(
|
|
1061
|
+
TOK,
|
|
1062
|
+
await self._execute_command(RequestType.FlushDB, args, route),
|
|
1063
|
+
)
|
|
1064
|
+
|
|
1065
|
+
async def copy(
|
|
1066
|
+
self,
|
|
1067
|
+
source: TEncodable,
|
|
1068
|
+
destination: TEncodable,
|
|
1069
|
+
# TODO next major release the arguments replace and destinationDB must have their order
|
|
1070
|
+
# swapped to align with the standalone order.
|
|
1071
|
+
# At the moment of the patch release 2.1.1. we can't have a breaking change
|
|
1072
|
+
replace: Optional[bool] = None,
|
|
1073
|
+
destinationDB: Optional[int] = None,
|
|
1074
|
+
) -> bool:
|
|
1075
|
+
"""
|
|
1076
|
+
Copies the value stored at the `source` to the `destination` key. If `destinationDB`
|
|
1077
|
+
is specified, the value will be copied to the database specified by `destinationDB`,
|
|
1078
|
+
otherwise the current database will be used. When `replace` is True, removes the
|
|
1079
|
+
`destination` key first if it already exists, otherwise performs no action.
|
|
1080
|
+
|
|
1081
|
+
See [valkey.io](https://valkey.io/commands/copy) for more details.
|
|
1082
|
+
|
|
1083
|
+
Note:
|
|
1084
|
+
Both `source` and `destination` must map to the same hash slot.
|
|
1085
|
+
|
|
1086
|
+
Args:
|
|
1087
|
+
source (TEncodable): The key to the source value.
|
|
1088
|
+
destination (TEncodable): The key where the value should be copied to.
|
|
1089
|
+
replace (Optional[bool]): If the destination key should be removed before copying the value to it.
|
|
1090
|
+
destinationDB (Optional[int]): The alternative logical database index for the destination key.
|
|
1091
|
+
|
|
1092
|
+
Returns:
|
|
1093
|
+
bool: True if the source was copied. Otherwise, returns False.
|
|
1094
|
+
|
|
1095
|
+
Examples:
|
|
1096
|
+
>>> await client.set("source", "sheep")
|
|
1097
|
+
>>> await client.copy(b"source", b"destination")
|
|
1098
|
+
True # Source was copied
|
|
1099
|
+
>>> await client.copy(b"source", b"destination", destinationDB=1)
|
|
1100
|
+
True # Source was copied to DB 1
|
|
1101
|
+
>>> await client.select(1)
|
|
1102
|
+
>>> await client.get("destination")
|
|
1103
|
+
b"sheep"
|
|
1104
|
+
|
|
1105
|
+
Since: Valkey version 6.2.0.
|
|
1106
|
+
The destinationDB argument is available since Valkey 9.0.0
|
|
1107
|
+
"""
|
|
1108
|
+
|
|
1109
|
+
# Build command arguments
|
|
1110
|
+
args: List[TEncodable] = [source, destination]
|
|
1111
|
+
if destinationDB is not None:
|
|
1112
|
+
args.extend(["DB", str(destinationDB)])
|
|
1113
|
+
if replace is True:
|
|
1114
|
+
args.append("REPLACE")
|
|
1115
|
+
return cast(
|
|
1116
|
+
bool,
|
|
1117
|
+
await self._execute_command(RequestType.Copy, args),
|
|
1118
|
+
)
|
|
1119
|
+
|
|
1120
|
+
async def lolwut(
|
|
1121
|
+
self,
|
|
1122
|
+
version: Optional[int] = None,
|
|
1123
|
+
parameters: Optional[List[int]] = None,
|
|
1124
|
+
route: Optional[Route] = None,
|
|
1125
|
+
) -> TClusterResponse[bytes]:
|
|
1126
|
+
"""
|
|
1127
|
+
Displays a piece of generative computer art and the Valkey version.
|
|
1128
|
+
|
|
1129
|
+
See [valkey.io](https://valkey.io/commands/lolwut) for more details.
|
|
1130
|
+
|
|
1131
|
+
Args:
|
|
1132
|
+
version (Optional[int]): Version of computer art to generate.
|
|
1133
|
+
parameters (Optional[List[int]]): Additional set of arguments in order to change the output:
|
|
1134
|
+
|
|
1135
|
+
- For version `5`, those are length of the line, number of squares per row, and number of squares per column.
|
|
1136
|
+
- For version `6`, those are number of columns and number of lines.
|
|
1137
|
+
|
|
1138
|
+
route (Optional[Route]): The command will be routed to a random node, unless `route` is provided,
|
|
1139
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
1140
|
+
|
|
1141
|
+
Returns:
|
|
1142
|
+
TClusterResponse[bytes]: A piece of generative computer art along with the current Valkey version.
|
|
1143
|
+
|
|
1144
|
+
When specifying a route other than a single node, response will be::
|
|
1145
|
+
|
|
1146
|
+
{Address (bytes) : response (bytes) , ... }
|
|
1147
|
+
|
|
1148
|
+
with type of Dict[bytes, bytes].
|
|
1149
|
+
|
|
1150
|
+
Examples:
|
|
1151
|
+
>>> await client.lolwut(6, [40, 20], RandomNode());
|
|
1152
|
+
b"Redis ver. 7.2.3" # Indicates the current Valkey version
|
|
1153
|
+
"""
|
|
1154
|
+
args: List[TEncodable] = []
|
|
1155
|
+
if version is not None:
|
|
1156
|
+
args.extend(["VERSION", str(version)])
|
|
1157
|
+
if parameters:
|
|
1158
|
+
for var in parameters:
|
|
1159
|
+
args.append(str(var))
|
|
1160
|
+
return cast(
|
|
1161
|
+
TClusterResponse[bytes],
|
|
1162
|
+
await self._execute_command(RequestType.Lolwut, args, route),
|
|
1163
|
+
)
|
|
1164
|
+
|
|
1165
|
+
async def random_key(self, route: Optional[Route] = None) -> Optional[bytes]:
|
|
1166
|
+
"""
|
|
1167
|
+
Returns a random existing key name.
|
|
1168
|
+
|
|
1169
|
+
See [valkey.io](https://valkey.io/commands/randomkey) for more details.
|
|
1170
|
+
|
|
1171
|
+
Args:
|
|
1172
|
+
route (Optional[Route]): The command will be routed to all primary nodes, unless `route` is provided,
|
|
1173
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
1174
|
+
|
|
1175
|
+
Returns:
|
|
1176
|
+
Optional[bytes]: A random existing key name.
|
|
1177
|
+
|
|
1178
|
+
Examples:
|
|
1179
|
+
>>> await client.random_key()
|
|
1180
|
+
b"random_key_name" # "random_key_name" is a random existing key name.
|
|
1181
|
+
"""
|
|
1182
|
+
return cast(
|
|
1183
|
+
Optional[bytes],
|
|
1184
|
+
await self._execute_command(RequestType.RandomKey, [], route),
|
|
1185
|
+
)
|
|
1186
|
+
|
|
1187
|
+
async def wait(
|
|
1188
|
+
self,
|
|
1189
|
+
numreplicas: int,
|
|
1190
|
+
timeout: int,
|
|
1191
|
+
route: Optional[Route] = None,
|
|
1192
|
+
) -> int:
|
|
1193
|
+
"""
|
|
1194
|
+
Blocks the current client until all the previous write commands are successfully transferred
|
|
1195
|
+
and acknowledged by at least `numreplicas` of replicas. If `timeout` is
|
|
1196
|
+
reached, the command returns even if the specified number of replicas were not yet reached.
|
|
1197
|
+
|
|
1198
|
+
See [valkey.io](https://valkey.io/commands/wait) for more details.
|
|
1199
|
+
|
|
1200
|
+
Args:
|
|
1201
|
+
numreplicas (int): The number of replicas to reach.
|
|
1202
|
+
timeout (int): The timeout value specified in milliseconds. A value of 0 will block indefinitely.
|
|
1203
|
+
route (Optional[Route]): The command will be routed to all primary nodes, unless `route` is provided,
|
|
1204
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
1205
|
+
|
|
1206
|
+
Returns:
|
|
1207
|
+
int: The number of replicas reached by all the writes performed in the context of the current connection.
|
|
1208
|
+
|
|
1209
|
+
Examples:
|
|
1210
|
+
>>> await client.set("key", "value");
|
|
1211
|
+
>>> await client.wait(1, 1000);
|
|
1212
|
+
# return 1 when a replica is reached or 0 if 1000ms is reached.
|
|
1213
|
+
"""
|
|
1214
|
+
args: List[TEncodable] = [str(numreplicas), str(timeout)]
|
|
1215
|
+
return cast(
|
|
1216
|
+
int,
|
|
1217
|
+
await self._execute_command(RequestType.Wait, args, route),
|
|
1218
|
+
)
|
|
1219
|
+
|
|
1220
|
+
async def unwatch(self, route: Optional[Route] = None) -> TOK:
|
|
1221
|
+
"""
|
|
1222
|
+
Flushes all the previously watched keys for a transaction. Executing a transaction will
|
|
1223
|
+
automatically flush all previously watched keys.
|
|
1224
|
+
|
|
1225
|
+
See [valkey.io](https://valkey.io/commands/unwatch) for more details.
|
|
1226
|
+
|
|
1227
|
+
Args:
|
|
1228
|
+
route (Optional[Route]): The command will be routed to all primary nodes, unless `route` is provided,
|
|
1229
|
+
in which case the client will route the command to the nodes defined by `route`.
|
|
1230
|
+
|
|
1231
|
+
Returns:
|
|
1232
|
+
TOK: A simple "OK" response.
|
|
1233
|
+
|
|
1234
|
+
Examples:
|
|
1235
|
+
>>> await client.unwatch()
|
|
1236
|
+
'OK'
|
|
1237
|
+
"""
|
|
1238
|
+
return cast(
|
|
1239
|
+
TOK,
|
|
1240
|
+
await self._execute_command(RequestType.UnWatch, [], route),
|
|
1241
|
+
)
|
|
1242
|
+
|
|
1243
|
+
async def scan(
|
|
1244
|
+
self,
|
|
1245
|
+
cursor: ClusterScanCursor,
|
|
1246
|
+
match: Optional[TEncodable] = None,
|
|
1247
|
+
count: Optional[int] = None,
|
|
1248
|
+
type: Optional[ObjectType] = None,
|
|
1249
|
+
allow_non_covered_slots: bool = False,
|
|
1250
|
+
) -> List[Union[ClusterScanCursor, List[bytes]]]:
|
|
1251
|
+
"""
|
|
1252
|
+
Incrementally iterates over the keys in the cluster.
|
|
1253
|
+
The method returns a list containing the next cursor and a list of keys.
|
|
1254
|
+
|
|
1255
|
+
This command is similar to the SCAN command but is designed to work in a cluster environment.
|
|
1256
|
+
For each iteration, the new cursor object should be used to continue the scan.
|
|
1257
|
+
Using the same cursor object for multiple iterations will result in the same keys or unexpected behavior.
|
|
1258
|
+
For more information about the Cluster Scan implementation, see
|
|
1259
|
+
[Cluster Scan](https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#cluster-scan).
|
|
1260
|
+
|
|
1261
|
+
Like the SCAN command, the method can be used to iterate over the keys in the database,
|
|
1262
|
+
returning all keys the database has from when the scan started until the scan ends.
|
|
1263
|
+
The same key can be returned in multiple scan iterations.
|
|
1264
|
+
|
|
1265
|
+
See [valkey.io](https://valkey.io/commands/scan/) for more details.
|
|
1266
|
+
|
|
1267
|
+
Args:
|
|
1268
|
+
cursor (ClusterScanCursor): The cursor object that wraps the scan state.
|
|
1269
|
+
To start a new scan, create a new empty ClusterScanCursor using ClusterScanCursor().
|
|
1270
|
+
match (Optional[TEncodable]): A pattern to match keys against.
|
|
1271
|
+
count (Optional[int]): The number of keys to return in a single iteration.
|
|
1272
|
+
The actual number returned can vary and is not guaranteed to match this count exactly.
|
|
1273
|
+
This parameter serves as a hint to the server on the number of steps to perform in each iteration.
|
|
1274
|
+
The default value is 10.
|
|
1275
|
+
type (Optional[ObjectType]): The type of object to scan for.
|
|
1276
|
+
allow_non_covered_slots (bool): If set to True, the scan will perform even if some slots are not covered by any
|
|
1277
|
+
node.
|
|
1278
|
+
It's important to note that when set to True, the scan has no guarantee to cover all keys in the cluster,
|
|
1279
|
+
and the method loses its way to validate the progress of the scan. Defaults to False.
|
|
1280
|
+
|
|
1281
|
+
Returns:
|
|
1282
|
+
List[Union[ClusterScanCursor, List[TEncodable]]]: A list containing the next cursor and a list of keys,
|
|
1283
|
+
formatted as [ClusterScanCursor, [key1, key2, ...]].
|
|
1284
|
+
|
|
1285
|
+
Examples:
|
|
1286
|
+
>>> # Iterate over all keys in the cluster.
|
|
1287
|
+
>>> await client.mset({b'key1': b'value1', b'key2': b'value2', b'key3': b'value3'})
|
|
1288
|
+
>>> cursor = ClusterScanCursor()
|
|
1289
|
+
>>> all_keys = []
|
|
1290
|
+
>>> while not cursor.is_finished():
|
|
1291
|
+
>>> cursor, keys = await client.scan(cursor, count=10, allow_non_covered_slots=False)
|
|
1292
|
+
>>> all_keys.extend(keys)
|
|
1293
|
+
>>> print(all_keys) # [b'key1', b'key2', b'key3']
|
|
1294
|
+
|
|
1295
|
+
>>> # Iterate over keys matching the pattern "*key*".
|
|
1296
|
+
>>> await client.mset(
|
|
1297
|
+
... {
|
|
1298
|
+
... b"key1": b"value1",
|
|
1299
|
+
... b"key2": b"value2",
|
|
1300
|
+
... b"not_my_key": b"value3",
|
|
1301
|
+
... b"something_else": b"value4"
|
|
1302
|
+
... }
|
|
1303
|
+
... )
|
|
1304
|
+
>>> cursor = ClusterScanCursor()
|
|
1305
|
+
>>> all_keys = []
|
|
1306
|
+
>>> while not cursor.is_finished():
|
|
1307
|
+
>>> cursor, keys = await client.scan(cursor, match=b"*key*", count=10, allow_non_covered_slots=False)
|
|
1308
|
+
>>> all_keys.extend(keys)
|
|
1309
|
+
>>> print(all_keys) # [b'key1', b'key2', b'not_my_key']
|
|
1310
|
+
|
|
1311
|
+
>>> # Iterate over keys of type STRING.
|
|
1312
|
+
>>> await client.mset({b'key1': b'value1', b'key2': b'value2', b'key3': b'value3'})
|
|
1313
|
+
>>> await client.sadd(b"this_is_a_set", [b"value4"])
|
|
1314
|
+
>>> cursor = ClusterScanCursor()
|
|
1315
|
+
>>> all_keys = []
|
|
1316
|
+
>>> while not cursor.is_finished():
|
|
1317
|
+
>>> cursor, keys = await client.scan(cursor, type=ObjectType.STRING, allow_non_covered_slots=False)
|
|
1318
|
+
>>> all_keys.extend(keys)
|
|
1319
|
+
>>> print(all_keys) # [b'key1', b'key2', b'key3']
|
|
1320
|
+
"""
|
|
1321
|
+
return cast(
|
|
1322
|
+
List[Union[ClusterScanCursor, List[bytes]]],
|
|
1323
|
+
await self._cluster_scan(
|
|
1324
|
+
cursor=cursor,
|
|
1325
|
+
match=match,
|
|
1326
|
+
count=count,
|
|
1327
|
+
type=type,
|
|
1328
|
+
allow_non_covered_slots=allow_non_covered_slots,
|
|
1329
|
+
),
|
|
1330
|
+
)
|
|
1331
|
+
|
|
1332
|
+
async def script_exists(
|
|
1333
|
+
self, sha1s: List[TEncodable], route: Optional[Route] = None
|
|
1334
|
+
) -> TClusterResponse[List[bool]]:
|
|
1335
|
+
"""
|
|
1336
|
+
Check existence of scripts in the script cache by their SHA1 digest.
|
|
1337
|
+
|
|
1338
|
+
See [valkey.io](https://valkey.io/commands/script-exists) for more details.
|
|
1339
|
+
|
|
1340
|
+
Args:
|
|
1341
|
+
sha1s (List[TEncodable]): List of SHA1 digests of the scripts to check.
|
|
1342
|
+
route (Optional[Route]): The command will be routed to all primary nodes, unless `route` is provided, in which
|
|
1343
|
+
case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
1344
|
+
|
|
1345
|
+
Returns:
|
|
1346
|
+
TClusterResponse[List[bool]]: A list of boolean values indicating the existence of each script.
|
|
1347
|
+
|
|
1348
|
+
Examples:
|
|
1349
|
+
>>> lua_script = Script("return { KEYS[1], ARGV[1] }")
|
|
1350
|
+
>>> await client.script_exists([lua_script.get_hash(), "sha1_digest2"])
|
|
1351
|
+
[True, False]
|
|
1352
|
+
"""
|
|
1353
|
+
return cast(
|
|
1354
|
+
TClusterResponse[List[bool]],
|
|
1355
|
+
await self._execute_command(RequestType.ScriptExists, sha1s, route),
|
|
1356
|
+
)
|
|
1357
|
+
|
|
1358
|
+
async def script_flush(
|
|
1359
|
+
self, mode: Optional[FlushMode] = None, route: Optional[Route] = None
|
|
1360
|
+
) -> TOK:
|
|
1361
|
+
"""
|
|
1362
|
+
Flush the Lua scripts cache.
|
|
1363
|
+
|
|
1364
|
+
See [valkey.io](https://valkey.io/commands/script-flush) for more details.
|
|
1365
|
+
|
|
1366
|
+
Args:
|
|
1367
|
+
mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
|
|
1368
|
+
route (Optional[Route]): The command will be routed automatically to all nodes, unless `route` is provided, in
|
|
1369
|
+
which case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
1370
|
+
|
|
1371
|
+
Returns:
|
|
1372
|
+
TOK: A simple `OK` response.
|
|
1373
|
+
|
|
1374
|
+
Examples:
|
|
1375
|
+
>>> await client.script_flush()
|
|
1376
|
+
"OK"
|
|
1377
|
+
|
|
1378
|
+
>>> await client.script_flush(FlushMode.ASYNC)
|
|
1379
|
+
"OK"
|
|
1380
|
+
"""
|
|
1381
|
+
|
|
1382
|
+
return cast(
|
|
1383
|
+
TOK,
|
|
1384
|
+
await self._execute_command(
|
|
1385
|
+
RequestType.ScriptFlush, [mode.value] if mode else [], route
|
|
1386
|
+
),
|
|
1387
|
+
)
|
|
1388
|
+
|
|
1389
|
+
async def script_kill(self, route: Optional[Route] = None) -> TOK:
|
|
1390
|
+
"""
|
|
1391
|
+
Kill the currently executing Lua script, assuming no write operation was yet performed by the script.
|
|
1392
|
+
The command is routed to all nodes, and aggregates the response to a single array.
|
|
1393
|
+
|
|
1394
|
+
See [valkey.io](https://valkey.io/commands/script-kill) for more details.
|
|
1395
|
+
|
|
1396
|
+
Args:
|
|
1397
|
+
route (Optional[Route]): The command will be routed automatically to all nodes, unless `route` is provided, in
|
|
1398
|
+
which case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
1399
|
+
|
|
1400
|
+
Returns:
|
|
1401
|
+
TOK: A simple `OK` response.
|
|
1402
|
+
|
|
1403
|
+
Examples:
|
|
1404
|
+
>>> await client.script_kill()
|
|
1405
|
+
"OK"
|
|
1406
|
+
"""
|
|
1407
|
+
return cast(TOK, await self._execute_command(RequestType.ScriptKill, [], route))
|
|
1408
|
+
|
|
1409
|
+
async def invoke_script(
|
|
1410
|
+
self,
|
|
1411
|
+
script: Script,
|
|
1412
|
+
keys: Optional[List[TEncodable]] = None,
|
|
1413
|
+
args: Optional[List[TEncodable]] = None,
|
|
1414
|
+
) -> TClusterResponse[TResult]:
|
|
1415
|
+
"""
|
|
1416
|
+
Invokes a Lua script with its keys and arguments.
|
|
1417
|
+
This method simplifies the process of invoking scripts on a server by using an object that represents a Lua script.
|
|
1418
|
+
The script loading, argument preparation, and execution will all be handled internally.
|
|
1419
|
+
If the script has not already been loaded, it will be loaded automatically using the `SCRIPT LOAD` command.
|
|
1420
|
+
After that, it will be invoked using the `EVALSHA` command.
|
|
1421
|
+
|
|
1422
|
+
Note:
|
|
1423
|
+
When in cluster mode, each `key` must map to the same hash slot.
|
|
1424
|
+
|
|
1425
|
+
See [SCRIPT LOAD](https://valkey.io/commands/script-load/) and [EVALSHA](https://valkey.io/commands/evalsha/)
|
|
1426
|
+
for more details.
|
|
1427
|
+
|
|
1428
|
+
Args:
|
|
1429
|
+
script (Script): The Lua script to execute.
|
|
1430
|
+
keys (Optional[List[TEncodable]]): The keys that are used in the script. To ensure the correct execution of
|
|
1431
|
+
the script, all names of keys that a script accesses must be explicitly provided as `keys`.
|
|
1432
|
+
args (Optional[List[TEncodable]]): The non-key arguments for the script.
|
|
1433
|
+
|
|
1434
|
+
Returns:
|
|
1435
|
+
TResult: a value that depends on the script that was executed.
|
|
1436
|
+
|
|
1437
|
+
Examples:
|
|
1438
|
+
>>> lua_script = Script("return { KEYS[1], ARGV[1] }")
|
|
1439
|
+
>>> await client.invoke_script(lua_script, keys=["foo"], args=["bar"])
|
|
1440
|
+
[b"foo", b"bar"]
|
|
1441
|
+
"""
|
|
1442
|
+
return await self._execute_script(script.get_hash(), keys, args)
|
|
1443
|
+
|
|
1444
|
+
async def invoke_script_route(
|
|
1445
|
+
self,
|
|
1446
|
+
script: Script,
|
|
1447
|
+
args: Optional[List[TEncodable]] = None,
|
|
1448
|
+
route: Optional[Route] = None,
|
|
1449
|
+
) -> TClusterResponse[TResult]:
|
|
1450
|
+
"""
|
|
1451
|
+
Invokes a Lua script with its arguments and route.
|
|
1452
|
+
This method simplifies the process of invoking scripts on a server by using an object that represents a Lua script.
|
|
1453
|
+
The script loading, argument preparation, and execution will all be handled internally.
|
|
1454
|
+
If the script has not already been loaded, it will be loaded automatically using the `SCRIPT LOAD` command.
|
|
1455
|
+
After that, it will be invoked using the `EVALSHA` command.
|
|
1456
|
+
|
|
1457
|
+
See [SCRIPT LOAD](https://valkey.io/commands/script-load/) and [EVALSHA](https://valkey.io/commands/evalsha/)
|
|
1458
|
+
for more details.
|
|
1459
|
+
|
|
1460
|
+
Args:
|
|
1461
|
+
script (Script): The Lua script to execute.
|
|
1462
|
+
args (Optional[List[TEncodable]]): The non-key arguments for the script.
|
|
1463
|
+
route (Optional[Route]): The command will be routed automatically to a random node, unless `route` is provided, in
|
|
1464
|
+
which case the client will route the command to the nodes defined by `route`. Defaults to None.
|
|
1465
|
+
|
|
1466
|
+
Returns:
|
|
1467
|
+
TResult: a value that depends on the script that was executed.
|
|
1468
|
+
|
|
1469
|
+
Examples:
|
|
1470
|
+
>>> lua_script = Script("return { ARGV[1] }")
|
|
1471
|
+
>>> await client.invoke_script(lua_script, args=["bar"], route=AllPrimaries());
|
|
1472
|
+
[b"bar"]
|
|
1473
|
+
"""
|
|
1474
|
+
return await self._execute_script(
|
|
1475
|
+
script.get_hash(), keys=None, args=args, route=route
|
|
1476
|
+
)
|