Skip to content

Instantly share code, notes, and snippets.

@Bpless
Bpless / gist:1443857
Created December 7, 2011 18:02
Caching result check
Cachit.objects.all()

Cachit.objects.all().update(key_data=3)

Cachit.objects.create(key_data=5)

CacheIt.objects.all() # Likely to return stale data
objects = CachedManager(default_from_cache=True, cache_timeout=1200)
OurModel.objects.all().from_cache()  # Will always hit the cache

OurModel.objects.all()       # Will only hit the cache if  __init__() method sets _retrieve_from_cache to True
Cachit.objects.all()

Cachit.objects.all().update(key_data=3)

Cachit.objects.create(key_data=5)

CacheIt.objects.all()      # Likely to return stale data
from django.db import models
from caching.base import CachingMixing, CachingManager, CachingQuerySet
class OurModel(CachingMixin, models.Model):
data = models.IntegerField()
objects = CachedManager(default_from_cache=True, cache_timeout=1200)
from django.db import models
from caching.base import CachingManager, CachingMixin
class CacheIt(CachingMixin, models.Model):
key_data = models.CharField(max_length=30)
related_stuff = models.ForeignKey('related.RelatedStuff')
objects = CachingManager()
from django.db import models
from caching.base import CachingManager, CachingMixin
class CacheIt(CachingMixin, models.Model):
key_data = models.CharField(max_length=30)
related_stuff = models.ForeignKey('related.RelatedStuff')
objects = CachingManager()