Redis Cache in ASP.NET Core

I am new to Redis and using VS 2015 and the ASP.NET Core application (v 1.0), I installed the nugget package:

Install-Package StackExchange.Redis 

However, I cannot deploy and configure it in my services, there is no RedisCache or AddDistributedRedisCache method.

How can I enter and use it?

+6
source share
1 answer

01.Download last redis from download , install and start the service from services.msc redis

02. Add two libraries to project.json

 "Microsoft.Extensions.Caching.Redis.Core": "1.0.3", "Microsoft.AspNetCore.Session": "1.1.0", 

03.Add dependency injection to

 public void ConfigureServices(IServiceCollection services) { services.AddApplicationInsightsTelemetry(Configuration); services.AddMvc(); //For Redis services.AddSession(); services.AddDistributedRedisCache(options => { options.InstanceName = "Sample"; options.Configuration = "localhost"; }); } 
  1. and in the Configure method add top app.UseMvc line

    app.UseSession ();

use redis in the session store in the asp.net core. Now you can use this as HomeController.cs

 public class HomeController : Controller { private readonly IDistributedCache _distributedCache; public HomeController(IDistributedCache distributedCache) { _distributedCache = distributedCache; } //Use version Redis 3.22 //http://stackoverflow.com/questions/35614066/redissessionstateprovider-err-unknown-command-eval public IActionResult Index() { _distributedCache.SetString("helloFromRedis", "world"); var valueFromRedis = _distributedCache.GetString("helloFromRedis"); return View(); } } 
+4
source

Source: https://habr.com/ru/post/1011925/


All Articles