我有一个简单的字典,想要将其保存到缓存中。看起来 Django 正在尝试对其进行 pickle 处理:
podcasts = []
for i in items:
s = re.sub('[\s+]', '', str(i))
s2 = re.findall(r'<link/>(.*?)<itunes',s)[0]
item_obj = {}
item_obj['title'] = title
item_obj['url'] = s2
item_obj['created_at'] = created_at
item_obj['duration'] = duration
podcasts.append(item_obj)
这有一个非常简单的格式,输出如下:
[{'title': "Podcast1", 'url': 'https://example.com\\n', 'created_at': 'Thu, 28 Dec 2017', 'duration': '00:30:34'}]
我正在使用自定义管理命令运行以下内容:
python3 manage.py podcast_job
我尝试将内容保存到缓存:
podcasts = get_podcasts()
print(podcasts)
cache.set('podcasts', podcasts)
I get the error:
File "podcast_job.py", line 13, in handle
cache.set('podcasts', podcasts)
File "python3.6/site-packages/django_redis/cache.py", line 33, in _decorator
return method(self, *args, **kwargs)
File "python3.6/site-packages/django_redis/cache.py", line 68, in set
return self.client.set(*args, **kwargs)
File "python3.6/site-packages/django_redis/client/default.py", line 109, in set
nvalue = self.encode(value)
File "python3.6/site-packages/django_redis/client/default.py", line 329, in encode
value = self._serializer.dumps(value)
File "python3.6/site-packages/django_redis/serializers/pickle.py", line 33, in dumps
return pickle.dumps(value, self._pickle_version)
RecursionError: maximum recursion depth exceeded while calling a Python object
如果我尝试使用字符串保存,没有出现错误并且成功保存:
cache.set('podcasts', str(podcasts))
我该如何保存字典列表而不出现上述错误?
item_obj
。 - John Gordon