vijaykatam / django-cache-manager

Cache manager for django models
MIT License
31 stars 19 forks source link

django-cache-manager

Simple cache manager for django models that caches querysets for a model. Cache manager will cache any query that has been seen for a model. Model cache is evicted for any updates/deletes to the model. This manager is useful for models that don't change often.

Build Status Coverage Status PyPI version

Installation

pip install django-cache-manager

Caching strategy

Usage

Add to installed apps

INSTALLED_APPS = (
    ...
    'django_cache_manager',
    ...
)

Define cache backend for django_cache_manager.cache_backend in settings.py. The backend can be any cache backend that implements django cache API.

CACHES = {
    'django_cache_manager.cache_backend': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
    }
}
from django_cache_manager.cache_manager import CacheManager
class MyModel(models.Model):

   #set cache manager as default
   objects = CacheManager()

Django shell

To run django shell with sample models defined in tests.

make shell

Sample models

from tests.models import Manufacturer
from tests.models import Car
from tests.models import Driver
m = Manufacturer(name='Tesla')
m.save()
c = Car(make=m, model='Model S', year=2015)
c.save()
d = Driver(first_name ='ABC', last_name='XYZ')
d.save()
d.cars.add(c)
drivers = Driver.objects.select_related('car', 'manufacturer').all()

Testing

To run tests

make test
Supported Django versions

Supported - 1.5, 1.6, 1.7, 1.8, 1.9, 1.10