|
| 1 | +# Copyright 2025 Bytedance Ltd. and/or its affiliates |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import redis.asyncio as redis |
| 16 | +from redis.asyncio.retry import Retry |
| 17 | +from redis.backoff import ExponentialBackoff |
| 18 | +from redis.exceptions import BusyLoadingError, ConnectionError, TimeoutError |
| 19 | + |
| 20 | +from arkitect.core.client.base import Client |
| 21 | + |
| 22 | + |
| 23 | +class RedisClient(Client): |
| 24 | + """ |
| 25 | + Initialize a new Redis client object. |
| 26 | +
|
| 27 | + Parameters: |
| 28 | + host (str): The hostname of the Redis server. |
| 29 | + username (str): The username for the Redis server. |
| 30 | + password (str): The password for the Redis server. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + None. |
| 34 | +
|
| 35 | + """ |
| 36 | + |
| 37 | + def __init__(self, host: str, username: str, password: str): |
| 38 | + self.client = redis.Redis( |
| 39 | + host=host, |
| 40 | + username=username, |
| 41 | + password=password, |
| 42 | + retry=Retry(ExponentialBackoff(), 3), |
| 43 | + retry_on_error=[BusyLoadingError, ConnectionError, TimeoutError], |
| 44 | + ) |
| 45 | + |
| 46 | + async def get(self, key: str) -> str: |
| 47 | + """ |
| 48 | + Get the value of a key from the Redis database. |
| 49 | +
|
| 50 | + Args: |
| 51 | + key (str): The key to retrieve from the Redis database. |
| 52 | +
|
| 53 | + Returns: |
| 54 | + str: The value of the key, or None if the key does not exist. |
| 55 | +
|
| 56 | + """ |
| 57 | + return await self.client.get(key) |
| 58 | + |
| 59 | + async def set(self, key: str, value: str) -> None: |
| 60 | + """ |
| 61 | + Set the value of a key in the Redis database. |
| 62 | + Args: |
| 63 | + key (str): The key to set in the Redis database. |
| 64 | + value (str): The value to set for the key. |
| 65 | + Returns: |
| 66 | + None. |
| 67 | + """ |
| 68 | + await self.client.set(key, value) |
| 69 | + |
| 70 | + async def get_with_prefix(self, prefix: str) -> tuple[list[str], list[str]]: |
| 71 | + """ |
| 72 | + Asynchronous method to obtain all keys and values from the |
| 73 | + Redis database that match the specified prefix |
| 74 | +
|
| 75 | + :param prefix: The specified prefix |
| 76 | +
|
| 77 | + :return: A list of tuples containing matching keys |
| 78 | + and their corresponding values |
| 79 | + """ |
| 80 | + |
| 81 | + cursor = 0 |
| 82 | + keys = [] |
| 83 | + |
| 84 | + while True: |
| 85 | + # 使用 SCAN 命令进行迭代查询 |
| 86 | + cursor, key_data = await self.client.scan(cursor, match=prefix, count=1000) |
| 87 | + |
| 88 | + # 将匹配到的 key 添加到列表中 |
| 89 | + keys.extend(key_data) |
| 90 | + |
| 91 | + # 如果游标值为 0,则表示遍历完成 |
| 92 | + if cursor == 0 or len(key_data) == 0: |
| 93 | + break |
| 94 | + |
| 95 | + # 使用 MGET 命令获取所有匹配到的 key 的对应 value |
| 96 | + values = await self.client.mget(keys) |
| 97 | + |
| 98 | + return keys, values |
| 99 | + |
| 100 | + async def mget(self, keys: list[str]) -> list[str]: |
| 101 | + """ |
| 102 | + Get the values of multiple keys from the Redis database. |
| 103 | +
|
| 104 | + Args: |
| 105 | + keys (list): A list of keys to retrieve from the Redis database. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + list: A list of values corresponding to the given keys. |
| 109 | +
|
| 110 | + """ |
| 111 | + return await self.client.mget(keys) |
| 112 | + |
| 113 | + async def delete(self, key: str) -> None: |
| 114 | + """ |
| 115 | + Delete a key from the Redis database. |
| 116 | + Args: |
| 117 | + key (str): The key to delete from the Redis database. |
| 118 | + Returns: |
| 119 | + None. |
| 120 | + """ |
| 121 | + await self.client.delete(key) |
0 commit comments