Professional Documents
Culture Documents
Azure Redis Implementation
Azure Redis Implementation
After configuring settings next, we are going to click on the Create button to create a Cache.
This process will take a bit of time, but you can monitor the status of it. If your status as
running means you Redis cache is ready to use.
After deployment of Redis is successful next, we are going to get Access keys to access Redis
Resource form C#.
Getting Access keys
Here we are going to click on Resource which we have created “CoreCache” in Settings you
will find Access keys.
Next, we are going set connection string in appsettings.json file. For that, we are going to use
Primary connection string from Access Keys.
"Redis": {
"Password": "uefEg7DelBFFbAIw=",
"AllowAdmin": true,
"Ssl": true,
"ConnectTimeout": 6000,
"ConnectRetry": 2,
"Database": 0,
"Hosts": [
{
"Host": "CoreCacheDemo.redis.cache.windows.net",
"Port": "6380"
}
]
}
appsettings.json file
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Redis": {
"Password": "uefEg7DezCTgTJDnECMexdKqxTlg+NiWkRilBFFbAIw=",
"AllowAdmin": true,
"Ssl": true,
"ConnectTimeout": 6000,
"ConnectRetry": 2,
"Database": 0,
"Hosts": [
{
"Host": "CoreCache.redis.cache.windows.net",
"Port": "6380"
}
]
}
}
After setting connection string in appsettings.json file next, we are going register
“AddStackExchangeRedisExtensions” service in ConfigureServices Method.
Adding “AddStackExchangeRedisExtensions” method in the ConfigureServices method
In ConfigureServices Method first we are going to read Redis connection settings from
appsettings.json file.
var redisConfiguration =
Configuration.GetSection("Redis").Get<RedisConfiguration>();
After reading it we are going to pass values to AddStackExchangeRedisExtensions method as
show below.
public void ConfigureServices(IServiceCollection services)
{
var redisConfiguration =
Configuration.GetSection("Redis").Get<RedisConfiguration>();
services.AddControllersWithViews();
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfigu
ration);
}
After registering service next we are going add controller.
Adding DemoController and injecting IRedisCacheClient for accessing method
We are going to add a controller with name DemoController after adding we are going to add
a constructor to it. For injecting IRedisCacheClient dependency.
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis.Extensions.Core.Abstractions;
namespace WebCacheStackExchangeDemo.Controllers
{
public class DemoController : Controller
{
private IRedisCacheClient _redisCacheClient;
public DemoController(IRedisCacheClient redisCacheClient)
{
_redisCacheClient = redisCacheClient;
}
public IActionResult Index()
{
return View();
}
}
}
After creating a controller and adding constructor of class for injecting dependency next, we
are going to and simple model product and then we are going to implement methods of it.
Object to store
DateTimeOffset
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis.Extensions.Core.Abstractions;
using WebCacheStackExchangeDemo.Models;
namespace WebCacheStackExchangeDemo.Controllers
{
public class DemoController : Controller
{
private readonly IRedisCacheClient _redisCacheClient;
public DemoController(IRedisCacheClient redisCacheClient)
{
_redisCacheClient = redisCacheClient;
}
return View();
}
}
}
Output
I previously was using Redis Desktop Manager to see key and values which are stored in
Redis. But now the Redis Desktop Manager tool is not free. You can use another free
tool AnotherRedisDesktopManager that has cool features.
Download Another-Redis-Desktop-Manager
AddAllAsync methods
Storing multiple Object with single round trip.
We are storing a list of products with different keys in single go you can use this method to
add keys to Redis in a single request.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis.Extensions.Core.Abstractions;
using WebCacheStackExchangeDemo.Models;
namespace WebCacheStackExchangeDemo.Controllers
{
public class DemoController : Controller
{
private readonly IRedisCacheClient _redisCacheClient;
public DemoController(IRedisCacheClient redisCacheClient)
{
_redisCacheClient = redisCacheClient;
}
await _redisCacheClient.Db0.AddAllAsync(values,
DateTimeOffset.Now.AddMinutes(30));
return View();
}
}
}
Output