【发布时间】: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?