hashing keys across servers

Jed Reynolds lists at benrey.is-a-geek.net
Thu Oct 19 05:26:47 UTC 2006


Since I'm not using a PECL library for PHP, I found the phpCA memcache 
client [ http://phpca.cytherianage.net/memcached/ ] and it seems to work 
alright. I had to dig into it a bit to figure out it's "buckets" model 
for mapping keys across servers. I did have to make this modification:

   // map a text key to an integer that can be
   // modulated to an index
   // of a server bucket
   function _hashfunc ($key)
   {
      $hash = 0;
      // the following hash doesn't work with my keys
      /*
      for ($i=0; $i<strlen($key); $i++)
      {
         $hash = $hash*33 + ord($key[$i]);
      }
      */
      // this has better distribution
      $hash = crc32( $key );

      return $hash;
   }


Which brings up a question: Is the hash function I commented out a 
pretty common one?
I understand that hash functions have to be tuned to the values of the 
keys they use. So is my modification a pretty common one? Because I was 
finding that this has function modulated all my keys to one index.

Jed


More information about the memcached mailing list