【问题标题】:Microsoft.Extensions.Caching.Redis select different database than db0Microsoft.Extensions.Caching.Redis 选择与 db0 不同的数据库
【发布时间】:2018-07-11 09:21:40
【问题描述】:

关于了解使用哪个redis数据库以及如何配置的问题。

我有一个默认的 ASP.NET Core Web 应用程序和一个默认配置的本地 redis-server(包含 15 个数据库)

通过我安装的包管理控制台:

Install-Package Microsoft.Extensions.Caching.Redis

Redis 在 Startup.cs 中配置如下:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddDistributedRedisCache(option =>
    {
        option.Configuration = "127.0.0.1";
        option.InstanceName = "master";
    });
}

将值读取和写入缓存的代码取自文档:

var cacheKey = "TheTime";
var existingTime = _distributedCache.GetString(cacheKey);
if (!string.IsNullOrEmpty(existingTime))
{
    return "Fetched from cache : " + existingTime;
}
else
{
    existingTime = DateTime.UtcNow.ToString();
    _distributedCache.SetString(cacheKey, existingTime);
    return "Added to cache : " + existingTime;
}

但无论我配置什么,这段代码都只使用默认数据库db0

例如使用此配置:

services.AddDistributedRedisCache(option =>
{
    option.Configuration = "127.0.0.1";
    option.InstanceName = "db6";
});

导致:

我必须配置什么才能使用,例如db6?

我必须为此使用 Stackexchange.Redis 吗?

【问题讨论】:

  • 您可以阅读stackexchange.github.io/StackExchange.Redis/Configuration.html。我想你对“DefaultDatabase”选项感兴趣。
  • 你能提供一个文档链接吗?对于您在这里所说的问题:The code to read and write values into the cache is taken from the docs
  • 我想在我的项目中使用 Redis 缓存。您更喜欢使用Microsoft.Extensions.Caching.Redis 还是StackExchange.Redis

标签: c# asp.net caching redis


【解决方案1】:

Microsoft.Extensions.Caching.Redis 是 using Stackexchange.Redis 到 connect 到 Redis。

Configuration 字符串是 documented on StackExchange.Redis。也就是说,您应该能够做到:

services.AddDistributedRedisCache(option =>
{
    option.Configuration = "127.0.0.1;defaultDatabase=4";
    option.InstanceName = "master";
});

【讨论】:

  • 是的,这行得通,但是我如何使用一种配置连接到多个数据库?有可能吗?
  • @GuoHuang 你找到一个连接多个数据库的解决方案了吗?
猜你喜欢
  • 1970-01-01
  • 2012-09-24
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多