2023-05-15 08:51:32 +08:00
|
|
|
import redis
|
2023-05-17 15:40:21 +08:00
|
|
|
from redis.connection import SSLConnection, Connection
|
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
|
|
|
|
if app.config.get('REDIS_USE_SSL', False):
|
|
|
|
connection_class = SSLConnection
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
redis_client.connection_pool = redis.ConnectionPool(**{
|
|
|
|
'host': app.config.get('REDIS_HOST', 'localhost'),
|
|
|
|
'port': app.config.get('REDIS_PORT', 6379),
|
2023-05-17 15:40:21 +08:00
|
|
|
'username': app.config.get('REDIS_USERNAME', None),
|
2023-05-15 08:51:32 +08:00
|
|
|
'password': app.config.get('REDIS_PASSWORD', None),
|
|
|
|
'db': app.config.get('REDIS_DB', 0),
|
|
|
|
'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
|