2023-05-15 08:51:32 +08:00
|
|
|
import redis
|
2024-01-12 12:34:01 +08:00
|
|
|
from redis.connection import Connection, SSLConnection
|
2024-09-08 13:23:51 +08:00
|
|
|
from redis.sentinel import Sentinel
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-09-08 13:23:51 +08:00
|
|
|
|
|
|
|
class RedisClientWrapper(redis.Redis):
|
|
|
|
"""
|
|
|
|
A wrapper class for the Redis client that addresses the issue where the global
|
|
|
|
`redis_client` variable cannot be updated when a new Redis instance is returned
|
|
|
|
by Sentinel.
|
|
|
|
|
|
|
|
This class allows for deferred initialization of the Redis client, enabling the
|
|
|
|
client to be re-initialized with a new instance when necessary. This is particularly
|
|
|
|
useful in scenarios where the Redis instance may change dynamically, such as during
|
|
|
|
a failover in a Sentinel-managed Redis setup.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
_client (redis.Redis): The actual Redis client instance. It remains None until
|
|
|
|
initialized with the `initialize` method.
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
initialize(client): Initializes the Redis client if it hasn't been initialized already.
|
|
|
|
__getattr__(item): Delegates attribute access to the Redis client, raising an error
|
|
|
|
if the client is not initialized.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._client = None
|
|
|
|
|
|
|
|
def initialize(self, client):
|
|
|
|
if self._client is None:
|
|
|
|
self._client = client
|
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
if self._client is None:
|
|
|
|
raise RuntimeError("Redis client is not initialized. Call init_app first.")
|
|
|
|
return getattr(self._client, item)
|
|
|
|
|
|
|
|
|
|
|
|
redis_client = RedisClientWrapper()
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
def init_app(app):
|
2024-09-08 13:23:51 +08:00
|
|
|
global redis_client
|
2023-05-17 15:40:21 +08:00
|
|
|
connection_class = Connection
|
2024-08-15 12:54:05 +08:00
|
|
|
if app.config.get("REDIS_USE_SSL"):
|
2023-05-17 15:40:21 +08:00
|
|
|
connection_class = SSLConnection
|
|
|
|
|
2024-09-08 13:23:51 +08:00
|
|
|
redis_params = {
|
|
|
|
"username": app.config.get("REDIS_USERNAME"),
|
|
|
|
"password": app.config.get("REDIS_PASSWORD"),
|
|
|
|
"db": app.config.get("REDIS_DB"),
|
|
|
|
"encoding": "utf-8",
|
|
|
|
"encoding_errors": "strict",
|
|
|
|
"decode_responses": False,
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.config.get("REDIS_USE_SENTINEL"):
|
|
|
|
sentinel_hosts = [
|
|
|
|
(node.split(":")[0], int(node.split(":")[1])) for node in app.config.get("REDIS_SENTINELS").split(",")
|
|
|
|
]
|
|
|
|
sentinel = Sentinel(
|
|
|
|
sentinel_hosts,
|
|
|
|
sentinel_kwargs={
|
|
|
|
"socket_timeout": app.config.get("REDIS_SENTINEL_SOCKET_TIMEOUT", 0.1),
|
|
|
|
"username": app.config.get("REDIS_SENTINEL_USERNAME"),
|
|
|
|
"password": app.config.get("REDIS_SENTINEL_PASSWORD"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
master = sentinel.master_for(app.config.get("REDIS_SENTINEL_SERVICE_NAME"), **redis_params)
|
|
|
|
redis_client.initialize(master)
|
|
|
|
else:
|
|
|
|
redis_params.update(
|
|
|
|
{
|
|
|
|
"host": app.config.get("REDIS_HOST"),
|
|
|
|
"port": app.config.get("REDIS_PORT"),
|
|
|
|
"connection_class": connection_class,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
pool = redis.ConnectionPool(**redis_params)
|
|
|
|
redis_client.initialize(redis.Redis(connection_pool=pool))
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
app.extensions["redis"] = redis_client
|