Showing posts with label redis. Show all posts
Showing posts with label redis. Show all posts

Monday 12 June 2017

Node js Redis tutorial - Quick Start

Install redis in nodeJS, Make redis connection and stored data in redis, Get data from redis, update and delete the data from redis.

Question: How to install redis module with node?
npm install redis

Question: How to include redis in nodeJS Project?
var redis = require("redis");

Question: How to connect to redis with nodeJS?
var client = redis.createClient();

//On connection
client.on('connect', function() {

//On error of connection
client.on('error', function() {

Question: How to Store data/Get Data/ Delete data in redis with nodejs?
Store single value
client.set('technology', 'NodeJS');

Get Stored value

Delete Stored value
client.del('technology', function(err, reply) {

Question: How to check Stored value exist OR Not
client.exists('technology', function(err, reply) {
    if (reply === 1) {
        console.log('Stored in Redis');
    } else {
        console.log('Not Stored in Redis');

Question: What is HMSET in nodeJS?
HMSET is like hset but it allow to store value in "key/value" paris.

Question: How to store data in key/value pairs in redis?
client.hmset('frameworks_list', {
    'javascript': 'AngularJS',
    'css': 'Bootstrap',
    'node': 'Express'
}, function(err, msg) {
    if (err) {
        console.log("Error During save the data");

Question: How to get data which was stored with hmset redis?
client.hgetall('frameworks_list', function(err, msg) {
    if (err) {
        console.log("Error During save the data");
    //console.log(msg);//{javascript: 'AngularJS', css: 'Bootstrap', node: 'Express' }

Question: How to store unique values in redis?
client.sadd(['emails', '', '', ''], function(err, msg) {

Question: How to get stored values which was set sadd with in redis?
client.smembers('emails', function(err, msg) {

Wednesday 8 February 2017

Redis Commands with examples

Question: How to set the data with hset?
hset testdata name "Arun Kumar"

Question: How to get the data with hset?
hget testdata name

Question: How to set more data with hset?
hset testdata age "12"
hget testdata age

Question: How to get all data which was set with hset?
hgetall testdata

1) "name"
2) "Arun Kumar"
3) "age"
4) "30"

Question: How to delete data which was set with hset?
hdel testdata age

Question: How to get all data which was set with hset?
hgetall testdata
1) "name"
2) "Arun Kumar"

Redis Commands with examples

Question: How to insert data with lpush ?
lpush usernames "Arun"
lpush usernames "Tarun"
lpush usernames "Karan"

Question: How to list data which was set with lpush ?
lrange usernames 0 10

Question: How to insert data with zadd?
zadd testdata1 0 "Zero" 
zadd testdata1 1 "One" 
zadd testdata1 2 "Two" 
1) "zero"
2) "ONe"
3) "Two"

Question: Difference between io.sockets.emit and socket.broadcast.emit?
io.sockets.emit will send to all the clients.
socket.broadcast.emit will send the message to all the other clients except the newly created connection.

Tuesday 10 January 2017

Redis PHP Interview questions and answers

Redis php interview questions and answers

Question: What is Redis?
Redis is an advanced key-value data store and cache. It has is also referred to as a data structure server as such the keys not only contains strings, but also hashes, sets, lists.

Question: What are the advantages of using Redis?
  1. It provides high speed
  2. It has got command level Atomic Operation (tx operation)
  3. It supports a server-side locking
  4. It has got lots of client lib

Question: What are the limitations of Redis?
  1. It is single threaded
  2. It has got limited client support
  3. It is not deployed

Question: Why Redis is different as compared to other key-value stores?
  1. Redis values can contain more complex data types, with atomic operations.
  2. Redis is an in-memory but persistent on disk database.

Question: How to delete current database?
redis-cli flushdb

Question: How to remove all database?
redis-cli flushall

Question: How to check redis is running?
try {
    $redis = new Redis();
    $redis->connect('', 6379);
    echo "Redis is running.";
    echo "Server is running: " . $redis->ping();
} catch (Exception $e) {
    echo $e->getMessage();

Question: How to set value in redis?
$redis->set("name", "Set string in redis");

Question: How to get value from redis?
 echo $redis->get('name'); //Set string in redis

Question: How to set multiple values (Array) in redis?
$redis->lpush("tutorials", "PHP");
$redis->lpush("tutorials", "MySQL");
$redis->lpush("tutorials", "Redis");
$redis->lpush("tutorials", "Mongodb");
$redis->lpush("tutorials", "Mysql");

Question: How to get array data from redis?
$list = $redis->lrange("tutorials", 0 ,5);   
/*Array ( [0] => Mysql [1] => Mongodb [2] => Redis [3] => MySQL [4] => PHP [5] => Mysql ) */
Question: How to get all keys from redis?
$keyList = $redis->keys("*");
/*Array ( [0] => tutorials [1] => name ) */
Question: How to stop redis?
/etc/init.d/redis-server stop

Question: How to start redis?
/etc/init.d/redis-server start

Question: How to re-start redis?
/etc/init.d/redis-server restart

Question: How do I move a redis database from one server to another?
  1. use BGSAVE command to Save a spanshot of the database into a dump.rdb
  2. Copy this dump.rdb file into another server.

Wednesday 28 December 2016

How to install redis on wamp server?

How to install redis on wamp server?

Question: What is Redis?
it is an open source advanced key-value database storage system like NoSQL.

Question: Why Redis is used?
Redis is used for caching to speed up application.

Question: How redis helpful in Optimization?
Redis operations can be executed on the server side and reduce the client's workload.

Question: What type of data can be stored in Redis server?
Redis store complex data structures like strings, hashes, lists, sets, sorted sets, bitmaps and hyperlogs as its key values.

Question: Why redis is fast?
Redis is inherently fast as it stores data in memory.

Question: How to install redis in wampserver?
  1. Download the redis
       A) Download Redis setup For 32 Bit System and Install
       B) Download Redis Setup for 64 Bit system and follow below steps.    
    1. Extract the zip file
    2. Place the unzip file in c:\redis
    3. Run redis-server.exe from c:\redis
    4. then Run redis-cli.exe from c:\redis
  2. Check out your php version and know whether thread safety is enabled or not(by using phpinfo()).
  3. Now we need to download the php_redis.dll. Download the php_redis.dll from PECL.
    Download as per PHP Version and thread based.
  4. Extract the zip file.
  5. Copy the php_redis.dll and paste to following folder in Wamp Server.
  6. Add the following line in your php.ini
  7. Re-start the wamp Server.
  8. Do phpinfo() and search redis. It will start displaying which means its Redis is installed
  9. Learn and Work with redis.

Question: How to check redis is installed Or Not?
try {
    $redis = new Redis();
    $redis->connect('localhost', 6379);
    $redis->set('name', 'Redis is Installed');
    echo $glueStatus = $redis->get('name');
} catch (Exception $ex) {
    echo $ex->getMessage();