- March 14, 2007
- Pre .96
- 10 (after 12 ratings)
A decorator similar to
cache_page, which will cache any function for any amount of time using the Django cache API.
I use this to cache API calls to remote services like Flickr in my view, to prevent having to hit their servers on every request.
I posted a sample function which uses the delicious API in the function, also.
Update: It now also will put in a temporary 'in-process' variable (an instance of
MethodNotFinishedError) in the cache while the function is processing. This will prevent the cache from calling the method again if it's still processing. This does not affect anything unless you're using threads.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
def cache_function(length): """ Caches a function, using the function itself as the key, and the return value as the value saved. It passes all arguments on to the function, as it should. The decorator itself takes a length argument, which is the number of seconds the cache will keep the result around. It will put in a MethodNotFinishedError in the cache while the function is processing. This should not matter in most cases, but if the app is using threads, you won't be able to get the previous value, and will need to wait until the function finishes. If this is not desired behavior, you can remove the first two lines after the ``else``. """ def decorator(func): def inner_func(*args, **kwargs): from django.core.cache import cache value = cache.get(func) if cache.has_key(func): return value else: # This will set a temporary value while ``func`` is being # processed. When using threads, this is vital, as otherwise # the function can be called several times before it finishes # and is put into the cache. class MethodNotFinishedError(Exception): pass cache.set(func, MethodNotFinishedError( 'The function %s has not finished processing yet. This \ value will be replaced when it finishes.' % (func.__name__) ), length) result = func(*args, **kwargs) cache.set(func, result, length) return result return inner_func return decorator
More like this
- Stuff by NixonDash 1 month ago
- Add custom fields to the built-in Group model by jmoppel 3 months ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 6 months, 2 weeks ago
- Python Django CRUD Example Tutorial by tuts_station 7 months ago
- Browser-native date input field by kytta 8 months, 2 weeks ago
A couple of usage examples would be also very helpful.
Is there a similar version of this out there that will serialize arguments as part of the caching key?
Hmm, that's certainly a reasonable idea, and in fact I had considered it when writing the function. I decided not to add that in for simplicity, and because I did not need it.
I might get around to adding it, or you can add it also. It shouldn't be too difficult.
Please login first before commenting.