Cache Any Function

 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

  1. Never cache a group of URLs by ChipX86 6 years, 8 months ago
  2. Cache any function (with its arguments) by bkroeze 6 years, 11 months ago
  3. Effective content caching for mass-load site using redirect feature by nnseva 2 years, 9 months ago
  4. per-function cache decorator by nicois 6 years, 2 months ago
  5. Run and cache only one instance of a heavy request by farnsworth 3 years, 8 months ago

Comments

jeffwheeler (on March 15, 2007):

Whoops, didn't mean to mark this as Javascript, and the edit page doesn't seem to allow a change. Can somebody change this?

#

Archatas (on March 15, 2007):

A couple of usage examples would be also very helpful.

#

Kaelten (on March 16, 2007):

Is there a similar version of this out there that will serialize arguments as part of the caching key?

#

jeffwheeler (on March 16, 2007):

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.

#

(Forgotten your password?)