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