2023-05-15 08:51:32 +08:00
|
|
|
import redis
|
2024-01-12 12:34:01 +08:00
|
|
|
from redis.connection import Connection, SSLConnection
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
redis_client = redis.Redis()
|
|
|
|
|
|
|
|
|
|
|
|
def init_app(app):
|
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-08-15 12:54:05 +08:00
|
|
|
redis_client.connection_pool = redis.ConnectionPool(
|
|
|
|
**{
|
|
|
|
"host": app.config.get("REDIS_HOST"),
|
|
|
|
"port": app.config.get("REDIS_PORT"),
|
|
|
|
"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,
|
|
|
|
},
|
|
|
|
connection_class=connection_class,
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-08-15 12:54:05 +08:00
|
|
|
app.extensions["redis"] = redis_client
|