Redis Introduction
Redis is one of the latest and mostly used caching mechanism in PHP. Redis is store data in key valu pair and the data is stored in the cache memory so it makes loading the data at very fast rate. Hence as redis is mostly used for caching rather then proper database.
For php there are lot of clients available that provide Redis support like:
1. Predis
2. Credis
3. phpish/redis
4. Rediska
There are few more php redis client that are mostly used for php. but this are major and active clients.
Predis is best one in this clients as it regularly updates and also easy to implement as on dll files are
required to be installed on php but one can use it directly.
Predis Installation and Syntax:
To install predis just download the predis from the git site as linked mentioned above and put the folder on your project folder where to use or can clone from git.
In the predis there is the example folder where you can see lot of example using predis syntax.
For example create a test,php in the examples folder and write the following code:
require __DIR__.'/shared.php';
$single_server = array( | |
'host' => '127.0.0.1', | |
'port' => 6379, | |
'database' => 15, | |
); |
$test_redis = new Predis\Client($single_server); // To create redis object and connect redis server
$test_redis->set('first name'=>'XYZ'); //So the value will be stored on the server as key value payer here key is 'name' and value is 'XYZ';
$get_value = $test_redis->get('first name');
print_r($get_value); // Will output 'XYZ';
So this is the basic example to use Redis. There are lot of other usefull function of redis that can be used. Here is the list:
1. $test_redis->incr('visitor_count'); // So the value for the 'visitor count will be increased by one every time it will be used';
2.$test_redis->incrby('visitor_count', 4); // This will increment the value by 4;
3.$test_redis->decr('visitor_count'); // decrease the value by one.
4.$test_redis->decrby('visitor_count',4); // decrease by specified value;
5.To store in multidimensional form or array Redis has hset function:
$test_redis->hset('India','National Fruit','Mango');
$test_redis->hset('India','National Flower','Lotus');
To get
$national_fruit = $test_redis->hget('India','National Fruit');
print($national_fruit); // Will output Mango;
To delete
$test_redis->hdet('India','National Fruit');// Will delete the National Fruit
6. To expire after particular time :
$test_redis->expire('first name',1800); // This will expire after 1800 seconds;
The above list shown few important functions of redis there are lot more. Now the is some practical example for the redis caching in php.
Suppose we want to cache the all users list record so we can do like this:
$redis_users = $this->User->find('all'); //Get all the users;
Now store this in the redis like this:
$test_redis->set('all_users'=>$redis_users);
In the view you can get by just get method as told above. This users can also be set to expire if the users detail are changing so first on can check if the value is there in the cache if there fetch from there else create cache and fetch from there. Or can set a cron job that will automatically delete the cache after certain interval.
Comments
Post a Comment