craig campbell

  • A while back I wrote about a way to keep data valid across multiple pages by invalidating cache keys. I have since thought of a better way to handle this.

    The basic idea is that anytime you have child objects related to a parent object you can use a property of the parent object to invalidate the children. To make this more clear let's revisit the original problem: You are building a blog with a lot of users where each user posts a bunch of articles. The articles are paginated and cached per page. If the user adds a new article, all the items on the page will shift and we need a way to invalidate all the pages in cache.

    The approach mentioned in my previous article involved having a namespace key that would store a random integer which would then be incremented when there is an update. This has a few problems:
    read more
  • added December 28, 2009 at 12:57 am
    updated May 24, 2010 at 1:03 am
    At work I have been working with memcache quite a bit and coming up with creative ways to cache stuff. Perhaps I will elaborate on some of these in later posts, but for now I think I will focus on one interesting issue I ran into using multiget.

    Using PECL Memcache extension the Memcache::get() method allows you to pass in a single key as a string or an array of keys to retrieve from cache. This can be very useful for retrieving lots of information with one request to memcache, however, there are a couple things to keep in mind. The first is something minor that I found annoying.

    Let's say you request array('item_1', 'item_2') from cache. If only item 1 is in cache then the resulting array will be something like this: array('item_1' => 'this is the value for item 1'). This means you have to actually check if a key exists in the resulting array or not in order to know how to proceed.

    The second thing had me banging my head against the wall for a short while since it was not possible to reproduce on a development environment. If you request multiple items from cache in one multiget request and the items requested are distributed over more than one server then they will not be returned in the same order that they were requested in.
    read more
  • Recently at work we were presented with an interesting problem, one of many problems that came up from using the Google Search Appliance to index and handle search results on a large site (I will probably touch on some of the others in future blog posts). The problem is this:

    We have a site containing many content items that are all indexed by the Google Search Appliance. We have to manually feed XML to the GSA since we have very specific business requirements and allowing Google to crawl our site will not work. This means that when there are updates to items we need to let the GSA know to reindex this item with the correct information. For most things this is fine, but for ratings if we have to queue an item up each time a user rates an item that would significantly slow down the site and prove to not be the best solution. The rating does not have to be 100% up to the minute, but if something is indexed today and then rated 100 times without being reindexed, the rating in the index could be very far off. This means we needed a way to keep the item's ratings accurate in the search results.
    read more
  • added May 27, 2009 at 12:02 am
    updated March 9, 2010 at 2:30 am
    UPDATED 09 MAR 2010 SEE THIS POST

    I have been so incredibly busy lately that I have not had anytime to write here. Anyway I just wanted to take some time to talk about something cool I have been doing for a project I have been working on outside of work.

    By now everyone knows what Memcached is so I won't bore you with an introduction. Basically it is an insanely simple caching system designed to eliminate load on the database.

    While in an ideal world you would just throw cache on everything and watch your application scale to infinity and beyond, things aren't always that simple. The problem I'm going to show you today has to do with pagination and cached database queries. I will try to make this pretty straightforward so anyone can follow.
    read more