redis编码错误怎么办?
import redis
key = ":1:rn_data_select" + "线路"
data = cache.get(key)
if data == None:
cursor.execute("select lat,lng,instationnum,outstationnum \
from %s ;" % (tablename))
data = cursor.fetchall()
cache.set(key,result)
我使用以上脚本(略去不重要代码),将数据库查询结果存入redis,但是在django中读取redis中key对应结果时却出现了以下错误:(django中使用的是django-redis)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/yangtz/anaconda3/envs/keshihua/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/yangtz/anaconda3/envs/keshihua/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/yangtz/anaconda3/envs/keshihua/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/yangtz/**平台/visualplat/roadnet/views_new.py", line 240, in roadnet_maindata2
datas = cache.get(key)
File "/home/yangtz/anaconda3/envs/keshihua/lib/python3.6/site-packages/django_redis/cache.py", line 33, in _decorator
return method(self, *args, **kwargs)
File "/home/yangtz/anaconda3/envs/keshihua/lib/python3.6/site-packages/django_redis/cache.py", line 82, in get
client=client)
File "/home/yangtz/anaconda3/envs/keshihua/lib/python3.6/site-packages/django_redis/client/default.py", line 208, in get
return self.decode(value)
File "/home/yangtz/anaconda3/envs/keshihua/lib/python3.6/site-packages/django_redis/client/default.py", line 320, in decode
value = self._serializer.loads(value)
File "/home/yangtz/anaconda3/envs/keshihua/lib/python3.6/site-packages/django_redis/serializers/pickle.py", line 36, in loads
return pickle.loads(force_bytes(value))
_pickle.UnpicklingError: invalid load key, '6'.
我在redis命令行看了看,脚本写入的value显示是这样的,
而使用django-redis写入的同样的结果显示这样的
这怎么办?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看你的代码,应该是写入redis前,使用pickle模块进行序列化,把数据转成二进制了,根据github上作者的回答,除了整数,其他数据类型都序列化了,如果你不想序列化,就这样
redis_cache.get_redis_connection(alias)
具体作者回复:https://github.com/niwinz/dja...
因为你是写入进去的是二进制对象,你可以在程序端将对象用某种编码转换成字符串再写入