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() {
    console.log('connected');
});

//On error of connection
client.on('error', function() {
    console.log('error');
});



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

Get Stored value
client.get('technology');

Delete Stored value
client.del('technology', function(err, reply) {
    console.log(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");
    }
    //console.log(msg);//ok    
});




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', 'abc1@no-spam.ws', 'abc2@no-spam.ws', 'abc3@no-spam.ws'], 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

Output
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
Command
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
1)"Arun"
2)"Tarun"
3)"Karan"



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: 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('127.0.0.1', 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);   
print_r($list);
/*Array ( [0] => Mysql [1] => Mongodb [2] => Redis [3] => MySQL [4] => PHP [5] => Mysql ) */
Question: How to get all keys from redis?
$keyList = $redis->keys("*");
print_r($keyList);
/*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 It is used?
Redis is used for caching to speed up web 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.
    wamp\bin\php\php5.x.xx\ext\
  6. Add the following line in your php.ini
    extension=php_redis.dll
  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();
}