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
Comments
Whoops, didn't mean to mark this as Javascript, and the edit page doesn't seem to allow a change. Can somebody change this?
#
A couple of usage examples would be also very helpful.
#
Just posted a second snippet which utilizes this one: http://www.djangosnippets.org/snippets/110/
#
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.
#
Variant with cached arguments at:
http://www.djangosnippets.org/snippets/202/
I would've just posted it as a comment, but the comments don't like code all that much, and it looked all messed up.
#