Tuesday, January 31, 2012

ANN: Django Better Cache 0.5.1 with expanded docs

I've made a small incremental release on Django Better Cache, adding a from_miss() method to handle cache misses in CacheModel objects. Now, you can define your cache model and how it initializes data when the cache is empty, and perform all your lookups through the CacheModel, expecting misses to auto-populate for you.

You can see a quick example of the new functionality below, but I have also made large improvements to the documentation hosted over at ReadTheDocs, so go read the full docs right now, if you are interested.

from bettercache.objects import CacheModel
from django.contrib import auth

class User(CacheModel):
    username = Key()
    email = Field()
    full_name = Field()

    def from_miss(self, username):
        user = auth.models.User.objects.get(username=username)
        self.email = user.email
        self.full_name = user.get_full_name()

If this get() does not find the object in the cache, it will create a new
User instance and call from_miss() with the key parameter username to
initialize it, and then save and return that object for you. The next call,
it will find it in the cache.

user = User.get(username="bob")

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