我是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
显然,酸洗/序列化出错了。这里发生了什么?