This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Caching decorator and django cache backend with advanced invalidation ability and dog-pile effect prevention

Project Description

django-cache-utils provides some utils for make cache-related work easier:

  • cached decorator. It can be applied to function, method or classmethod and can be used with any django cache backend (built-in or third-party like django-newcache).

    Supports fine-grained invalidation for exact parameter set (with any backend) and bulk cache invalidation (only with group_backend). Cache keys are human-readable because they are constructed from callable’s full name and arguments and then sanitized to make memcached happy.

    Wrapped callable gets invalidate methods. Call invalidate with same arguments as function and the cached result for these arguments will be invalidated.

  • group_backend. It is a django memcached cache backend with group O(1) invalidation ability, dog-pile effect prevention using MintCache algorythm and project version support to allow gracefull updates and multiple django projects on same memcached instance. Long keys (>250) are auto-truncated and appended with md5 hash.


pip install django-cache-utils

and then (optional):

CACHE_BACKEND = 'cache_utils.group_backend://localhost:11211/'


cached decorator can be used with any django caching backend (built-in or third-party like django-newcache):

from cache_utils.decorators import cached

def foo(x, y=0):
    print 'foo is called'
    return x+y

foo(1,2) # foo is called
foo(5,6) # foo is called
foo(1,2) # foo is called
foo(x=2) # foo is called

class Foo(object):
    def foo(self, x,y):
        print "foo is called"
        return x+y

obj = Foo(),2) # foo is called,2)

With group_backend cached decorator supports bulk O(1) invalidation:

from django.db import models
from cache_utils.decorators import cached

class CityManager(models.Manager):

    # cache a method result. 'self' parameter is ignored
    @cached(60*60*24, 'cities')
    def default(self):

    # cache a method result. 'self' parameter is ignored, args and
    # kwargs are used to construct cache key
    @cached(60*60*24, 'cities')
    def get(self, *args, **kwargs):
        return super(CityManager, self).get(*args, **kwargs)

class City(models.Model):
    # ... field declarations

    objects = CityManager()

    # an example how to cache django model methods by instance id
    def has_offers(self):
        def offer_count(pk):
            return self.offer_set.count()
        return history_count( > 0

# cache the function result based on passed parameter
@cached(60*60*24, 'cities')
def get_cities(slug)
    return City.objects.get(slug=slug)

# cache for 'cities' group can be invalidated at once
def invalidate_city(sender, **kwargs):
pre_delete.connect(invalidate_city, City)
post_save.connect(invalidate_city, City)


If decorated function returns None cache will be bypassed.

django-cache-utils use 2 reads from memcached to get a value if ‘group’ argument is passed to ‘cached’ decorator:

def foo(param)
    return ..

@cached(60, 'my_group')
def bar(param)
    return ..

# 1 read from memcached
value1 = foo(1)

# 2 reads from memcached + ability to invalidate all values at once
value2 = bar(1)

Running tests

cd test_project
Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-cache-utils-0.7.2.tar.gz (7.2 kB) Copy SHA256 Checksum SHA256 Source Jan 7, 2011

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting