首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用StackExchange.Redis ConnectionMultiplexer而不是BookSleeve ConnectionUtils连接到Redis sentinel

使用StackExchange.Redis ConnectionMultiplexer而不是BookSleeve ConnectionUtils连接到Redis sentinel
EN

Stack Overflow用户
提问于 2014-05-07 03:33:56
回答 1查看 1.9K关注 0票数 2

如何使用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");

但它不起作用。

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2015-10-27 20:38:35

这不是一个真正的答案,但我希望能对你有所帮助,我使用的是REDIS Sentinel和ServiceStack.Redis,我没有找到使用免费StackExchange.Redis的解决方案。这是我用ServiceStack编写的代码:

代码语言:javascript
复制
    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);
            }
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23503424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档