Login

Cache Any Function

Author:
jeffwheeler
Posted:
March 14, 2007
Language:
Python
Version:
Pre .96
Score:
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

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks 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.

#

Please login first before commenting.