In my previous post (Awesomeness of Redis – Redis Installation and Configuration), I explained how to install and configure Redis server, a ultra fast Key-Value pair engine. As a follow up, this post (Awesomeness of Redis - redis and php) will focus on how to use PHP to connect to Redis server using Predis library.
Redis and PHP Connection:
First, you need to download the Predis library from the download page. Once downloaded, extract the contents and you can discard everything other than the Predis folder inside the lib directory. This is the core library files which you will be using from your PHP files to connect to Redis. Copy the directory Predis (which is inside the lib directory) in to the folder where your PHP script lies. Alternatively, you can copy the directory in
/usr/share/php/
for Ubuntu/Debian systems which is defined by default in php.ini as a place to place library files. By placing the directory in the above location, the Predis library will be accessible to all PHP scripts. Now that you have got the library files, It is time to load the library in your PHP code and connect to the Redis server.
The Predis comes with the auto-load function which will pretty much do everything and ease your work. Include the following line at the top of your PHP file.
require 'Predis/Autoloader.php';Predis\Autoloader::register();
These lines include and loads the file Autoloader.php inside the Predis directory which we copied in the last step. Now connect to your redis database from PHP using
$redis = new Predis\Client(array( 'scheme' => 'tcp', 'host' => '10.0.0.1', 'port' => 6379,));
Change the Parameter depending on the IP Address and the port in which your installed Redis. If you have set a passkey to Redis Instance, you need to use below snippet. Scheme is by default tcp.
$redis = new Predis\Client(array( 'host' => '10.0.0.1', 'port' => 6379, 'password' => {PASS_KEY}, 'database' => 0));
I have introduced a new parameter, database here. This lets you connect to the database you like, by default, it connects to 0. Now that you are connected, lets look into how to execute commands from PHP in your redis server. It is easy to add, modify, delete Keys from Redis server. I have compiled and listed out few basic and commonly used redis commands and its Predis equivalence below as I did not find the information to be readily available.
EditRedis Command | PHP Predis Command | Comment |
---|---|---|
SELECT index | $redis->select(index) | Change the selected database for the current connection |
FLUSHDB | $redis->flushdb(); | Remove all keys from the current database |
FLUSHALL | $redis->flushall(); | Remove all keys from all databases |
SAVE | $redis->save(); | Synchronously save the dataset to disk |
QUIT | $redis->quit(); | Close the connection |
PING | $redis->ping(); | Ping the server |
ECHO message | $redis->do_echo('ECHO test'); | Echo the given string |
SET key value | $redis->set('aaa', 'bbb') | Set the string value of a key |
SETNX key value | $redis->set('aaa', 'ccc', true); | Set the value of a key, only if the key does not exist |
GET key | $redis->get('aaa'); | Get the value of a key |
INCR key | $redis->incr('aaa'); | Increment the integer value of a key by one |
INCRBY key increment | $redis->incr('aaa', 2); | Increment the integer value of a key by the given amount |
DECR key | $redis->decr('aaa'); | Decrement the integer value of a key by one |
DECRBY key decrement | $redis->decr('aaa', 2); | Decrement the integer value of a key by the given number |
EXISTS key | $redis->exists('aaa'); | Determine if a key exists |
DEL key [key ...] | $redis->delete('aaa'); | Delete a key |
KEYS pattern | print_r($redis->keys('*'), true); | Find all keys matching the given pattern. In this case, all the keys |
RANDOMKEY | $redis->randomkey('a*') | Return a random key from the keyspace matching the pattern. |
RENAME key newkey | $redis->rename('a1', 'a0'); | Rename a key |
RENAMENX key newkey | $redis->rename('a0', 'a2', true); | Rename a key, only if the new key does not exist |
LPUSH key value [value ...] | $redis->push('a0', 'aaa'); | Prepend one or multiple values to a list |
RPUSH key value [value ...] | $redis->push('a0', 'ccc', false); | Append one or multiple values to a list |
LLEN key | $redis->llen('a0'); | Get the length of a list |
LRANGE key start stop | $redis->lrange('sdkjhfskdjfh', 0, 100), true) | Get a range of elements from a list |
LTRIM key start stop | $redis->ltrim('a0', 0, 1); | Trim a list to the specified range |
LINDEX key index | $redis->lindex('a0', 0); | Get an element from a list by its index |
RPOP key | $redis->pop('a0'); | Remove and get the last element in a list |
LPOP key | $redis->pop('a0', false); | Remove and get the first element in a list |
LSET key index value | $redis->lset('a0', 'ccc', 0); | Set the value of an element in a list by its index |
SADD key member [member ...] | $redis->sadd('s0', 'aaa'); | Add one or more members to a set |
SREM key member [member ...] | $redis->srem('s0', 'bbb'); | Remove one or more members from a set |
SISMEMBER key member | $redis->sismember('s0', 'aaa'); | Determine if a given value is a member of a set |
SINTER key [key ...] | $redis->sinter(array('s0', 's1')), true) | Intersect multiple sets |
SMEMBERS key | $redis->smembers('s1'), true); | Get all the members in a set |
MOVE key db | $redis->move('s1', 1); | Move a key to another database |
BGSAVE | $redis->save(true); | Asynchronously save the dataset to disk |
LASTSAVE | $redis->lastsave(); | Get the UNIX time stamp of the last successful save to disk |
HSET key field value | $redis->hset('a', "field","value'); $redis->hset('a', "field2","value"); | Set the string value of a hash field |
HGET key field | $redis->hget('a','field') | Get the value of a hash field |
HGETALL key | $redis->hgetall('a'); | Get all the fields and values in a hash |
The above table covers pretty much all the commands in Redis. For more information on Commands available in Redis visit the Command Reference.
This concludes our tutorial on How to Connect to Redis from PHP using client Predis. I would like to extent my thanks to Daniele Alessandri for his excellent work in developing Predis library.
Update: Table updated on May 28, 2012 with Predis Commands to work with Hashes Data Type of Redis.
Arun Chinnachamy
I am a chemical Engineer from BITS-Pilani. Right now, I work as Technology Lead at MySmartPrice. This is just a place where I write about the things I work and think.
Awesomeness of Redis - Redis and PHP was published onMay 08, 2012.