valkey-glide 2.2.2__cp314-cp314-manylinux_2_17_x86_64.manylinux2014_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.
- 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-314-x86_64-linux-gnu.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.2.dist-info/METADATA +211 -0
- valkey_glide-2.2.2.dist-info/RECORD +40 -0
- valkey_glide-2.2.2.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,1001 @@
|
|
|
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 Script
|
|
8
|
+
from glide_shared.commands.batch import Batch
|
|
9
|
+
from glide_shared.commands.batch_options import BatchOptions
|
|
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
|
+
TEncodable,
|
|
19
|
+
TFunctionListResponse,
|
|
20
|
+
TFunctionStatsFullResponse,
|
|
21
|
+
TResult,
|
|
22
|
+
)
|
|
23
|
+
from glide_shared.protobuf.command_request_pb2 import RequestType
|
|
24
|
+
|
|
25
|
+
from .core import CoreCommands
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class StandaloneCommands(CoreCommands):
|
|
29
|
+
async def custom_command(self, command_args: List[TEncodable]) -> TResult:
|
|
30
|
+
"""
|
|
31
|
+
Executes a single command, without checking inputs.
|
|
32
|
+
See the [Valkey GLIDE Wiki](https://github.com/valkey-io/valkey-glide/wiki/General-Concepts#custom-command)
|
|
33
|
+
for details on the restrictions and limitations of the custom command API.
|
|
34
|
+
|
|
35
|
+
This function should only be used for single-response commands. Commands that don't return complete response and awaits
|
|
36
|
+
(such as SUBSCRIBE), or that return potentially more than a single response (such as XREAD), or that change the
|
|
37
|
+
client's behavior (such as entering pub/sub mode on RESP2 connections) shouldn't be called using this function.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
command_args (List[TEncodable]): List of the command's arguments, where each argument is either a string or bytes.
|
|
41
|
+
Every part of the command, including the command name and subcommands, should be added as a separate value in args.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
TResult: The returning value depends on the executed command.
|
|
45
|
+
|
|
46
|
+
Example:
|
|
47
|
+
>>> await client.customCommand(["CLIENT", "LIST", "TYPE", "PUBSUB"])
|
|
48
|
+
# Expected Output: A list of all pub/sub clients
|
|
49
|
+
|
|
50
|
+
"""
|
|
51
|
+
return await self._execute_command(RequestType.CustomCommand, command_args)
|
|
52
|
+
|
|
53
|
+
async def info(
|
|
54
|
+
self,
|
|
55
|
+
sections: Optional[List[InfoSection]] = None,
|
|
56
|
+
) -> bytes:
|
|
57
|
+
"""
|
|
58
|
+
Get information and statistics about the server.
|
|
59
|
+
|
|
60
|
+
Starting from server version 7, command supports multiple section arguments.
|
|
61
|
+
|
|
62
|
+
See [valkey.io](https://valkey.io/commands/info/) for details.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
sections (Optional[List[InfoSection]]): A list of InfoSection values specifying which sections of
|
|
66
|
+
information to retrieve. When no parameter is provided, the default option is assumed.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
bytes: Returns bytes containing the information for the sections requested.
|
|
70
|
+
"""
|
|
71
|
+
args: List[TEncodable] = (
|
|
72
|
+
[section.value for section in sections] if sections else []
|
|
73
|
+
)
|
|
74
|
+
return cast(bytes, await self._execute_command(RequestType.Info, args))
|
|
75
|
+
|
|
76
|
+
async def exec(
|
|
77
|
+
self,
|
|
78
|
+
batch: Batch,
|
|
79
|
+
raise_on_error: bool,
|
|
80
|
+
options: Optional[BatchOptions] = None,
|
|
81
|
+
) -> Optional[List[TResult]]:
|
|
82
|
+
"""
|
|
83
|
+
Executes a batch by processing the queued commands.
|
|
84
|
+
|
|
85
|
+
See [Valkey Transactions (Atomic Batches)](https://valkey.io/docs/topics/transactions/) and
|
|
86
|
+
[Valkey Pipelines (Non-Atomic Batches)](https://valkey.io/docs/topics/pipelining/) for details.
|
|
87
|
+
|
|
88
|
+
Notes:
|
|
89
|
+
- Atomic Batches - Transactions: If the transaction fails due to a ``WATCH`` command,
|
|
90
|
+
``exec`` will return ``None``.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
batch (Batch): A ``Batch`` containing the commands to execute.
|
|
94
|
+
raise_on_error (bool): Determines how errors are handled within the batch response.
|
|
95
|
+
When set to ``True``, the first encountered error in the batch will be raised as a
|
|
96
|
+
``RequestError`` exception after all retries and reconnections have been executed.
|
|
97
|
+
When set to ``False``, errors will be included as part of the batch response array, allowing
|
|
98
|
+
the caller to process both successful and failed commands together. In this case, error details
|
|
99
|
+
will be provided as instances of ``RequestError``.
|
|
100
|
+
options (Optional[BatchOptions]): A ``BatchOptions`` object containing execution options.
|
|
101
|
+
|
|
102
|
+
Returns:
|
|
103
|
+
Optional[List[TResult]]: An array of results, where each entry corresponds to a command's execution result.
|
|
104
|
+
If the batch fails due to a ``WATCH`` command, ``exec`` will return ``None``.
|
|
105
|
+
|
|
106
|
+
Example (Atomic Batch - Transaction):
|
|
107
|
+
>>> transaction = Batch(is_atomic=True) # Atomic (Transaction)
|
|
108
|
+
>>> transaction.set("key", "1")
|
|
109
|
+
>>> transaction.incr("key")
|
|
110
|
+
>>> transaction.get("key")
|
|
111
|
+
>>> result = await client.exec(transaction, raise_on_error=True)
|
|
112
|
+
>>> print(f"Transaction Batch Result: {result}")
|
|
113
|
+
# Expected Output: Transaction Batch Result: [OK, 2, b'2']
|
|
114
|
+
|
|
115
|
+
Example (Non-Atomic Batch - Pipeline):
|
|
116
|
+
>>> pipeline = Batch(is_atomic=False) # Non-Atomic (Pipeline)
|
|
117
|
+
>>> pipeline.set("key1", "value1")
|
|
118
|
+
>>> pipeline.set("key2", "value2")
|
|
119
|
+
>>> pipeline.get("key1")
|
|
120
|
+
>>> pipeline.get("key2")
|
|
121
|
+
>>> result = await client.exec(pipeline, raise_on_error=True)
|
|
122
|
+
>>> print(f"Pipeline Batch Result: {result}")
|
|
123
|
+
# Expected Output: Pipeline Batch Result: [OK, OK, b'value1', b'value2']
|
|
124
|
+
|
|
125
|
+
Example (Atomic Batch - Transaction with options):
|
|
126
|
+
>>> from glide import BatchOptions
|
|
127
|
+
>>> transaction = Batch(is_atomic=True)
|
|
128
|
+
>>> transaction.set("key", "1")
|
|
129
|
+
>>> transaction.incr("key")
|
|
130
|
+
>>> transaction.custom_command(["get", "key"])
|
|
131
|
+
>>> options = BatchOptions(timeout=1000) # Set a timeout of 1000 milliseconds
|
|
132
|
+
>>> result = await client.exec(
|
|
133
|
+
... transaction,
|
|
134
|
+
... raise_on_error=False, # Do not raise an error on failure
|
|
135
|
+
... options=options
|
|
136
|
+
... )
|
|
137
|
+
>>> print(f"Transaction Result: {result}")
|
|
138
|
+
# Expected Output: Transaction Result: [OK, 2, b'2']
|
|
139
|
+
|
|
140
|
+
Example (Non-Atomic Batch - Pipeline with options):
|
|
141
|
+
>>> from glide import BatchOptions
|
|
142
|
+
>>> pipeline = Batch(is_atomic=False)
|
|
143
|
+
>>> pipeline.custom_command(["set", "key1", "value1"])
|
|
144
|
+
>>> pipeline.custom_command(["set", "key2", "value2"])
|
|
145
|
+
>>> pipeline.custom_command(["get", "key1"])
|
|
146
|
+
>>> pipeline.custom_command(["get", "key2"])
|
|
147
|
+
>>> options = BatchOptions(timeout=1000) # Set a timeout of 1000 milliseconds
|
|
148
|
+
>>> result = await client.exec(
|
|
149
|
+
... pipeline,
|
|
150
|
+
... raise_on_error=False, # Do not raise an error on failure
|
|
151
|
+
... options=options
|
|
152
|
+
... )
|
|
153
|
+
>>> print(f"Pipeline Result: {result}")
|
|
154
|
+
# Expected Output: Pipeline Result: [OK, OK, b'value1', b'value2']
|
|
155
|
+
"""
|
|
156
|
+
commands = batch.commands[:]
|
|
157
|
+
timeout = options.timeout if options else None
|
|
158
|
+
return await self._execute_batch(
|
|
159
|
+
commands,
|
|
160
|
+
is_atomic=batch.is_atomic,
|
|
161
|
+
raise_on_error=raise_on_error,
|
|
162
|
+
timeout=timeout,
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
async def config_resetstat(self) -> TOK:
|
|
166
|
+
"""
|
|
167
|
+
Resets the statistics reported by the server using the INFO and LATENCY HISTOGRAM commands.
|
|
168
|
+
|
|
169
|
+
See [valkey.io](https://valkey.io/commands/config-resetstat/) for details.
|
|
170
|
+
|
|
171
|
+
Returns:
|
|
172
|
+
OK: Returns "OK" to confirm that the statistics were successfully reset.
|
|
173
|
+
"""
|
|
174
|
+
return cast(TOK, await self._execute_command(RequestType.ConfigResetStat, []))
|
|
175
|
+
|
|
176
|
+
async def config_rewrite(self) -> TOK:
|
|
177
|
+
"""
|
|
178
|
+
Rewrite the configuration file with the current configuration.
|
|
179
|
+
|
|
180
|
+
See [valkey.io](https://valkey.io/commands/config-rewrite/) for details.
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
OK: OK is returned when the configuration was rewritten properly.
|
|
184
|
+
|
|
185
|
+
Otherwise, an error is raised.
|
|
186
|
+
"""
|
|
187
|
+
return cast(TOK, await self._execute_command(RequestType.ConfigRewrite, []))
|
|
188
|
+
|
|
189
|
+
async def client_id(
|
|
190
|
+
self,
|
|
191
|
+
) -> int:
|
|
192
|
+
"""
|
|
193
|
+
Returns the current connection id.
|
|
194
|
+
|
|
195
|
+
See [valkey.io](https://valkey.io/commands/client-id/) for more information.
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
int: the id of the client.
|
|
199
|
+
"""
|
|
200
|
+
return cast(int, await self._execute_command(RequestType.ClientId, []))
|
|
201
|
+
|
|
202
|
+
async def ping(self, message: Optional[TEncodable] = None) -> bytes:
|
|
203
|
+
"""
|
|
204
|
+
Ping the server.
|
|
205
|
+
|
|
206
|
+
See [valkey.io](https://valkey.io/commands/ping/) for more details.
|
|
207
|
+
|
|
208
|
+
Args:
|
|
209
|
+
message (Optional[TEncodable]): An optional message to include in the PING command. If not provided,
|
|
210
|
+
the server will respond with b"PONG". If provided, the server will respond with a copy of the message.
|
|
211
|
+
|
|
212
|
+
Returns:
|
|
213
|
+
bytes: b"PONG" if `message` is not provided.
|
|
214
|
+
|
|
215
|
+
Otherwise return a copy of `message`.
|
|
216
|
+
|
|
217
|
+
Examples:
|
|
218
|
+
>>> await client.ping()
|
|
219
|
+
b"PONG"
|
|
220
|
+
>>> await client.ping("Hello")
|
|
221
|
+
b"Hello"
|
|
222
|
+
"""
|
|
223
|
+
argument = [] if message is None else [message]
|
|
224
|
+
return cast(bytes, await self._execute_command(RequestType.Ping, argument))
|
|
225
|
+
|
|
226
|
+
async def config_get(self, parameters: List[TEncodable]) -> Dict[bytes, bytes]:
|
|
227
|
+
"""
|
|
228
|
+
Get the values of configuration parameters.
|
|
229
|
+
Starting from server version 7, command supports multiple parameters
|
|
230
|
+
|
|
231
|
+
See [valkey.io](https://valkey.io/commands/config-get/) for details.
|
|
232
|
+
|
|
233
|
+
Args:
|
|
234
|
+
parameters (List[TEncodable]): A list of configuration parameter names to retrieve values for.
|
|
235
|
+
|
|
236
|
+
Returns:
|
|
237
|
+
Dict[bytes, bytes]: A dictionary of values corresponding to the configuration parameters.
|
|
238
|
+
|
|
239
|
+
Examples:
|
|
240
|
+
>>> await client.config_get(["timeout"] , RandomNode())
|
|
241
|
+
{b'timeout': b'1000'}
|
|
242
|
+
>>> await client.config_get([b"timeout" , "maxmemory"])
|
|
243
|
+
{b'timeout': b'1000', b'maxmemory': b'1GB'}
|
|
244
|
+
"""
|
|
245
|
+
return cast(
|
|
246
|
+
Dict[bytes, bytes],
|
|
247
|
+
await self._execute_command(RequestType.ConfigGet, parameters),
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
async def config_set(self, parameters_map: Mapping[TEncodable, TEncodable]) -> TOK:
|
|
251
|
+
"""
|
|
252
|
+
Set configuration parameters to the specified values.
|
|
253
|
+
Starting from server version 7, command supports multiple parameters.
|
|
254
|
+
|
|
255
|
+
See [valkey.io](https://valkey.io/commands/config-set/) for details.
|
|
256
|
+
|
|
257
|
+
Args:
|
|
258
|
+
parameters_map (Mapping[TEncodable, TEncodable]): A map consisting of configuration
|
|
259
|
+
parameters and their respective values to set.
|
|
260
|
+
|
|
261
|
+
Returns:
|
|
262
|
+
OK: Returns OK if all configurations have been successfully set.
|
|
263
|
+
|
|
264
|
+
Otherwise, raises an error.
|
|
265
|
+
|
|
266
|
+
Examples:
|
|
267
|
+
>>> config_set({"timeout": "1000", "maxmemory": "1GB"})
|
|
268
|
+
OK
|
|
269
|
+
"""
|
|
270
|
+
parameters: List[TEncodable] = []
|
|
271
|
+
for pair in parameters_map.items():
|
|
272
|
+
parameters.extend(pair)
|
|
273
|
+
return cast(TOK, await self._execute_command(RequestType.ConfigSet, parameters))
|
|
274
|
+
|
|
275
|
+
async def client_getname(self) -> Optional[bytes]:
|
|
276
|
+
"""
|
|
277
|
+
Get the name of the primary's connection.
|
|
278
|
+
|
|
279
|
+
See [valkey.io](https://valkey.io/commands/client-getname/) for more details.
|
|
280
|
+
|
|
281
|
+
Returns:
|
|
282
|
+
Optional[bytes]: Returns the name of the client connection as a byte string if a name is set.
|
|
283
|
+
|
|
284
|
+
`None` if no name is assigned.
|
|
285
|
+
|
|
286
|
+
Examples:
|
|
287
|
+
>>> await client.client_getname()
|
|
288
|
+
b'Connection Name'
|
|
289
|
+
"""
|
|
290
|
+
return cast(
|
|
291
|
+
Optional[bytes], await self._execute_command(RequestType.ClientGetName, [])
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
async def dbsize(self) -> int:
|
|
295
|
+
"""
|
|
296
|
+
Returns the number of keys in the currently selected database.
|
|
297
|
+
|
|
298
|
+
See [valkey.io](https://valkey.io/commands/dbsize) for more details.
|
|
299
|
+
|
|
300
|
+
Returns:
|
|
301
|
+
int: The number of keys in the currently selected database.
|
|
302
|
+
|
|
303
|
+
Examples:
|
|
304
|
+
>>> await client.dbsize()
|
|
305
|
+
10 # Indicates there are 10 keys in the current database.
|
|
306
|
+
"""
|
|
307
|
+
return cast(int, await self._execute_command(RequestType.DBSize, []))
|
|
308
|
+
|
|
309
|
+
async def echo(self, message: TEncodable) -> bytes:
|
|
310
|
+
"""
|
|
311
|
+
Echoes the provided `message` back.
|
|
312
|
+
|
|
313
|
+
See [valkey.io](https://valkey.io/commands/echo) for more details.
|
|
314
|
+
|
|
315
|
+
Args:
|
|
316
|
+
message (TEncodable): The message to be echoed back.
|
|
317
|
+
|
|
318
|
+
Returns:
|
|
319
|
+
bytes: The provided `message`.
|
|
320
|
+
|
|
321
|
+
Examples:
|
|
322
|
+
>>> await client.echo("Valkey GLIDE")
|
|
323
|
+
b'Valkey GLIDE'
|
|
324
|
+
"""
|
|
325
|
+
return cast(bytes, await self._execute_command(RequestType.Echo, [message]))
|
|
326
|
+
|
|
327
|
+
async def function_load(
|
|
328
|
+
self, library_code: TEncodable, replace: bool = False
|
|
329
|
+
) -> bytes:
|
|
330
|
+
"""
|
|
331
|
+
Loads a library to Valkey.
|
|
332
|
+
|
|
333
|
+
See [valkey.io](https://valkey.io/commands/function-load/) for more details.
|
|
334
|
+
|
|
335
|
+
Args:
|
|
336
|
+
library_code (TEncodable): The source code that implements the library.
|
|
337
|
+
replace (bool): Whether the given library should overwrite a library with the same name if
|
|
338
|
+
it already exists.
|
|
339
|
+
|
|
340
|
+
Returns:
|
|
341
|
+
bytes: The library name that was loaded.
|
|
342
|
+
|
|
343
|
+
Examples:
|
|
344
|
+
>>> code = "#!lua name=mylib \\n redis.register_function('myfunc', function(keys, args) return args[1] end)"
|
|
345
|
+
>>> await client.function_load(code, True)
|
|
346
|
+
b"mylib"
|
|
347
|
+
|
|
348
|
+
Since: Valkey 7.0.0.
|
|
349
|
+
"""
|
|
350
|
+
return cast(
|
|
351
|
+
bytes,
|
|
352
|
+
await self._execute_command(
|
|
353
|
+
RequestType.FunctionLoad,
|
|
354
|
+
["REPLACE", library_code] if replace else [library_code],
|
|
355
|
+
),
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
async def function_list(
|
|
359
|
+
self, library_name_pattern: Optional[TEncodable] = None, with_code: bool = False
|
|
360
|
+
) -> TFunctionListResponse:
|
|
361
|
+
"""
|
|
362
|
+
Returns information about the functions and libraries.
|
|
363
|
+
|
|
364
|
+
See [valkey.io](https://valkey.io/commands/function-list/) for more details.
|
|
365
|
+
|
|
366
|
+
Args:
|
|
367
|
+
library_name_pattern (Optional[TEncodable]): A wildcard pattern for matching library names.
|
|
368
|
+
with_code (bool): Specifies whether to request the library code from the server or not.
|
|
369
|
+
|
|
370
|
+
Returns:
|
|
371
|
+
TFunctionListResponse: Info about all or
|
|
372
|
+
selected libraries and their functions.
|
|
373
|
+
|
|
374
|
+
Examples:
|
|
375
|
+
>>> response = await client.function_list("myLib?_backup", True)
|
|
376
|
+
[{
|
|
377
|
+
b"library_name": b"myLib5_backup",
|
|
378
|
+
b"engine": b"LUA",
|
|
379
|
+
b"functions": [{
|
|
380
|
+
b"name": b"myfunc",
|
|
381
|
+
b"description": None,
|
|
382
|
+
b"flags": {b"no-writes"},
|
|
383
|
+
}],
|
|
384
|
+
b"library_code": b"#!lua name=mylib \\n sever.register_function('myfunc', function(keys, args) " \
|
|
385
|
+
b"return args[1] end)"
|
|
386
|
+
}]
|
|
387
|
+
|
|
388
|
+
Since: Valkey 7.0.0.
|
|
389
|
+
"""
|
|
390
|
+
args = []
|
|
391
|
+
if library_name_pattern is not None:
|
|
392
|
+
args.extend(["LIBRARYNAME", library_name_pattern])
|
|
393
|
+
if with_code:
|
|
394
|
+
args.append("WITHCODE")
|
|
395
|
+
return cast(
|
|
396
|
+
TFunctionListResponse,
|
|
397
|
+
await self._execute_command(
|
|
398
|
+
RequestType.FunctionList,
|
|
399
|
+
args,
|
|
400
|
+
),
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
async def function_flush(self, mode: Optional[FlushMode] = None) -> TOK:
|
|
404
|
+
"""
|
|
405
|
+
Deletes all function libraries.
|
|
406
|
+
|
|
407
|
+
See [valkey.io](https://valkey.io/commands/function-flush/) for more details.
|
|
408
|
+
|
|
409
|
+
Args:
|
|
410
|
+
mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
|
|
411
|
+
|
|
412
|
+
Returns:
|
|
413
|
+
TOK: A simple `OK`.
|
|
414
|
+
|
|
415
|
+
Examples:
|
|
416
|
+
>>> await client.function_flush(FlushMode.SYNC)
|
|
417
|
+
"OK"
|
|
418
|
+
|
|
419
|
+
Since: Valkey 7.0.0.
|
|
420
|
+
"""
|
|
421
|
+
return cast(
|
|
422
|
+
TOK,
|
|
423
|
+
await self._execute_command(
|
|
424
|
+
RequestType.FunctionFlush,
|
|
425
|
+
[mode.value] if mode else [],
|
|
426
|
+
),
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
async def function_delete(self, library_name: TEncodable) -> TOK:
|
|
430
|
+
"""
|
|
431
|
+
Deletes a library and all its functions.
|
|
432
|
+
|
|
433
|
+
See [valkey.io](https://valkey.io/commands/function-delete/) for more details.
|
|
434
|
+
|
|
435
|
+
Args:
|
|
436
|
+
library_code (TEncodable): The library name to delete
|
|
437
|
+
|
|
438
|
+
Returns:
|
|
439
|
+
TOK: A simple `OK`.
|
|
440
|
+
|
|
441
|
+
Examples:
|
|
442
|
+
>>> await client.function_delete("my_lib")
|
|
443
|
+
"OK"
|
|
444
|
+
|
|
445
|
+
Since: Valkey 7.0.0.
|
|
446
|
+
"""
|
|
447
|
+
return cast(
|
|
448
|
+
TOK,
|
|
449
|
+
await self._execute_command(
|
|
450
|
+
RequestType.FunctionDelete,
|
|
451
|
+
[library_name],
|
|
452
|
+
),
|
|
453
|
+
)
|
|
454
|
+
|
|
455
|
+
async def function_kill(self) -> TOK:
|
|
456
|
+
"""
|
|
457
|
+
Kills a function that is currently executing.
|
|
458
|
+
This command only terminates read-only functions.
|
|
459
|
+
|
|
460
|
+
FUNCTION KILL runs on all nodes of the server, including primary and replicas.
|
|
461
|
+
|
|
462
|
+
See [valkey.io](https://valkey.io/commands/function-kill/) for more details.
|
|
463
|
+
|
|
464
|
+
Returns:
|
|
465
|
+
TOK: A simple `OK`.
|
|
466
|
+
|
|
467
|
+
Examples:
|
|
468
|
+
>>> await client.function_kill()
|
|
469
|
+
"OK"
|
|
470
|
+
|
|
471
|
+
Since: Valkey 7.0.0.
|
|
472
|
+
"""
|
|
473
|
+
return cast(
|
|
474
|
+
TOK,
|
|
475
|
+
await self._execute_command(RequestType.FunctionKill, []),
|
|
476
|
+
)
|
|
477
|
+
|
|
478
|
+
async def function_stats(self) -> TFunctionStatsFullResponse:
|
|
479
|
+
"""
|
|
480
|
+
Returns information about the function that's currently running and information about the
|
|
481
|
+
available execution engines.
|
|
482
|
+
|
|
483
|
+
FUNCTION STATS runs on all nodes of the server, including primary and replicas.
|
|
484
|
+
The response includes a mapping from node address to the command response for that node.
|
|
485
|
+
|
|
486
|
+
See [valkey.io](https://valkey.io/commands/function-stats/) for more details
|
|
487
|
+
|
|
488
|
+
Returns:
|
|
489
|
+
TFunctionStatsFullResponse: A Map where the key is the node address and the value is a Map of two keys:
|
|
490
|
+
|
|
491
|
+
- `running_script` with information about the running script.
|
|
492
|
+
- `engines` with information about available engines and their stats.
|
|
493
|
+
|
|
494
|
+
See example for more details.
|
|
495
|
+
|
|
496
|
+
Examples:
|
|
497
|
+
>>> await client.function_stats()
|
|
498
|
+
{b"addr": { # Response from the master node
|
|
499
|
+
b'running_script': {
|
|
500
|
+
b'name': b'foo',
|
|
501
|
+
b'command': [b'FCALL', b'foo', b'0', b'hello'],
|
|
502
|
+
b'duration_ms': 7758
|
|
503
|
+
},
|
|
504
|
+
b'engines': {
|
|
505
|
+
b'LUA': {
|
|
506
|
+
b'libraries_count': 1,
|
|
507
|
+
b'functions_count': 1,
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
},
|
|
511
|
+
b"addr2": { # Response from a replica
|
|
512
|
+
b'running_script': None,
|
|
513
|
+
b"engines": {
|
|
514
|
+
b'LUA': {
|
|
515
|
+
b'libraries_count': 1,
|
|
516
|
+
b'functions_count': 1,
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}}
|
|
520
|
+
|
|
521
|
+
Since: Valkey version 7.0.0.
|
|
522
|
+
"""
|
|
523
|
+
return cast(
|
|
524
|
+
TFunctionStatsFullResponse,
|
|
525
|
+
await self._execute_command(RequestType.FunctionStats, []),
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
async def function_dump(self) -> bytes:
|
|
529
|
+
"""
|
|
530
|
+
Returns the serialized payload of all loaded libraries.
|
|
531
|
+
|
|
532
|
+
See [valkey.io](https://valkey.io/docs/latest/commands/function-dump/) for more details.
|
|
533
|
+
|
|
534
|
+
Returns:
|
|
535
|
+
bytes: The serialized payload of all loaded libraries.
|
|
536
|
+
|
|
537
|
+
Examples:
|
|
538
|
+
>>> payload = await client.function_dump()
|
|
539
|
+
# The serialized payload of all loaded libraries. This response can
|
|
540
|
+
# be used to restore loaded functions on any Valkey instance.
|
|
541
|
+
>>> await client.function_restore(payload)
|
|
542
|
+
"OK" # The serialized dump response was used to restore the libraries.
|
|
543
|
+
|
|
544
|
+
Since: Valkey 7.0.0.
|
|
545
|
+
"""
|
|
546
|
+
return cast(bytes, await self._execute_command(RequestType.FunctionDump, []))
|
|
547
|
+
|
|
548
|
+
async def function_restore(
|
|
549
|
+
self, payload: TEncodable, policy: Optional[FunctionRestorePolicy] = None
|
|
550
|
+
) -> TOK:
|
|
551
|
+
"""
|
|
552
|
+
Restores libraries from the serialized payload returned by the `function_dump` command.
|
|
553
|
+
|
|
554
|
+
See [valkey.io](https://valkey.io/docs/latest/commands/function-restore/) for more details.
|
|
555
|
+
|
|
556
|
+
Args:
|
|
557
|
+
payload (TEncodable): The serialized data from the `function_dump` command.
|
|
558
|
+
policy (Optional[FunctionRestorePolicy]): A policy for handling existing libraries.
|
|
559
|
+
|
|
560
|
+
Returns:
|
|
561
|
+
TOK: OK.
|
|
562
|
+
|
|
563
|
+
Examples:
|
|
564
|
+
>>> payload = await client.function_dump()
|
|
565
|
+
# The serialized payload of all loaded libraries. This response can
|
|
566
|
+
# be used to restore loaded functions on any Valkey instance.
|
|
567
|
+
>>> await client.function_restore(payload)
|
|
568
|
+
"OK" # The serialized dump response was used to restore the libraries.
|
|
569
|
+
>>> await client.function_restore(payload, FunctionRestorePolicy.FLUSH)
|
|
570
|
+
"OK" # The serialized dump response was used to restore the libraries with the specified policy.
|
|
571
|
+
|
|
572
|
+
Since: Valkey 7.0.0.
|
|
573
|
+
"""
|
|
574
|
+
args: List[TEncodable] = [payload]
|
|
575
|
+
if policy is not None:
|
|
576
|
+
args.append(policy.value)
|
|
577
|
+
|
|
578
|
+
return cast(TOK, await self._execute_command(RequestType.FunctionRestore, args))
|
|
579
|
+
|
|
580
|
+
async def time(self) -> List[bytes]:
|
|
581
|
+
"""
|
|
582
|
+
Returns the server time.
|
|
583
|
+
|
|
584
|
+
See [valkey.io](https://valkey.io/commands/time/) for more details.
|
|
585
|
+
|
|
586
|
+
Returns:
|
|
587
|
+
List[bytes]: The current server time as a two items `array`:
|
|
588
|
+
A Unix timestamp and the amount of microseconds already elapsed in the current second.
|
|
589
|
+
The returned `array` is in a [Unix timestamp, Microseconds already elapsed] format.
|
|
590
|
+
|
|
591
|
+
Examples:
|
|
592
|
+
>>> await client.time()
|
|
593
|
+
[b'1710925775', b'913580']
|
|
594
|
+
"""
|
|
595
|
+
return cast(
|
|
596
|
+
List[bytes],
|
|
597
|
+
await self._execute_command(RequestType.Time, []),
|
|
598
|
+
)
|
|
599
|
+
|
|
600
|
+
async def lastsave(self) -> int:
|
|
601
|
+
"""
|
|
602
|
+
Returns the Unix time of the last DB save timestamp or startup timestamp if no save was made since then.
|
|
603
|
+
|
|
604
|
+
See [valkey.io](https://valkey.io/commands/lastsave) for more details.
|
|
605
|
+
|
|
606
|
+
Returns:
|
|
607
|
+
int: The Unix time of the last successful DB save.
|
|
608
|
+
|
|
609
|
+
Examples:
|
|
610
|
+
>>> await client.lastsave()
|
|
611
|
+
1710925775 # Unix time of the last DB save
|
|
612
|
+
"""
|
|
613
|
+
return cast(
|
|
614
|
+
int,
|
|
615
|
+
await self._execute_command(RequestType.LastSave, []),
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
async def publish(self, message: TEncodable, channel: TEncodable) -> int:
|
|
619
|
+
"""
|
|
620
|
+
Publish a message on pubsub channel.
|
|
621
|
+
|
|
622
|
+
See [valkey.io](https://valkey.io/commands/publish) for more details.
|
|
623
|
+
|
|
624
|
+
Args:
|
|
625
|
+
message (TEncodable): Message to publish
|
|
626
|
+
channel (TEncodable): Channel to publish the message on.
|
|
627
|
+
|
|
628
|
+
Returns:
|
|
629
|
+
int: Number of subscriptions in primary node that received the message.
|
|
630
|
+
|
|
631
|
+
**Note:** this value does not include subscriptions that configured on replicas.
|
|
632
|
+
|
|
633
|
+
Examples:
|
|
634
|
+
>>> await client.publish("Hi all!", "global-channel")
|
|
635
|
+
1 # This message was posted to 1 subscription which is configured on primary node
|
|
636
|
+
"""
|
|
637
|
+
return cast(
|
|
638
|
+
int, await self._execute_command(RequestType.Publish, [channel, message])
|
|
639
|
+
)
|
|
640
|
+
|
|
641
|
+
async def flushall(self, flush_mode: Optional[FlushMode] = None) -> TOK:
|
|
642
|
+
"""
|
|
643
|
+
Deletes all the keys of all the existing databases. This command never fails.
|
|
644
|
+
|
|
645
|
+
See [valkey.io](https://valkey.io/commands/flushall) for more details.
|
|
646
|
+
|
|
647
|
+
Args:
|
|
648
|
+
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
|
|
649
|
+
|
|
650
|
+
Returns:
|
|
651
|
+
TOK: A simple OK response.
|
|
652
|
+
|
|
653
|
+
Examples:
|
|
654
|
+
>>> await client.flushall(FlushMode.ASYNC)
|
|
655
|
+
OK # This command never fails.
|
|
656
|
+
"""
|
|
657
|
+
args: List[TEncodable] = []
|
|
658
|
+
if flush_mode is not None:
|
|
659
|
+
args.append(flush_mode.value)
|
|
660
|
+
|
|
661
|
+
return cast(
|
|
662
|
+
TOK,
|
|
663
|
+
await self._execute_command(RequestType.FlushAll, args),
|
|
664
|
+
)
|
|
665
|
+
|
|
666
|
+
async def flushdb(self, flush_mode: Optional[FlushMode] = None) -> TOK:
|
|
667
|
+
"""
|
|
668
|
+
Deletes all the keys of the currently selected database. This command never fails.
|
|
669
|
+
|
|
670
|
+
See [valkey.io](https://valkey.io/commands/flushdb) for more details.
|
|
671
|
+
|
|
672
|
+
Args:
|
|
673
|
+
flush_mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
|
|
674
|
+
|
|
675
|
+
Returns:
|
|
676
|
+
TOK: A simple OK response.
|
|
677
|
+
|
|
678
|
+
Examples:
|
|
679
|
+
>>> await client.flushdb()
|
|
680
|
+
OK # The keys of the currently selected database were deleted.
|
|
681
|
+
>>> await client.flushdb(FlushMode.ASYNC)
|
|
682
|
+
OK # The keys of the currently selected database were deleted asynchronously.
|
|
683
|
+
"""
|
|
684
|
+
args: List[TEncodable] = []
|
|
685
|
+
if flush_mode is not None:
|
|
686
|
+
args.append(flush_mode.value)
|
|
687
|
+
|
|
688
|
+
return cast(
|
|
689
|
+
TOK,
|
|
690
|
+
await self._execute_command(RequestType.FlushDB, args),
|
|
691
|
+
)
|
|
692
|
+
|
|
693
|
+
async def copy(
|
|
694
|
+
self,
|
|
695
|
+
source: TEncodable,
|
|
696
|
+
destination: TEncodable,
|
|
697
|
+
destinationDB: Optional[int] = None,
|
|
698
|
+
replace: Optional[bool] = None,
|
|
699
|
+
) -> bool:
|
|
700
|
+
"""
|
|
701
|
+
Copies the value stored at the `source` to the `destination` key. If `destinationDB`
|
|
702
|
+
is specified, the value will be copied to the database specified by `destinationDB`,
|
|
703
|
+
otherwise the current database will be used. When `replace` is True, removes the
|
|
704
|
+
`destination` key first if it already exists, otherwise performs no action.
|
|
705
|
+
|
|
706
|
+
See [valkey.io](https://valkey.io/commands/copy) for more details.
|
|
707
|
+
|
|
708
|
+
Args:
|
|
709
|
+
source (TEncodable): The key to the source value.
|
|
710
|
+
destination (TEncodable): The key where the value should be copied to.
|
|
711
|
+
destinationDB (Optional[int]): The alternative logical database index for the destination key.
|
|
712
|
+
replace (Optional[bool]): If the destination key should be removed before copying the value to it.
|
|
713
|
+
|
|
714
|
+
Returns:
|
|
715
|
+
bool: True if the source was copied.
|
|
716
|
+
|
|
717
|
+
Otherwise, return False.
|
|
718
|
+
|
|
719
|
+
Examples:
|
|
720
|
+
>>> await client.set("source", "sheep")
|
|
721
|
+
>>> await client.copy(b"source", b"destination", 1, False)
|
|
722
|
+
True # Source was copied
|
|
723
|
+
>>> await client.select(1)
|
|
724
|
+
>>> await client.get("destination")
|
|
725
|
+
b"sheep"
|
|
726
|
+
|
|
727
|
+
Since: Valkey version 6.2.0.
|
|
728
|
+
"""
|
|
729
|
+
args: List[TEncodable] = [source, destination]
|
|
730
|
+
if destinationDB is not None:
|
|
731
|
+
args.extend(["DB", str(destinationDB)])
|
|
732
|
+
if replace is True:
|
|
733
|
+
args.append("REPLACE")
|
|
734
|
+
return cast(
|
|
735
|
+
bool,
|
|
736
|
+
await self._execute_command(RequestType.Copy, args),
|
|
737
|
+
)
|
|
738
|
+
|
|
739
|
+
async def lolwut(
|
|
740
|
+
self,
|
|
741
|
+
version: Optional[int] = None,
|
|
742
|
+
parameters: Optional[List[int]] = None,
|
|
743
|
+
) -> bytes:
|
|
744
|
+
"""
|
|
745
|
+
Displays a piece of generative computer art and the Valkey version.
|
|
746
|
+
|
|
747
|
+
See [valkey.io](https://valkey.io/commands/lolwut) for more details.
|
|
748
|
+
|
|
749
|
+
Args:
|
|
750
|
+
version (Optional[int]): Version of computer art to generate.
|
|
751
|
+
parameters (Optional[List[int]]): Additional set of arguments in order to change the output:
|
|
752
|
+
|
|
753
|
+
- For version `5`, those are length of the line, number of squares per row, and number of squares per column.
|
|
754
|
+
- For version `6`, those are number of columns and number of lines.
|
|
755
|
+
|
|
756
|
+
Returns:
|
|
757
|
+
bytes: A piece of generative computer art along with the current Valkey version.
|
|
758
|
+
|
|
759
|
+
Examples:
|
|
760
|
+
>>> await client.lolwut(6, [40, 20]);
|
|
761
|
+
b"Redis ver. 7.2.3" # Indicates the current Valkey version
|
|
762
|
+
>>> await client.lolwut(5, [30, 5, 5]);
|
|
763
|
+
b"Redis ver. 7.2.3" # Indicates the current Valkey version
|
|
764
|
+
"""
|
|
765
|
+
args: List[TEncodable] = []
|
|
766
|
+
if version is not None:
|
|
767
|
+
args.extend(["VERSION", str(version)])
|
|
768
|
+
if parameters:
|
|
769
|
+
for var in parameters:
|
|
770
|
+
args.append(str(var))
|
|
771
|
+
return cast(
|
|
772
|
+
bytes,
|
|
773
|
+
await self._execute_command(RequestType.Lolwut, args),
|
|
774
|
+
)
|
|
775
|
+
|
|
776
|
+
async def random_key(self) -> Optional[bytes]:
|
|
777
|
+
"""
|
|
778
|
+
Returns a random existing key name from the currently selected database.
|
|
779
|
+
|
|
780
|
+
See [valkey.io](https://valkey.io/commands/randomkey) for more details.
|
|
781
|
+
|
|
782
|
+
Returns:
|
|
783
|
+
Optional[bytes]: A random existing key name from the currently selected database.
|
|
784
|
+
|
|
785
|
+
Examples:
|
|
786
|
+
>>> await client.random_key()
|
|
787
|
+
b"random_key_name" # "random_key_name" is a random existing key name from the currently selected database.
|
|
788
|
+
"""
|
|
789
|
+
return cast(
|
|
790
|
+
Optional[bytes],
|
|
791
|
+
await self._execute_command(RequestType.RandomKey, []),
|
|
792
|
+
)
|
|
793
|
+
|
|
794
|
+
async def wait(
|
|
795
|
+
self,
|
|
796
|
+
numreplicas: int,
|
|
797
|
+
timeout: int,
|
|
798
|
+
) -> int:
|
|
799
|
+
"""
|
|
800
|
+
Blocks the current client until all the previous write commands are successfully transferred
|
|
801
|
+
and acknowledged by at least `numreplicas` of replicas. If `timeout` is
|
|
802
|
+
reached, the command returns even if the specified number of replicas were not yet reached.
|
|
803
|
+
|
|
804
|
+
See [valkey.io](https://valkey.io/commands/wait) for more details.
|
|
805
|
+
|
|
806
|
+
Args:
|
|
807
|
+
numreplicas (int): The number of replicas to reach.
|
|
808
|
+
timeout (int): The timeout value specified in milliseconds. A value of 0 will block indefinitely.
|
|
809
|
+
|
|
810
|
+
Returns:
|
|
811
|
+
int: The number of replicas reached by all the writes performed in the context of the current connection.
|
|
812
|
+
|
|
813
|
+
Examples:
|
|
814
|
+
>>> await client.set("key", "value");
|
|
815
|
+
>>> await client.wait(1, 1000);
|
|
816
|
+
# return 1 when a replica is reached or 0 if 1000ms is reached.
|
|
817
|
+
"""
|
|
818
|
+
args: List[TEncodable] = [str(numreplicas), str(timeout)]
|
|
819
|
+
return cast(
|
|
820
|
+
int,
|
|
821
|
+
await self._execute_command(RequestType.Wait, args),
|
|
822
|
+
)
|
|
823
|
+
|
|
824
|
+
async def unwatch(self) -> TOK:
|
|
825
|
+
"""
|
|
826
|
+
Flushes all the previously watched keys for an atomic batch (Transaction). Executing a transaction will
|
|
827
|
+
automatically flush all previously watched keys.
|
|
828
|
+
|
|
829
|
+
See [valkey.io](https://valkey.io/commands/unwatch) for more details.
|
|
830
|
+
|
|
831
|
+
Returns:
|
|
832
|
+
TOK: A simple "OK" response.
|
|
833
|
+
|
|
834
|
+
Examples:
|
|
835
|
+
>>> await client.unwatch()
|
|
836
|
+
'OK'
|
|
837
|
+
"""
|
|
838
|
+
return cast(
|
|
839
|
+
TOK,
|
|
840
|
+
await self._execute_command(RequestType.UnWatch, []),
|
|
841
|
+
)
|
|
842
|
+
|
|
843
|
+
async def scan(
|
|
844
|
+
self,
|
|
845
|
+
cursor: TEncodable,
|
|
846
|
+
match: Optional[TEncodable] = None,
|
|
847
|
+
count: Optional[int] = None,
|
|
848
|
+
type: Optional[ObjectType] = None,
|
|
849
|
+
) -> List[Union[bytes, List[bytes]]]:
|
|
850
|
+
"""
|
|
851
|
+
Incrementally iterate over a collection of keys.
|
|
852
|
+
SCAN is a cursor based iterator. This means that at every call of the command,
|
|
853
|
+
the server returns an updated cursor that the user needs to use as the cursor argument in the next call.
|
|
854
|
+
An iteration starts when the cursor is set to "0", and terminates when the cursor returned by the server is "0".
|
|
855
|
+
|
|
856
|
+
A full iteration always retrieves all the elements that were present
|
|
857
|
+
in the collection from the start to the end of a full iteration.
|
|
858
|
+
Elements that were not constantly present in the collection during a full iteration, may be returned or not.
|
|
859
|
+
|
|
860
|
+
See [valkey.io](https://valkey.io/commands/scan) for more details.
|
|
861
|
+
|
|
862
|
+
Args:
|
|
863
|
+
cursor (TResult): The cursor used for iteration. For the first iteration, the cursor should be set to "0".
|
|
864
|
+
|
|
865
|
+
- Using a non-zero cursor in the first iteration, or an invalid cursor at any iteration, will lead to
|
|
866
|
+
undefined results.
|
|
867
|
+
- Using the same cursor in multiple iterations will, in case nothing changed between the iterations,
|
|
868
|
+
return the same elements multiple times.
|
|
869
|
+
- If the the db has changed, it may result an undefined behavior.
|
|
870
|
+
|
|
871
|
+
match (Optional[TResult]): A pattern to match keys against.
|
|
872
|
+
count (Optional[int]): The number of keys to return per iteration.
|
|
873
|
+
|
|
874
|
+
- The number of keys returned per iteration is not guaranteed to be the same as the count argument.
|
|
875
|
+
- The argument is used as a hint for the server to know how many "steps" it can use to retrieve the keys.
|
|
876
|
+
- The default value is 10.
|
|
877
|
+
|
|
878
|
+
type (ObjectType): The type of object to scan for.
|
|
879
|
+
|
|
880
|
+
Returns:
|
|
881
|
+
List[Union[bytes, List[bytes]]]: A List containing the next cursor value and a list of keys,
|
|
882
|
+
formatted as [cursor, [key1, key2, ...]]
|
|
883
|
+
|
|
884
|
+
Examples:
|
|
885
|
+
>>> result = await client.scan(b'0')
|
|
886
|
+
print(result) #[b'17', [b'key1', b'key2', b'key3', b'key4', b'key5', b'set1', b'set2', b'set3']]
|
|
887
|
+
first_cursor_result = result[0]
|
|
888
|
+
result = await client.scan(first_cursor_result)
|
|
889
|
+
print(result) #[b'349', [b'key4', b'key5', b'set1', b'hash1', b'zset1', b'list1', b'list2',
|
|
890
|
+
b'list3', b'zset2', b'zset3', b'zset4', b'zset5', b'zset6']]
|
|
891
|
+
result = await client.scan(result[0])
|
|
892
|
+
print(result) #[b'0', [b'key6', b'key7']]
|
|
893
|
+
>>> result = await client.scan(first_cursor_result, match=b'key*', count=2)
|
|
894
|
+
print(result) #[b'6', [b'key4', b'key5']]
|
|
895
|
+
>>> result = await client.scan("0", type=ObjectType.Set)
|
|
896
|
+
print(result) #[b'362', [b'set1', b'set2', b'set3']]
|
|
897
|
+
"""
|
|
898
|
+
args = [cursor]
|
|
899
|
+
if match:
|
|
900
|
+
args.extend(["MATCH", match])
|
|
901
|
+
if count:
|
|
902
|
+
args.extend(["COUNT", str(count)])
|
|
903
|
+
if type:
|
|
904
|
+
args.extend(["TYPE", type.value])
|
|
905
|
+
return cast(
|
|
906
|
+
List[Union[bytes, List[bytes]]],
|
|
907
|
+
await self._execute_command(RequestType.Scan, args),
|
|
908
|
+
)
|
|
909
|
+
|
|
910
|
+
async def script_exists(self, sha1s: List[TEncodable]) -> List[bool]:
|
|
911
|
+
"""
|
|
912
|
+
Check existence of scripts in the script cache by their SHA1 digest.
|
|
913
|
+
|
|
914
|
+
See [valkey.io](https://valkey.io/commands/script-exists) for more details.
|
|
915
|
+
|
|
916
|
+
Args:
|
|
917
|
+
sha1s (List[TEncodable]): List of SHA1 digests of the scripts to check.
|
|
918
|
+
|
|
919
|
+
Returns:
|
|
920
|
+
List[bool]: A list of boolean values indicating the existence of each script.
|
|
921
|
+
|
|
922
|
+
Examples:
|
|
923
|
+
>>> await client.script_exists(["sha1_digest1", "sha1_digest2"])
|
|
924
|
+
[True, False]
|
|
925
|
+
"""
|
|
926
|
+
return cast(
|
|
927
|
+
List[bool], await self._execute_command(RequestType.ScriptExists, sha1s)
|
|
928
|
+
)
|
|
929
|
+
|
|
930
|
+
async def script_flush(self, mode: Optional[FlushMode] = None) -> TOK:
|
|
931
|
+
"""
|
|
932
|
+
Flush the Lua scripts cache.
|
|
933
|
+
|
|
934
|
+
See [valkey.io](https://valkey.io/commands/script-flush) for more details.
|
|
935
|
+
|
|
936
|
+
Args:
|
|
937
|
+
mode (Optional[FlushMode]): The flushing mode, could be either `SYNC` or `ASYNC`.
|
|
938
|
+
|
|
939
|
+
Returns:
|
|
940
|
+
TOK: A simple `OK` response.
|
|
941
|
+
|
|
942
|
+
Examples:
|
|
943
|
+
>>> await client.script_flush()
|
|
944
|
+
"OK"
|
|
945
|
+
|
|
946
|
+
>>> await client.script_flush(FlushMode.ASYNC)
|
|
947
|
+
"OK"
|
|
948
|
+
"""
|
|
949
|
+
|
|
950
|
+
return cast(
|
|
951
|
+
TOK,
|
|
952
|
+
await self._execute_command(
|
|
953
|
+
RequestType.ScriptFlush, [mode.value] if mode else []
|
|
954
|
+
),
|
|
955
|
+
)
|
|
956
|
+
|
|
957
|
+
async def script_kill(self) -> TOK:
|
|
958
|
+
"""
|
|
959
|
+
Kill the currently executing Lua script, assuming no write operation was yet performed by the script.
|
|
960
|
+
|
|
961
|
+
See [valkey.io](https://valkey.io/commands/script-kill) for more details.
|
|
962
|
+
|
|
963
|
+
Returns:
|
|
964
|
+
TOK: A simple `OK` response.
|
|
965
|
+
|
|
966
|
+
Examples:
|
|
967
|
+
>>> await client.script_kill()
|
|
968
|
+
"OK"
|
|
969
|
+
"""
|
|
970
|
+
return cast(TOK, await self._execute_command(RequestType.ScriptKill, []))
|
|
971
|
+
|
|
972
|
+
async def invoke_script(
|
|
973
|
+
self,
|
|
974
|
+
script: Script,
|
|
975
|
+
keys: Optional[List[TEncodable]] = None,
|
|
976
|
+
args: Optional[List[TEncodable]] = None,
|
|
977
|
+
) -> TResult:
|
|
978
|
+
"""
|
|
979
|
+
Invokes a Lua script with its keys and arguments.
|
|
980
|
+
This method simplifies the process of invoking scripts on a the server by using an object that represents a Lua script.
|
|
981
|
+
The script loading, argument preparation, and execution will all be handled internally.
|
|
982
|
+
If the script has not already been loaded, it will be loaded automatically using the `SCRIPT LOAD` command.
|
|
983
|
+
After that, it will be invoked using the `EVALSHA` command.
|
|
984
|
+
|
|
985
|
+
See [SCRIPT LOAD](https://valkey.io/commands/script-load/) and [EVALSHA](https://valkey.io/commands/evalsha/)
|
|
986
|
+
for more details.
|
|
987
|
+
|
|
988
|
+
Args:
|
|
989
|
+
script (Script): The Lua script to execute.
|
|
990
|
+
keys (Optional[List[TEncodable]]): The keys that are used in the script.
|
|
991
|
+
args (Optional[List[TEncodable]]): The arguments for the script.
|
|
992
|
+
|
|
993
|
+
Returns:
|
|
994
|
+
TResult: a value that depends on the script that was executed.
|
|
995
|
+
|
|
996
|
+
Examples:
|
|
997
|
+
>>> lua_script = Script("return { KEYS[1], ARGV[1] }")
|
|
998
|
+
>>> await client.invoke_script(lua_script, keys=["foo"], args=["bar"])
|
|
999
|
+
[b"foo", b"bar"]
|
|
1000
|
+
"""
|
|
1001
|
+
return await self._execute_script(script.get_hash(), keys, args)
|