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-06-19 13:41:12 +08:00
|
|
|
if app.config.get('REDIS_USE_SSL'):
|
2023-05-17 15:40:21 +08:00
|
|
|
connection_class = SSLConnection
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
redis_client.connection_pool = redis.ConnectionPool(**{
|
2024-06-19 13:41:12 +08:00
|
|
|
'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'),
|
2023-05-15 08:51:32 +08:00
|
|
|
'encoding': 'utf-8',
|
|
|
|
'encoding_errors': 'strict',
|
|
|
|
'decode_responses': False
|
2023-05-17 15:40:21 +08:00
|
|
|
}, connection_class=connection_class)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
app.extensions['redis'] = redis_client
|