Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5488

Redis Introduction with PHP

$
0
0

Redis is an open source, in memory data structure store, which can be used either as NoSQL database or cache. It works on advanced key-value store and retrieve process. It supports various types of data structures like strings, lists, sets and hashes having lots of operations on it. It is mostly used for cache replication but it can also be used like a database as well.

Installation of Redis.

Before installing Redis you have to confirm that your system has TCL installed if not you can simply install it by executing this command.

sudo apt-get install tcl

Now to install Redis you have to follow the procedure as follows-:

wget download.redis.io/releases/redis-stable.tar.gz

tar xzf redis-stable.tar.gz

cd redis-stable

make

Note-: Redis binary files are stored in the src subdirectory located in redis-stable directory.

To run the redis server you have to execute the command

src/redis-server

If you want to explore some basic commands regarding redis, try their interactive tutorial.

Integrating Redis with php using Predis.

Predis is a redis client which is used to integrate the redis with php, to use the predis we have to clone it from its official repository.

git clone git://github.com/nrk/predis.git

Connect your php application to Redis

<?php
require “predis/autoload.php”;
Predis\Autoloader::register();
try {
$redis = new PredisClient(); //If redis server and client is on same server
$redis = new PredisClient(array("scheme" => "tcp","host" => "192.168.1.10","port" => 6379 )); //if redis server is installed on some remote server, 192.168.1.10 is remote server IP.
));
}
catch (Exception $e) {
die ($e -> getMessage());
}
Now you can perform various getter and setter operations on Redis.
Getter -> to get the key pair value.
Setter -> to set the key pair value.
Lets take an example to understand the basic functionality.

$redis-> set (“counter”, 2);
$redis-> incr (“counter”);
$cvalue-> $redis->get(“counter); //cvalue stores the value 3

 

With the help of Redis we can perform various operations on sets, strings, hashes and lists as well as control the flow of application’s content to make it fast with the help of caching.


Viewing all articles
Browse latest Browse all 5488

Trending Articles