ASP.NET Core中的Redis缓存

时间:2016-11-01 20:01:41

标签: redis asp.net-core stackexchange.redis distributed-caching

我是Redis的新手并使用VS 2015和ASP.NET Core应用程序(v 1.0),我安装了nugget包:

Install-Package StackExchange.Redis

但是我无法将其注入并配置到我的服务中,没有 RedisCache 或" AddDistributedRedisCache "方法

我如何注射和使用它?

2 个答案:

答案 0 :(得分:5)

01.从download下载最新的redis,从services.msc安装并启动redis服务

02.在 project.json

中添加两个库
"Microsoft.Extensions.Caching.Redis.Core": "1.0.3",
"Microsoft.AspNetCore.Session": "1.1.0",

03.在

中添加依赖注入
public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry(Configuration);

        services.AddMvc();
        //For Redis
        services.AddSession();
        services.AddDistributedRedisCache(options =>
        {
            options.InstanceName = "Sample";
            options.Configuration = "localhost";
        });
  } 
  1. 并在Configure方法中添加 app.UseMvc 行的顶部

    app.UseSession();

  2. 在asp.net核心的会话存储中使用redis。现在可以在 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();
        }
     }
    

答案 1 :(得分:0)

我使用以下解决方案并获得了答案。

1。下载 Redis-x64-3.0.504.msi 并安装(link

2。从Nuget Packge Manager安装Microsoft.Extensions.Caching.StackExchangeRedis

3。在Startup.cs类的内部,在ConfigureServices方法中,添加以下命令:

 services.AddStackExchangeRedisCache(options => options.Configuration = "localhost:6379");
  1. 将IDistributedCache注入控制器:

     private readonly IDistributedCache _distributedCache;
    
     public WeatherForecastController( IDistributedCache distributedCache)
     {
         _distributedCache = distributedCache;
     }
    

5。最后:

   [HttpGet]
    public async Task<List<string>> GetStringItems()
    {
        string cacheKey = "redisCacheKey";
        string serializedStringItems;
        List<string> stringItemsList;

        var encodedStringItems = await _distributedCache.GetAsync(cacheKey);
        if (encodedStringItems != null)
        {
            serializedStringItems = Encoding.UTF8.GetString(encodedStringItems);
            stringItemsList = JsonConvert.DeserializeObject<List<string>>(serializedStringItems);
        }
        else
        {
            stringItemsList = new List<string>() { "John wick", "La La Land", "It" };
            serializedStringItems = JsonConvert.SerializeObject(stringItemsList);
            encodedStringItems = Encoding.UTF8.GetBytes(serializedStringItems);
            var options = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(1))
                .SetAbsoluteExpiration(TimeSpan.FromHours(6));
            await _distributedCache.SetAsync(cacheKey, encodedStringItems, options);

        }
        return stringItemsList;
    }

祝你好运!