A Redis Interface for Modest Developers
I wasn't really satisfied with the current PHP interface to Redis, so I wrote my own. If you don't know what Redis is, it's a distributed key-value store. It's different from memcached in that it's persistent, meaning that keys aren't lost when the server is shut down, and it supports abstract data types like lists and sets.
My implementation is called Redisent, and it's available on Github. I tried to make Redisent as small, simple, and intuitive as possible. To do so, I made all of the methods directly map to Redis protocol commands. Here's some simple example usage:
require 'redisent.php';
$redis = new Redisent('localhost');
$redis->set('awesome', 'absolutely');
echo sprintf('Is Redisent awesome? %s.\n', $redis->get('awesome'));
This bit of code sets a key, and then retrieves it.
Redisent also supports clustering of servers, and uses consistent hashing to distribute keys across servers in a way that will allow you to add and remove servers from the cluster without having most keys be remapped to different servers. To use clustering, use the RedisentCluster class:
require 'redisent_cluster.php';
$cluster = new RedisentCluster(array(
array('host' => '127.0.0.1', 'port' => 6379),
array('host' => '127.0.0.1', 'port' => 6380)
));
Now keys will be hashed and distributed to different servers, and the method names you use are exactly the same as the regular Redisent class. Not all Redis commands operate on keys though, some are server-specific. If you want to execute a command on a specific server, you can give a server an alias and then use the to($alias) method to execute the command on the server with the given alias.
require 'redisent_cluster.php';
$cluster = new RedisentCluster(array(
'alpha' => array('host' => '127.0.0.1', 'port' => 6379),
array('host' => '127.0.0.1', 'port' => 6380)
));
echo $cluster->to('alpha')->info();
Now the Redis server running on 127.0.0.1:6379 has the alias alpha, and you can route commands to it by using the to method. To see an example application using Redisent, the blog application distibuted with Tessera uses Redis as a datastore, and uses Redisent to interface with it.
Comments [1]