App Engine - 使用memcache实现获取KeyError

时间:2012-07-15 16:28:17

标签: python google-app-engine memcached

我正在尝试实现Guido发布的代码作为问题Avoiding Memcache 1M limit of values的答案。当我第一次加载页面并向memcache添加值时,似乎(?)一切正常工作,但是当我重新加载它并从memcache中检索值时,我得到一个奇怪的错误:

Traceback (most recent call last):
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 194, in Handle
    for chunk in result:
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/appstats/recording.py", line 1036, in appstats_wsgi_wrapper
    result = app(environ, appstats_start_response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/appengineurlhere/mypythoncode.py", line 87, in get
    entitylist = retrieve("entitylist")
  File "/appengineurlhere/mypythoncode.py", line 53, in retrieve
    return pickle.loads(serialized)
  File "/base/python27_runtime/python27_dist/lib/python2.7/pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "/base/python27_runtime/python27_dist/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
KeyError: ':'

这是存储/检索代码:

def store(key, value, chunksize=750000):
    serialized = pickle.dumps(value, 2)
    values = {}
    for i in xrange(0, len(serialized), chunksize):
        values['%s.%s' % (key, i//chunksize)] = serialized[i : i+chunksize]
    memcache.set_multi(values)

def retrieve(key):
    result = memcache.get_multi(['%s.%s' % (key, i) for i in xrange(32)])
    serialized = ''.join([v for v in result.values() if v is not None])
    return pickle.loads(serialized)

这就是我使用它的方式:

try:
  entitylist = retrieve("entitylist")
except EOFError:
  entitylist = MyModel.all().fetch(None)
  store("entitylist", entitylist)

其他奇怪之处:<​​/ p>

  • 我在开发服务器上看不到这个;只是生产App Engine网站。 (虽然我的开发服务器上的数据略有不同;我相信它都符合标准的1MB内存缓存大小,生产数据更大。)
  • 当我在开发和生产管理面板上搜索“entitylist”时,App Engine告诉我“没有这样的密钥”。然而,基于所显示的memcache的总大小(这是我实现memcache的唯一地方),绝对看起来有些东西被缓存了。

任何人都可以指出我应该注意或修复的方向吗?

1 个答案:

答案 0 :(得分:0)

您是否可能有一些MyModel的旧实例与MyModel的当前定义不匹配?如果存在无法识别或缺失的属性,则在酸洗时可能会出错。