David Cramer's Blog

Python, Django, and Scale.

Caching Layer for Django ORM

I’ve finally completed what I’ll call “phase 1” of the caching layer. It handles the easiest, and for my cases, the most useful level of cache invalidation: removing objects.

“Phase 1” Features:

  • Automatic caching of querysets.
  • Invalidating querysets when an object is removed.
  • Caching querysets objects in a key by key basis (per-object caching).
  • Automatic invalidation of per-object caches.

Grab the code from Google and look it over if you’re interested. This is still very much in a “It probably doesn’t work correctly” phase, and the code will have a lot of cleanup and structure changes before it’s done.

Here’s a quick rundown of the intended functionality and SQL usage:

class MyModel(CachedModel): [...]

MyModel.objects.create(name="Test") [1 query]

MyModel.objects.all() [1 query ]
[Test,]

MyModel.objects.create(name="Test2") [1 query]

MyModel.objects.all() [no queries]
[Test,]

MyModel.objects.create(name="Test3") [1 query]

MyModel.objects.all().reset() [1 query]
>>> [Test, Test2, Test3]

MyModel.objects.get(name="Test2").delete() [1 query]

MyModel.objects.all() [2 queries]
[Test, Test3]

z = MyModel.objects.get(name="Test")
z.name = "Test Changed"
z.save() [1 query]

MyModel.objects.all() [no queries]
[Test Changed, Test3]

Comments