Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Johnny Cache. Clever twist on ORM-level caching for Django. Johnny Cache (great name) monkey-patches Django’s QuerySet classes and caches the result of every single SELECT query in memcached with an infinite expiry time. The cache key includes a “generation” ID for each dependent database table, and the generation is changed every single time a table is updated. For apps with infrequent writes, this strategy should work really well—but if a popular table is being updated constantly the cache will be all but useless. Impressively, the system is transaction-aware—cache entries created during a transaction are held in local memory and only pushed to memcached should the transaction complete successfully.

Tagged , , , , , , ,

3 comments

  1. Wow, I was basically writing most of this same idea today for a project. Must be something in the water.

    Caching (and invalidation) like this is so hard to get right, it would be a huge benefit to build this into Django proper someday.

    Brian Beck - 1st March 2010 00:01 - #

  2. Is it true that a single write to a tables invalidate all the reads involve with this table?

    pablo - 1st March 2010 16:07 - #

  3. @pablo

    It should, because otherwise you would have to semantically parse queries to understand wether it was affected by the write at hand. Such behaviour should be left to hand-crafted caching mechanisms.

    bastih - 1st March 2010 16:50 - #

Sign in with OpenID

Auto-HTML: Line breaks are preserved; URLs will be converted in to links.

Manual XHTML: Enter your own, valid XHTML. Allowed tags are a, p, blockquote, ul, ol, li, dl, dt, dd, em, strong, dfn, code, q, samp, kbd, var, cite, abbr, acronym, sub, sup, br, pre

A django site