如何使用 StackExchange.Redis 通过 redis URL 连接到 redis 缓存?
How to connect to redis cache via redis URL using StackExchange.Redis?
目前,我正在使用ServiceStack.Redis连接到Redis缓存服务器。 redis缓存URL结构是这样的:redis://f:UIOPabcdXYZ@ip-172-31-81-144.manager.{domain}.io:33004
下面是使用 ServiceStack.Redis
连接到 Redis 缓存的示例代码
var redisClientManagerPool = new RedisManagerPool(ConfigManager.RedisCacheUrl);
using (var client = redisClientManagerPool.GetClient())
{
//SetClientData(key, client, data);
}
但是免费版有一定的限制,一旦达到限制就会报错
The free-quota limit on '6000 Redis requests per hour' has been
reached. Please see https://servicestack.net to upgrade to a
commercial license or visit https://github.com/ServiceStackV3/ServiceStackV3 to
revert back to the free ServiceStack v3
我想使用StackExchange.Redis
连接到redis缓存。但是我没有从 https://stackexchange.github.io/StackExchange.Redis/Basics 获得任何代码参考如何使用 redis URL 。
我正在寻找使用 redis 缓存 URL 的任何代码参考,例如 redis://f:UIOPabcdXYZ@ip-172-31-81-144.manager.domain.io:33004
通过使用 StackExchange.Redis
连接到 redis 缓存服务器小块。感谢您的帮助。
注:当前使用的库为.NET Standard 2.0版本ServiceStack.Redis。 nuget 名称是 ServiceStack.Redis.Core
版本 5.7.0.
StackExchange.Redis 适用于 redis://f:UIOPabcdXYZ@ip-172-31-81-144.manager.{domain}.io:33004
URL 格式。下面是代码参考:
var url = "redis://f:UIOPabcdXYZ@ip-172-31-81-144.manager.{domain}.io:33004";
var urlParts = url.Split("@ip-");
var password = urlParts[0].Split("redis://f:")[1].Trim();
var host = "ip-" + urlParts[1].Split(":")[0].Trim();
var port = int.Parse(urlParts[1].Split(":")[1].Trim());
var redisConfigurationOptions = new ConfigurationOptions
{
AllowAdmin = false,
Ssl = false,
Password = password,
EndPoints = {
{ host, port }
}
};
var conn = ConnectionMultiplexer.Connect(redisConfigurationOptions);
var client = conn.GetDatabase();
client.StringSet(cacheKey, Newtonsoft.Json.JsonConvert.SerializeObject(data), TimeSpan.FromMinutes(30));
//To retrieve data
if (client.KeyExists(cacheKey))
{
var outputString = client.StringGet(cacheKey).ToString();
}
else
{
//cacheKey doesn't exists
}
目前,我正在使用ServiceStack.Redis连接到Redis缓存服务器。 redis缓存URL结构是这样的:redis://f:UIOPabcdXYZ@ip-172-31-81-144.manager.{domain}.io:33004
下面是使用 ServiceStack.Redis
连接到 Redis 缓存的示例代码var redisClientManagerPool = new RedisManagerPool(ConfigManager.RedisCacheUrl);
using (var client = redisClientManagerPool.GetClient())
{
//SetClientData(key, client, data);
}
但是免费版有一定的限制,一旦达到限制就会报错
The free-quota limit on '6000 Redis requests per hour' has been reached. Please see https://servicestack.net to upgrade to a commercial license or visit https://github.com/ServiceStackV3/ServiceStackV3 to revert back to the free ServiceStack v3
我想使用StackExchange.Redis
连接到redis缓存。但是我没有从 https://stackexchange.github.io/StackExchange.Redis/Basics 获得任何代码参考如何使用 redis URL 。
我正在寻找使用 redis 缓存 URL 的任何代码参考,例如 redis://f:UIOPabcdXYZ@ip-172-31-81-144.manager.domain.io:33004
通过使用 StackExchange.Redis
连接到 redis 缓存服务器小块。感谢您的帮助。
注:当前使用的库为.NET Standard 2.0版本ServiceStack.Redis。 nuget 名称是 ServiceStack.Redis.Core
版本 5.7.0.
StackExchange.Redis 适用于 redis://f:UIOPabcdXYZ@ip-172-31-81-144.manager.{domain}.io:33004
URL 格式。下面是代码参考:
var url = "redis://f:UIOPabcdXYZ@ip-172-31-81-144.manager.{domain}.io:33004";
var urlParts = url.Split("@ip-");
var password = urlParts[0].Split("redis://f:")[1].Trim();
var host = "ip-" + urlParts[1].Split(":")[0].Trim();
var port = int.Parse(urlParts[1].Split(":")[1].Trim());
var redisConfigurationOptions = new ConfigurationOptions
{
AllowAdmin = false,
Ssl = false,
Password = password,
EndPoints = {
{ host, port }
}
};
var conn = ConnectionMultiplexer.Connect(redisConfigurationOptions);
var client = conn.GetDatabase();
client.StringSet(cacheKey, Newtonsoft.Json.JsonConvert.SerializeObject(data), TimeSpan.FromMinutes(30));
//To retrieve data
if (client.KeyExists(cacheKey))
{
var outputString = client.StringGet(cacheKey).ToString();
}
else
{
//cacheKey doesn't exists
}