craig campbell

  • added August 3, 2010 at 12:44 pm
    A while back I wrote about using TextMate to do code syntax highlighting for your blog. While I still consider that to be a perfectly valid method I realized it was pretty cumbersome after using it myself for a while.

    I began to search far and wide for alternatives and while there are plenty around most of them are pretty bad. I came upon this post by Dominic Szablewski and was inspired to begin creating my own syntax highlighting library.

    Where did the name Nijikodo come from?
    I decided I wanted to use some Japanese word so I translated "rainbow" which turned into "niji" and "code" which turned into "kodo". I'm sure it isn't a perfect translation.
    read more
  • added July 24, 2010 at 2:22 pm
    Most PHP developers are probably well aware of this already, but the release of PHP 5.3 brought a few cool features to the language including namespaces, late static binding, and anonymous functions (closures).

    In the interest of keeping this brief I just wanted to provide a few simple examples.

    Let's imagine you have an array of data:
    $users = array( array('id' => 1, 'name' => 'Tom', 'birthdate' => '1980-05-05'), array('id' => 2, 'name' => 'Julia', 'birthdate' => '1985-07-07'), array('id' => 3, 'name' => 'Michael', 'birthdate' => '1974-11-17') );
    Now let's say you wanted to grab all the user ids from this array. Normally you would do something like this:
    $ids = array(); foreach ($users as $user) { $ids[] = $user['id']; }
    With anonymous functions you could achieve the same thing by doing:
    $ids = array_map(function ($user) { return $user['id']; }, $users);
    read more
  • 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