Tuesday, February 14, 2012

ANN: Django Better Cache 0.5.3

I've added a small, but useful, addition to Django Better Cache, today. The new bettercache.decorators.CachedFormMethod utility is a decorator for Django form methods, which is essentially a memoizer, but with a nice twist.

This is great for forms that include DB or search index reading methods which can be expensive and you'd like to cache, but normal memoization fails when the important parameters to key on are in the form data, not the arguments to the actual method.

Read about the new decorator at the docs to learn more.

class FriendsLookup(forms.Form):

    username = forms.CharField(required=True)

    @CachedFormMethod(expires=60*15) # expire in 15 minutes
    def get_friends_list(self, include_pending=False):
        username = self.cleaned_data['username']
        friends = Friendship.objects.filter(
        if include_pending:
            friends = friends.filter(status__in=(PENDING, APPROVED))
            friends = friends.filter(status=APPROVED)

        return friends 

No comments:

I write here about programming, how to program better, things I think are neat and are related to programming. I might write other things at my personal website.

I am happily employed by the excellent Caktus Group, located in beautiful and friendly Carrboro, NC, where I work with Python, Django, and Javascript.

Blog Archive