Полезная презентация: Cache rules everything around me
http://lanyrd.com/2011/djangocon-us/shbrr/
A slick app that supports automatic or manual queryset caching and automatic granular event-driven invalidation. It can also cache results of user functions and invalidate them by time or the same way as querysets. It uses redis as backend for ORM cache and redis or filesystem for simple time-invalidated one.
Ничего особо полезного не нашел. Есть бэкенд для pylibmc, но такой уже имеется в джанге.
Кэширует все запросы. есть серьезные ограничения
Avoiding the database at all costs was not a goal, so different ordering clauses on the same dataset are considered different queries. Since invalidation happens at the table level, any table having been modified makes the cached query inaccessible
# cached, depends on `publisher` table
p = Publisher.objects.get(id=5)
# cached, depends on `book` and `publisher` table
Book.objects.all().select_related('publisher')
p.name = "Doubleday"
# write on `publisher` table, modifies publisher generation
p.save()
# the following are cache misses
Publisher.objects.get(id=5)
Book.objects.all().select_related('publisher')
В объект добавляется поле cache.
Instance Caching
class Model(django.models.Model):
cache = autocache.CacheController()
field = django.models.TextField()
Model.objects.get(pk=27) # hits the database
Model.cache.get(27) # Tries cache first
Related Objects Caching
instance = Model.cache.get(pk=27)
related_things = instance.things_set.all() # hits the database
related_things = instance.cache.things_set # Tries cache first
Тоже есть ограничения
Autocache relies on the post_save and post_delete signals to keep your cache up to date. Performing operations that alter the database state without sending these signals will result in your cache becoming out of sync with your database.
Еще одна библиотека для автоматического кэширования и инвалидации. Ограничение - CachingManager должен быть менеджером модели по умолчанию
from django.db import models
import caching.base
class Zomg(caching.base.CachingMixin, models.Model):
val = models.IntegerField()
objects = caching.base.CachingManager()
Django caching decorator + invalidate function
from cache_utils2 import cached, invalidate
@cached(60)
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
foo(x=2)