我第一次尝试在 App Engine 上使用 memcache 并遇到“PicklingError”。
我尝试使用 memcache 的第一个地方是在网站的主页上,我从数据存储中获取内容:
def get(self):
content = memcache.get('home:content')
if content is None:
all_content = Content.all()
all_content.order("-views")
all_content.filter('published =', True)
content = all_content.run(batch_size=5, limit=5)
memcache.add(key='home:content', value=content, time=120)
(请注意,无需我尝试将内容 Query 对象放入 memcache 即可正常工作。这是它在最后一行遇到的错误(memcache.add ...):
PicklingError: Pickling of datastore_query.Batcher is unsupported.
这是内容的模型:
class Content(db.Model):
category = db.StringProperty(required = True)
content_type = db.StringProperty(required = True)
published = db.BooleanProperty(default = False)
title = db.StringProperty(required = True)
abstract = db.TextProperty(required = True)
summary = db.TextProperty(required = True)
URL = db.LinkProperty(required = True)
youtube_id = db.StringProperty(required = False)
thumbnail = db.LinkProperty(required = True)
post_author = db.StringProperty(required = True)
author_url = db.LinkProperty(required = False)
date_post = db.DateTimeProperty(required = True, auto_now_add = True)
date_source = db.DateTimeProperty(required = False)
# todo: split out to use decent shardedcounter approach
views = db.IntegerProperty(default = 0)
up_votes = db.IntegerProperty(default = 0)
down_votes = db.IntegerProperty(default = 0)
def votes(self):
return self.up_votes - self.down_votes
我正在努力弄清楚 PicklingError 是什么以及它与尝试将 Query 对象存储到内存缓存中的关系。我的问题:我做错了什么?这是因为我试图缓存迭代器吗?缓存 Query 对象并需要在每次页面加载时对其调用 .run() 是否有任何价值?