Skip to content

Instantly share code, notes, and snippets.

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()
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)
Cachit.objects.all()

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

Cachit.objects.create(key_data=5)

CacheIt.objects.all()      # Likely to return stale data
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
objects = CachedManager(default_from_cache=True, cache_timeout=1200)
@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
@Bpless
Bpless / gist:1771461
Created February 8, 2012 17:26
DealUser Object Example
{
"_id" : ObjectId("4f15adc6e9f3d887f2ba7a31"),
"uid" : 274790,
"did" : 720717,
"city_id" : 4,
"score" : 10000,
"published" : ISODate("2012-01-17T17:20:54Z"),
"active" : true,
"recs" : [
{
@Bpless
Bpless / gist:1771930
Created February 8, 2012 18:23
MongoEngine Compression Code
from mongoengine.base import TopLevelDocumentMetaclass
class CompressedKeyDocumentMetaclass(TopLevelDocumentMetaclass):
def __new__(cls, name, bases, attrs):
"""
MongoEngine Document Classes access the 'TopLevelDocumentMetaclass'
__metaclass__. We allow that metaclass to set attrs on the class
and then compress the fields in the event that the instantiated
Document Class contains the meta attr 'compress_keys'
@Bpless
Bpless / gist:1771997
Created February 8, 2012 18:35
Compressed Document Example
from mongoengine.document import Document, EmbeddedDocument
class DecisionDocument(EmbeddedDocument):
guilt = fields.StringField(required=True)
details = fields.DictField()
meta = {'allow_inheritance': False}