REDIS_DECODE_RESPONSES = True 不起作用
时间: 2023-08-04 22:08:19 浏览: 70
如果在设置 `REDIS_DECODE_RESPONSES = True` 后,Redis 中的数据还是没有自动解码成字符串,可以尝试以下几个方法:
1. 在创建 Redis 实例时,显式设置 `decode_responses=True`,例如:
```python
from flask_redis import FlaskRedis
redis_store = FlaskRedis(decode_responses=True)
```
2. 确保 Redis 中的数据本身就是字符串类型,如果 Redis 中的数据是二进制类型,即使设置了 `decode_responses=True` 也无法自动解码成字符串。
3. 重新启动 Flask 应用,确保新的配置项生效。
4. 检查 Redis 服务器的版本是否支持 `decode_responses` 参数,可以通过运行 `redis-cli info server` 命令查看 Redis 服务器的信息,并查看 `redis_version` 字段的值。
如果以上方法都没有解决问题,可以尝试打印 Redis 实例的配置项以及从 Redis 中读取的数据类型,以确定问题出在哪里。
相关问题
flask_redis如何设置decode_responses=True
在使用 Flask-Redis 时,可以通过在创建 Redis 实例时设置 `decode_responses=True` 来实现自动解码 Redis 中的数据。具体的做法是,在 Flask 的配置中增加以下项:
```python
REDIS_SETTINGS = {
'host': '127.0.0.1',
'port': 6379,
'db': 0,
'decode_responses': True,
}
app = Flask(__name__)
app.config['REDIS_SETTINGS'] = REDIS_SETTINGS
redis_store = FlaskRedis(app)
```
其中,`decode_responses=True` 的作用是将 Redis 中的数据解码成字符串,这样可以直接使用字符串操作 Redis 中的数据,而不需要手动进行解码。
try: self.redis_pool = redis.ConnectionPool(host=self.redis_host, port=self.redis_port, password='tmcsoft@Redis_123', decode_responses=True, db=self.redis_conf_db, socket_connect_timeout=3) self.redis_client = redis.Redis(connection_pool=self.redis_pool) except Exception as e: print("configuration DB connection failed, please retry")
这段代码是一个类的方法,作用是通过Redis客户端连接池连接到Redis数据库。具体解释如下:
1. `redis.ConnectionPool()`:创建Redis客户端连接池对象,指定连接参数,例如Redis服务器地址、端口、认证密码、数据库编号等。这里使用了一个密码为'tmcsoft@Redis_123'的认证方式,连接超时时间为3秒。
2. `redis.Redis()`:创建Redis客户端对象,指定连接池对象。这里使用了`connection_pool`参数,将第1步创建的连接池对象传入,以便于后续Redis操作使用该连接池。
3. `except Exception as e:`:捕获Redis连接异常,并将异常对象保存到变量`e`中。
4. `print("configuration DB connection failed, please retry")`:打印错误提示信息,提示Redis连接失败。
在实际应用中,我们可以通过调用该方法来获取Redis客户端连接池对象,并使用该连接池来执行Redis操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)