#native_company# #native_desc#

Add NoSQL Data Storage to Your PHP Development with Redis Page 2

By Keith Vance
on April 28, 2010


A Redis string is analogous to a PHP variable. In PHP you might write:
$firstname = 'Keith';
With Redis you would write:
$r->set('firstname', 'Keith');
In the movie application you write:
$r->set('firstname', $_GET['firstname']);
Strings have a one-to-one relationship with their values. If you need to store a list of things, you could use a Redis list data type. Lists in Redis are like PHP arrays. They’re indexed numerically starting with zero.
To create a Redis list, or add new data to an existing one, you use either LPUSH or RPUSH. With LPUSH, the new element is pushed onto the head of the array. If you use RPUSH, the element is added to the tail of the array.
With our Redis PHP abstraction layer, the method is simply called “push.” You can specify whether you want to append the element to the head or the tail of the Redis list with a method attribute. In the movie application, you’re going to store the genres in a list by simply issuing this method call:
$r->push('genres', $_GET['genre']);
What’s nice about lists is that they’re really fast in terms of adding elements. So while a list isn’t necessarily the best data type choice for storing movie genres, lists are great for handling message queues, which can contain hundreds of thousands of records. But if you need to search through the list for a specific element, that can be laborious and slow.
The other downside of lists is that the data elements can contain duplicates. Well, it’s not necessarily a downside, but that’s how arrays work. For instance, if you enter “Horror” in the form as a genre twice, it will be added to the list twice.
That brings us to Redis sets. A set is similar to a PHP hash, but with Redis the data is the key and the value is always true. If you look at the movie application sample, you’re going to create a dataset of actors using a Redis set. Unlike with lists, you can’t enter duplicates. Entering “George Clooney” more than once will not result in a duplicate “George Clooney” element in your Redis Set.
To add data to a set, you use the command sadd like this:
$r->sadd('actors', $_GET['actor']);
What’s great about sets is that you can check if a given element exists. You can perform intersections and unions, as well as check for differences between sets.
The last Redis type is the most complex: the sorted set. With sorted sets you can specify a “score” for each element and than sort your list based on that score. Just like a Redis set, a sorted set is similar to a PHP hash, but the values aren’t always set to true. Rather, the value is a number that represents the element’s score.
To add a new element to a sorted set, you use the command zadd.
$r->zadd('movies', $r->zscore('movies', array_pop($r->zrange('movies', -1, -1)))+1, $movieid);
There’s a lot going on here, so let’s break it down. The zadd command takes three arguments: key, score and value. In our example, the key is movies. You’re storing your favorite movies in a sorted set so you can rank them from most favorite to least favorite. However, you don’t want to store the movie name (for instance, “The Big Lebowski”) in the sorted set. You want to generate a movie ID and use that instead.
Redis provides a handy command call cincr, it increments a Redis string data point. In our example application, the key is called “movieids.” It’s actually not a string but rather an integer, but like PHP, Redis figures all that out for you.
So what you need to do is generate a movie ID using the incr command, save the movie title and associate it with the movie ID, and then add the movie ID to the sorted set with the score or rank.
$movieid = $r->incr('movieids');
$r->set('movie:' . $movieid, $_GET['movie']);
$r->zadd('movies', $r->zscore('movies', array_pop($r->zrange('movies', -1, -1)))+1, $movieid);
Notice that second line, which is storing the movie title from the GET variable. What you’re doing here is just saving the movie title in a Redis string that is named movie:$movieid, where $movieid is the unique identifier for the current movie generated with the incr command. That means that when you’re looping through your sorted set of movie IDs, to get the movie title you just have to query Redis for the string that contains the title.
In our example, when rendering the movie titles to the Web browser, you’re going to loop the movie IDs and call the get command to pull the movie’s title.
$r->get('movie:' . $movie);


While it’s certainly acceptable to just run one instance of your Redis database, you might need your application to scale. If you decide to build a massive social networking application with Redis and your server starts to bog down, adding Redis slave servers to balance the load across any number of Redis servers is simple. For more information on Redis replication, check out the Replication How-to.


As you can see, building a Web application using PHP and Redis is really simple. Comb through the sample code provided with this tutorial and hack away at it to see what you can do. You should also check out the Retwis Twitter-clone application. It will give you an even deeper understanding of what you can do with Redis and PHP.
Redis also includes a handy command-line shell client. It’s helpful when analyzing data and performing database administration functions. For example, if you want to clean out your database and start over, run ./redis-cli in the directory where you installed Redis and issue the flushall command.
About the Author
Keith Vance is a software engineer and a journalist. He’s been developing web applications professionally since 1997, and he received his journalism degree from the University of Washington in 2008. to e-mail him.