python业务代码会向redis存储带有布尔类型的字典,原始版本使用redis-py 2.x,新服务器升级至redis-py 3.2 后,出现报错:
DataError: Invalid input of type: 'bool'. Convert to a byte, string or number first.
查询redis-py官方文档,发现2.x升级至3.0后有较大更新,其中一条:
Encoding of User Input
redis-py 3.0 only accepts user data as bytes, strings or numbers (ints, longs and floats). Attempting to specify a key or a value as any other type will raise a DataError exception.
redis-py 2.X attempted to coerce any type of input into a string. While occasionally convenient, this caused all sorts of hidden errors when users passed boolean values (which were coerced to ‘True’ or ‘False’), a None value (which was coerced to ‘None’) or other values, such as user defined types.
All 2.X users should make sure that the keys and values they pass into redis-py are either bytes, strings or numbers.
用户应确保存入的key,value必须是bytes,string或者number三种类型,如果有布尔类型、None类型或者其他用户自定义类型,必须显式转换为上述三种合法类型再存入redis,否则会报错。
解决办法:
- 服务器环境限制redis-py为2.x版本
- 改进业务代码,将字典数据显式转换为字符串,避免以后出现潜在问题(推荐)
网友评论