Write the code for fetching record from Redis using C#

 Posted by Rajnilari2015 on 10/25/2016 | Category: NoSql Interview questions | Views: 2831 | 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 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.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response