NoSql Interview Questions and Answers (44) - Page 3

List out some of the features of REDIS

Some of the Redis features are -

- LRU (Less Recently Used) Eviction
- Messaging broker implenemtation via Publisher-Subscriber Support
- Disk Persistence
- Automatic Failover
- Transaction
- Redis HyperLogLog
- Redis Lua Scripting
- Act as database
- Act as a cache
- Provides high availability via Redis Sentinel
- Provides Automatic partitioning with Redis Cluster
- Provides different levels of on-disk persistence
What is ConnectionMultiplexer?

The connection to Redis is handled by the ConnectionMultiplexer class which is the central object in the StackExchange.Redis namespace. The ConnectionMultiplexer is designed to be shared and reused between callers.

e.g.

static IDatabase GetRedisInstance()

{
return
ConnectionMultiplexer
.Connect("localhost")
.GetDatabase();
}


The GetDatabase() of the ConnectionMultiplexer sealed class obtains an interactive connection to a database inside redis.
Give an example of inserting bulk records to REDIS in C#

Let us first create a model

 public class Student

{
public int StudentID { get; set; }
public string StudentName { get; set; }
public string Gender { get; set; }
public string DOB { get; set; }
}


Next create Redis Connector

public class RedisConnector

{
static IDatabase GetRedisInstance()
{
return
ConnectionMultiplexer
.Connect("localhost")
.GetDatabase();
}
}


Then write the below code to insert bulk records

private void InsertBulkStudent()

{
var numberOfRecords = 100;

var cache = RedisConnector.GetRedisInstance();

for (int i=1;i<= numberOfRecords;i++)
{
cache.StringSet(
"Student" + i
,JsonConvert.SerializeObject(
new Student() { StudentID = i
, StudentName ="Student" + i
, Gender = i%2 ==0 ? "Male":"Female"
, DOB = DateTime.Now.AddYears(i).ToString()
}
));
}
}


The StringSet method sets key to hold the string value. If key already holds a value, it is overwritten regardless of its type.
Write the code for fetching record from Redis using C#

Let us first create a model

 public class Student

{
public int StudentID { get; set; }
public string StudentName { get; set; }
public string Gender { get; set; }
public string DOB { get; set; }
}


Next create Redis Connector

public class RedisConnector

{
static IDatabase GetRedisInstance()
{
return
ConnectionMultiplexer
.Connect("localhost")
.GetDatabase();
}
}


Then write the below code for fetching record from Redis using C#

public List<Student> GetStudentRecords()

{
var studentList = new List<Student>();
var cache = RedisConnector.GetRedisInstance();
var numberOfRecords = 10;

for (int i = 1; i <= numberOfRecords; i++)
{
studentList.Add(JsonConvert.DeserializeObject<Student>(cache.StringGet("Student" + i)));
}
return studentList;
}


The StringGet method get the value of a key or nil if the key does no exist.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories