Django Memcached酸洗错误

时间:2012-04-28 10:25:09

标签: python django memcached

我是Django的新手,我正在尝试使用内置缓存系统。我正在使用Django 1.4。

我的目标

在我看来,我想缓存对外部API的调用。因此,我想使用cache.get()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

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

1 个答案:

答案 0 :(得分:0)

感谢comment of DrTyrsa我找到了解决方案:

酸洗对象可能会导致错误,因此将所需数据提取到列表或词典会更可靠。