Give an example of inserting bulk records to REDIS in C#

 Posted by Rajnilari2015 on 10/25/2016 | Category: NoSql Interview questions | Views: 132 | Points: 40
Answer:

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.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response