dave
dave

Reputation: 7857

How do I cache a list/dictionary in Pylons?

On a website I'm making, there's a section that hits the database pretty hard. Harder than I want. The data that's being retrieved is all very static. It will rarely change. So I want to cache it.

I came across http://wiki.pylonshq.com/display/pylonsdocs/Caching+in+Templates+and+Controllers and had a good read have been making use of template caching using:

return render('tmpl.html', cache_expire='never')

That works great until I modify the HTML. The only way I've found to delete the cache is to remove the cache_expire parameter from render() and delete the cache folder. But, meh, it works.

What I want to be able to, however, is cache Lists, Tuples and Dictionaries. From reading the above wiki page, it seems this isn't possible?

I want to be able to do something like:

data    = [i for i in range(0, 2000000)]
mycache = cache.get_cache('cachename')

value = mycache.get(key='dataset1', list=data, type='memory', expiretime='3600')

print value

Allowing me to do some CPU intensive work (list generation, in this example) and then cache it.

Can this be done with Pylons?

Upvotes: 1

Views: 497

Answers (2)

rob
rob

Reputation: 37644

Why not use memcached?
Look at this question on SO on how to use it with pylons: Pylons and Memcached

Upvotes: 1

estin
estin

Reputation: 3121

As alternative of traditional cache you can use app globals variables. Once on server startup load data to variable and then use data in you actions or direct in templates.

http://pylonsbook.com/en/1.1/exploring-pylons.html#app-globals-object

Also you can code some action to update this global variable through the admin interface or by other events.

Upvotes: 1

Related Questions