views:

200

answers:

1

I'm trying to manually set an object in the Django cache API but it fails (i think due to pickling?) The object is given to me by a third party, my code is:

def index(request, template_name="mytemplate.htm"):
    user_list = cache.get("user_list_ds")
    if user_list is None:
            # this is the expensive bit I'm trying to cache
            # given to me by a third part
     user_list = graffiti.user_list("top", 100).responseObj().blocks()
     cache.set("user_list_ds", user_list, 10*60) # 10 minutes

    return render_to_response(template_name, { 'user_list' : user_list,}, context_instance = RequestContext(request))

When I run this I get an error;

Can't pickle <type 'etree._Element'>: import of module etree failed
in -    cache.set("user_list_ds", user_list, 10*60) # 10 minutes

I'm very new to python, and I'm wondering how best to resolve this, do i need to pickle something first?

+1  A: 

It appears that you need to install ElementTree, because the pickle operation tries and fails to import the etree module.

UPDATE: Looking at it further, are you actually trying to cache document nodes? If you're trying to cache the data from the node, you probably need to do some processing of the value you're currently storing in user_list.

Hank Gay
You were right, I had to change the type of data we were trying to cache into a normal python list.
Tristan