Sunday, September 20, 2015

Redis and PHP Connection

Download the Predis library from the https://github.com/nrk/predis


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.

Redis CommandPHP Predis CommandComment
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 patternprint_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