Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Memcached Pickling Error

I am fairly new to Django and i am trying to use the builtin cache system. I am using Django 1.4.

My goal

In my view i want to cache a call to an external API. Therefore i want to use cache.get() and cache.set().

My approach

1) I installed pylibmc as well as python-memcached

2) In my settings i added:

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) In my view i added:

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

4) myApiCall() is a method call of the lastfm library:

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

The problem

I get this error message when i use the pylibmc cache backend:

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

And this one when using the memcached cache backend:

TypeError
TypeError: can't pickle module objects

Apparently, the pickling/serialization goes wrong. What is happening here?

like image 885
Alp Avatar asked Apr 20 '26 21:04

Alp


1 Answers

Thanks to the comment of DrTyrsa i found the solution:

Pickling objects can lead to errors, therefore it's more reliable to extract the needed data to a list or dict.

like image 143
Alp Avatar answered Apr 22 '26 12:04

Alp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!