Amazon Prime Day August 2020

Setup redis in PHP

Download predis and copy in your application
Download redis for windows and install

PHP Example
       
require "predis/autoload.php";
$redis = new Predis\Client('tcp://127.0.0.1:6379');
$redis->set('foo', 'bar');
$keysArr = $redis->get('foo').PHP_EOL;

print_r($keysArr);



Basic Commands:
Store data in redis: set <key_name> <value>
E.g: set technology php

List all keys: keys "*"

Get specific key:  get <key_name>

 Redis Pipeline Example:
The client can send multiple requests to the server without waiting for the replies at all, and finally reads the replies in a single step.

       

require "predis/autoload.php";
$redis = new Predis\Client('tcp://127.0.0.1:6379');
//$redis->auth('rahul'); //Authentication

$result = $redis->pipeline(function($pipe) {

/*
* Datatype:Sets Usage
*
*/
$colors = array('yellow', 'blue', 'green');
$key = 'colors';
foreach ($colors as $color)
{
$pipe->sadd($key,$color );
}

/*
* Datatype:Hashes usage
*
*/
$mobiles = array(
'lenovo'=>array(
'ram'=>'2GB',
'memory'=>'32GB',
'size'=>'5',
),
'redmi'=>array(
'ram'=>'4GB',
'memory'=>'64GB',
'size'=>'5.5',
),
);
foreach($mobiles as $mobile=>$data){
foreach($data as $key=>$val){
$pipe->hmset($mobile, [
$key=>$val,
]);
}
}
$pipe->smembers('colors');
$pipe->hgetall('lenovo');
$pipe->hget('redmi','ram');
});


print_r($result);











Post a Comment

0 Comments