Tuesday, July 18, 2017

Getting Started with Redis in PHP

What is Redis?

Redis created by Salvatore Sanfilippo is an open source, in-memory data structure server with advanced key-value cache and store, often referred to as a NoSQL database. It is also referred to as a data structure server, since it can store strings, hashes, lists, sets, sorted sets, and more.

The essence of a key-value store is the ability to store some data, called a value inside a key. This data can later be retrieved only if we know the exact key used to store it.

Usage of Redis

  • Caching can be used in the same manner as memcached.
  • Leaderboards or related problems.
  • Counting stuff.
  • Real time analysis.
  • Deletion and filtering.
  • Show latest item listings in your home page.

Install on Windows

Now, let us check how to set up Redis PHP driver.

You need to download the phpredis from github repository https://github.com/nicolasff/phpredis. Once you’ve downloaded it, extract the files to phpredis directory.

extension = redis.so

Connect to Redis Server

<?php
//echo phpinfo();exit;
//Connecting to Redis server on localhost
$redis = new Redis();
$status=$redis->connect('XXX.XXXX.XXX.XXX', $portnumber);
echo "<pre>";
echo "Connection to server sucessfully";
//check whether server is running or not
echo "Server is running: ".$redis->ping();
//creating the key
$key="Key_Name";
$redis->set($key, 'MAK Key Venki');
//Retreving the specified key value
echo $redis->get($key);
// Get the stored keys and print it
$arList = $redis->keys("*");
echo "Stored keys in redis:: " ;
print_r($arList);
?>
view raw redis.php hosted with ❤ by GitHub