A quick write-up on python decorators and memcached
November 17, 2009
…or how to do proper caching without heavily modifing your code.
I’ve been using this technique on django views and Juno for some time. It’s just a decorator which inspect the parameters given to a function to search for a key on memcached.
You will find two files on github: no_decorator_example.py and decorator_example_memcache.py. Both of them search on twitter for a given term. The first script executes the search everytime, and the second script implements a 60 second cache, which uses the parameter (search term) as key.
I’m sure there must be another way to deal with parameters without the wrapper class, but so far nothing I’ve tried gave the same result.
ForĀ Django views, one could go so far as using return HttpResponse(your_response, mimetype=’text_plain’) too.
Enjoy.