Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

“.

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.

Adding Product Model for demo


namespace WebCacheStackExchangeDemo.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
}
Implementing AddAsync, AddAllAsync methods
AddAsync
Storing an object into Redis using AddAsync.
While storing, we are using “Db0” which is database 0, you can configure 16 different
databases. To add, we are using AddAsync method.
 Key

 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;
}

public async Task<IActionResult> Index()


{
var product = new Product()
{
Id = 1,
Name = "hand sanitizer",
Price = 100
};

bool isAdded = await _redisCacheClient.Db0.AddAsync("Product",


product, DateTimeOffset.Now.AddMinutes(10));

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;
}

public async Task<IActionResult> Index()


{

var values = new List<Tuple<string, Product>>


{
new Tuple<string, Product>("Product1", new Product()
{
Id = 1,
Name = "hand sanitizer 1",
Price = 100
}),
new Tuple<string, Product>("Product2",new Product()
{
Id = 2,
Name = "hand sanitizer 2",
Price = 200
}),
new Tuple<string, Product>("Product3", new Product()
{
Id = 3,
Name = "hand sanitizer 3",
Price = 300
})
};

await _redisCacheClient.Db0.AddAllAsync(values,
DateTimeOffset.Now.AddMinutes(30));

return View();
}
}
}
Output

You might also like