gpt4 book ai didi

c# - 使用 StackExchange.Redis ConnectionMultiplexer 而不是 BookSleeve ConnectionUtils 连接到 Redis sentinel

转载 作者:IT王子 更新时间:2023-10-29 06:06:50 24 4
gpt4 key购买 nike

如何使用 StackExchange.Redis ConnectionMultiplexer 而不是 BookSleeve ConnectionUtils 连接到 Redis sentinel。

我目前正在使用 BookSleeve:

ConnectionUtils.Connect("127.0.0.1:26379,serviceName=mymaster");

我尝试用 StackExchange.Redis 替换它:ConnectionMultiplexer.Connect("127.0.0.1:26379,serviceName=mymaster");

但它不起作用。

有什么想法吗?

最佳答案

这不是真正的答案,但我希望对您有所帮助,我将 REDIS Sentinel 与 ServiceStack.Redis 结合使用,我没有找到使用免费 StackExchange.Redis 的解决方案来使用 Sentinel。这是我使用 ServiceStack 的代码:

    using ServiceStack.Redis;

private static IRedisClientsManager GetRedisSentinel()
{
IRedisClientsManager redisManager = null;

try
{
List<string> listSentinels = new List<string>();
listSentinels.Add(ConfigurationManager.AppSettings["RedisDB1"]);
listSentinels.Add(ConfigurationManager.AppSettings["RedisDB2"]);
listSentinels.Add(ConfigurationManager.AppSettings["RedisDB3"]);
listSentinels.Add(ConfigurationManager.AppSettings["RedisDB4"]);

RedisSentinel redisSentinel = new RedisSentinel(listSentinels, ConfigurationManager.AppSettings["RedisMaster"]);
redisSentinel.SentinelWorkerConnectTimeoutMs = 500;
redisManager = redisSentinel.Start();
}
catch (Exception ex)
{
_log.Trace(ex, "Error Redis Connection: " + ex.Message);
}

return redisManager;
}

public static object GetRedisCache<T>(string key)
{
object myObject = null;

//naming convention [PLATFORM]:[PROJECT]:[FUNCTION]:[PARAMETERS…]
string redisKey = string.Format("WEB:MyProject:{0}:{1}", typeof(T), key);


//Open Redis
IRedisClientsManager redisManager = GetRedisSentinel();
if (redisManager != null)
{
try
{
using (RedisClient redis = (RedisClient)redisManager.GetClient())
{
//Redis and Object connection
var redisGateways = redis.As<T>();

//Check if the object exists with the key
myObject = redisGateways.GetValue(redisKey);
}
}
catch (Exception ex)
{
_log.Trace(ex, "Error Get In Redis: " + ex.Message);
}
}

return myObject;
}

public static void SaveRedisCache<T>(object myObject, string key, DateTime? expireDateTime = null)
{
if (expireDateTime == null)
expireDateTime = DateTime.Now.AddMinutes(20);

//naming convention [PLATFORM]:[PROJECT]:[FUNCTION]:[PARAMETERS…]
string redisKey = string.Format("WEB:MyProject:{0}:{1}", typeof(T), key);

if (myObject != null)
{
//Open Redis
IRedisClientsManager redisManager = GetRedisSentinel();
if (redisManager != null)
{
try
{
using (RedisClient redis = (RedisClient)redisManager.GetClient())
{
//Redis and Object connection
var redisGateways = redis.As<T>();

//Store in Redis and put 20 minutes as expire
bool isExists = redisGateways.SetEntryIfNotExists(redisKey, (T)myObject);
if (isExists)
redisGateways.ExpireEntryAt(redisKey, (DateTime)expireDateTime);
else
redisGateways.SetEntry(redisKey, (T)myObject); //Update
}
}
catch (Exception ex)
{
_log.Trace(ex, "Error Save In Redis: " + ex.Message);
}
}
}
}

public static void EraseRedisCache<T>(string key)
{
//naming convention [PLATFORM]:[PROJECT]:[FUNCTION]:[PARAMETERS…]
string redisKey = string.Format("WEB:MyProject:{0}:{1}", typeof(T), key);

//Open Redis
IRedisClientsManager redisManager = GetRedisSentinel();
if (redisManager != null)
{
try
{
using (RedisClient redis = (RedisClient)redisManager.GetClient())
{
//Redis and Object connection
var redisGateways = redis.As<T>();

redisGateways.RemoveEntry(redisKey);
}
}
catch (Exception ex)
{
_log.Trace(ex, "Error Remove In Redis: " + ex.Message);
}
}
}

关于c# - 使用 StackExchange.Redis ConnectionMultiplexer 而不是 BookSleeve ConnectionUtils 连接到 Redis sentinel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23503424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com