redis sample code tutorial c#
ADD A Class
public class RedisConnectorHelper
{
static RedisConnectorHelper()
{
RedisConnectorHelper.lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
return ConnectionMultiplexer.Connect("localhost");
});
}
private static Lazy<ConnectionMultiplexer> lazyConnection;
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
}
and then Add code in the main:
static void Main(string[] args)
{
var program = new Program();
Console.WriteLine("Saving random data in cache");
program.SaveBigData();
Console.WriteLine("Reading data from cache");
program.ReadData();
Console.ReadLine();
}
public void ReadData()
{
var cache = RedisConnectorHelper.Connection.GetDatabase();
var devicesCount = 10000;
for (int i = 0; i < devicesCount; i++)
{
var value = cache.StringGet($"Device_Status:{i}");
Console.WriteLine($"Valor={value}");
}
}
public void SaveBigData()
{
var devicesCount = 10000;
var rnd = new Random();
var cache = RedisConnectorHelper.Connection.GetDatabase();
for (int i = 1; i < devicesCount; i++)
{
var value = rnd.Next(0, 10000);
cache.StringSet($"Device_Status:{i}", value);
}
}