A common issue in a high traffic dynamic site is keeping the content fresh, but still utilizing a content cache to serve pages up quickly. When you're able to keep close track of which pages serve up a specific chunk of data, it's pretty easy to just invalidate those pages, but when you have a site where small chunks of data may be impacting several pages, you need a way to group related objects together in a "cache class". This decouples the cache keys from the actual object that is changing and allows you to easily manage how your content is invalidated. Since most of our cache implementations utilize memcached, there isn't an easy way in memcached to group keys together for invalidation. Our approach is to use a separate entry that is an array of all keys that make up a specific class. When a new page or item is created, the following process occurs:

  • Build content and put it in the cache normally.
  • If the content block or page is assigned to a class (we do this with decorators in Python), fetch the "class" key from the cache, which is just a list of cache keys.
  • Append the new key to the list, and put the "class" key back in the cache

Now, when you change the object that is the cache class is dependent on, instead of trying to keep track of all of your keys in the save method of the object, you can just invalidate the cache class. This simply involves getting the class key from the cache and invalidating each object in the list. If you're using a application framework (suich as Django!), you can wire up the model objects to a signal that will always invalidate certain classes when those objects change. There is certainly extra overhead in doing this, and each site is different. It is important to think about how your classes are split up to optimize this strategy. When you have a site that has a certain section that has times when it is heavily dynamic, this keeps the rest of the site that isn't changing snappy and saves the processing power for the really dynamic bits.

blog comments powered by Disqus
Close

Contact Us