Skip to main content

Django caching decorator + invalidate function

Project description

django-cache-utils2 provides cached decorator and invalidate function.


pip install django-cache-utils2


from cache_utils2 import cached, invalidate

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

foo(1, 2) # foo is called
foo(1, y=2)
foo(5, 6) # foo is called
foo(5, 6)
invalidate(foo, {'x': 1, 'y': 2})
foo(1, 2) # foo is called
foo(5, 6)
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)

invalidate(, {'x': 1, 'y': 2}),2) # foo is called

Django example

from django.db import models
from cache_utils2 import cached, invalidate

class CityManager(models.Manager):

    # cache a method result. 'self' parameter is ignored
    def default(self):

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

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

    objects = CityManager()

    # cache django model instance method result by instance pk
    @cached(30, vary='')
    def has_offers(self):
        return self.offer_set.count() > 0

# invalidation of model methods
invalidate(City.has_offers, {'': 1}


If decorated function returns cache_utils2.NO_CACHE cache will be bypassed.

Running tests

Get the source code and run

Project details

Release history Release notifications | RSS feed

This version


Download files

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

Files for django-cache-utils2, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size django-cache-utils2-0.1.tar.gz (5.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page