使用BookSleeve维护打开的Redis连接

Ofe*_*lig 26 c# redis booksleeve

有没有人有通过BookSleeve库获取Redis的可靠模式?

我的意思是:

BookSleeve的作者@MarcGravell 建议不要每次都打开和关闭连接,而是在整个应用程序中保持一个连接.但是你怎么能处理网络中断?即,连接可能首先成功打开,但是当某些代码尝试读取/写入Redis时,连接可能已经丢失,您必须重新打开它(如果它不能打开,则会优雅地失败 - 但是这取决于您的设计需求.)

我寻找覆盖一般Redis连接开放的代码片段,以及在每次读/写之前使用的一般"活动"检查(+可选唤醒,如果不活动).

这个问题表明对这个问题有一个很好的态度,但它只是部分(例如,它没有恢复丢失的连接),并且该问题的接受答案采用了正确的方式,但没有展示具体的代码.

我希望这个主题得到可靠的答案,并最终成为一个关于BookSleeve在.Net应用程序中使用的Wiki.

-----------------------------

重要更新(2014年3月21日):

-----------------------------

马克Gravell(@MarcGravell)/堆栈交易所已经最近发布StackExchange.Redis库,最终取代Booksleeve.除了别的以外,这个新库在内部处理重新连接并使我的问题变得多余(也就是说,它对于Booksleeve来说并不是多余的,也不是我的答案,但我想最好的方法是开始使用新的StackExchange.Redis库).

Ofe*_*lig 29

由于我没有得到任何好的答案,我想出了这个解决方案(BTW感谢@Simon和@Alex的答案!).

我想与所有社区分享,作为参考.当然,任何更正都将受到高度赞赏.

using System;
using System.Net.Sockets;
using BookSleeve;

namespace Redis
{
    public sealed class RedisConnectionGateway
    {
        private const string RedisConnectionFailed = "Redis connection failed.";
        private RedisConnection _connection;
        private static volatile RedisConnectionGateway _instance;

        private static object syncLock = new object();
        private static object syncConnectionLock = new object();

        public static RedisConnectionGateway Current
        {
            get
            {
                if (_instance == null)
                {
                    lock (syncLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new RedisConnectionGateway();
                        }
                    }
                }

                return _instance;
            }
        }

        private RedisConnectionGateway()
        {
            _connection = getNewConnection();
        }

        private static RedisConnection getNewConnection()
        {
            return new RedisConnection("127.0.0.1" /* change with config value of course */, syncTimeout: 5000, ioTimeout: 5000);
        }

        public RedisConnection GetConnection()
        {
            lock (syncConnectionLock)
            {
                if (_connection == null)
                    _connection = getNewConnection();

                if (_connection.State == RedisConnectionBase.ConnectionState.Opening)
                    return _connection;

                if (_connection.State == RedisConnectionBase.ConnectionState.Closing || _connection.State == RedisConnectionBase.ConnectionState.Closed)
                {
                    try
                    {
                        _connection = getNewConnection();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(RedisConnectionFailed, ex);
                    }
                }

                if (_connection.State == RedisConnectionBase.ConnectionState.Shiny)
                {
                    try
                    {
                        var openAsync = _connection.Open();
                        _connection.Wait(openAsync);
                    }
                    catch (SocketException ex)
                    {
                        throw new Exception(RedisConnectionFailed, ex);
                    }
                }

                return _connection;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)