af83

Which PHP Library to use with Redis?

Redis is magic. You should use it everywhere. Well, maybe not everywhere but it is a damn useful tool. It has great many bindings to great many languages. We have been using it on many projects, and usually the client bindings represent the design simplicity of the server.

Well, and as it is usually that is not true with PHP.

Most languages have a client library, PHP is blessed with at least 6! Including a C extension.

From the official doc at http://redis.io/clients

  • phpredis: This is a client written in C as a PHP module.
  • Predis: Mature and supported
  • Redis PHP bindings: This is a deprecated client, check Predis, Rediska, PHPRedis for up to date alternatives.
  • Redisent: justinpoliey
  • Rediska: shumkov

After looking at all of these I quickly turned to the C extension, Redisent looked nice but did not implement the new unified bulk protocol, and to be true, the code was a bit buggy. The others (Predis, Rediska ) seemed like bloatware, that capture the real essence of PHP (making complicated incoherent APIs) while missing that of Redis (Simple, Fast, Awsomeness).

The extension did what is was supposed to, and works fast, with a small memory footprint. But then again, you need to compile it… and:

To my astonishment I discovered the authors discovered they would not get into the Ol' PHP Gentlemen's Club, if they did not randomly alter the API. All I was trying to do was to map Redis directly to HTTP (so you could do http://example.com/redis/get/1).

Which meant that :

  1. this would not work magically
  2. I needed to remember the API twice; once for every other Redis implementation, and once for PHP.

Examples: while most command stay the same, the nice "keys" is in the PHP extension "getKeys" , "del" became "delete", "llen" is now "lsize" but why just change randomly stuff when you can incoherently randomly change stuff so "ltrim" is now* "listTrim"*  !

Of course true to the PHP heritage this is undocumented.

So… still wanting to use Redis with PHP and not really wanting to play with this one, I came to Damz (Damien Tournoud) my dear friend from CommerceGuys, being the gracious CTO he is, we set down and revamped good old Redisent to work with the current protocol, while adding some unit tests and general optimizations. The good news is that Damz did most of the work… and he is a much better coder then me…

The library is just 70 lines of highly optimized code. It has the same performance as the PHP extension (though a considerably larger memory foot print: which can be an issue). There may be some more optimizations that can be done on talking with the socket and any expertise on this would be greatly appreciated.

Damz ever the purist, his Redisent just wraps around the protocol, I wanted to add a bit more ease of use, so I added a ten lines wrapper that gets two commands hgetall and info to give you associative arrays instead of a flat array, or plain text..

So, first great big thanks to Justin Poliey for having written the Library. You can read his original post over at http://blog.justinpoliey.com/tag/redisent

Now, he hasn't really been maintaining it.. so for the moment, you should probably be using Damz or AF83's version.

I solemnly pledge to continue to maintain it. latest commits are on https://github.com/AF83/redisent or https://github.com/damz/redisent

We still need to write many more tests…. but it seems to function properly.

blog comments powered by Disqus