2

我对 Django 相当陌生,我正在尝试使用内置缓存系统。我正在使用 Django 1.4。

我的目标

在我看来,我想缓存对外部 API 的调用。因此我想使用cache.get()and cache.set()

我的方法

1)我安装了 pylibmc 以及 python-memcached

2)在我的设置中,我添加了:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        # i also tried 'django.core.cache.backends.memcached.MemcachedCache'
        'LOCATION': '127.0.0.1:11211',
    }
}

3)在我看来,我补充说:

myData = cache.get('myKey')
if not myData:
    myData = myApiCall()
    cache.set('myKey', myData)

4)myApiCall()是lastfm库的方法调用:

api_key = '12345678901234567890'
api = lastfm.Api(api_key)
user = api.get_user('aLastFmUser')
myData = user.top_artists # this is the relevant line

问题

当我使用 pylibmc 缓存后端时收到此错误消息:

cPickle.PicklingError
PicklingError: Can't pickle <type 'module'>: attribute lookup __builtin__.module failed

而这个使用 memcached 缓存后端时:

TypeError
TypeError: can't pickle module objects

显然,酸洗/序列化出错了。这里发生了什么?

4

1 回答 1

0

感谢DrTyrsa 的评论,我找到了解决方案:

腌制对象可能会导致错误,因此将所需数据提取到列表或字典中更可靠。

于 2012-06-13T16:21:51.573 回答