维护使用BookSleeve开放Redis的连接 [英] Maintaining an open Redis connection using BookSleeve

查看:615
本文介绍了维护使用BookSleeve开放Redis的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有固定模式获取的Redis通过 BookSleeve 库?

Does anyone have a solid pattern fetching Redis via BookSleeve library?

我的意思是:

BookSleeve的作者@MarcGravell <一个href=\"http://stackoverflow.com/questions/6477623/how-should-i-be-using-booksleeve-with-protobuf-net/6478883#comment10725570_6478883\">recommends不开&安培;关闭连接每一次,而是保持整个应用程序的一个连接。但你怎么能处理网络中断?即连接可能会成功地在第一时间被打开,但是当一些code尝试读取/写入Redis的,还有就是连接已经下降的可能性,必须重新打开它(并失败正常,如果它不会打开 - 但最多也就是您的设计需求)

BookSleeve's author @MarcGravell recommends not to open & close the connection every time, but rather maintain one connection throughout the app. But how can you handle network breaks? i.e. the connection might be opened successfully in the first place, but when some code tries to read/write to Redis, there is the possibility that the connection has dropped and you must reopen it (and fail gracefully if it won't open - but that is up to your design needs.)

我寻求code段(县),涵盖了一般Redis的连接孔,和一般的'活'检查(+可选清醒,如果不是活的),将每个读/写之前使用。

I seek for code snippet(s) that cover general Redis connection opening, and a general 'alive' check (+ optional awake if not alive) that would be used before each read/write.

<一个href=\"http://stackoverflow.com/questions/7543751/how-often-should-i-open-close-my-booksleeve-connection\">This问题提出一个很好的态度的问题,但它只是部分(不恢复丢失的连接,例如),和的接受的答案这个问题得出正确的方式,但并不能说明具体code。

This question suggests a nice attitude to the problem, but it's only partial (it does not recover a lost connection, for example), and the accepted answer to that question draws the right way but does not demonstrate concrete code.

我希望这个线程将得到坚实的答案,并最终成为一种维基的问候BookSleeve在.Net应用程序使用。

I hope this thread will get solid answers and eventually become a sort of a Wiki with regards to BookSleeve use in .Net applications.

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

重要更新(21/3/2014):

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

马克Gravell(@MarcGravell)/堆叠交换具有的最近发布的 StackExchange.Redis 库,最终取代Booksleeve。这种新的图书馆,除其他事项外,在内部处理重新连接,并呈现我的问题冗余(也就是说,它不是多余的Booksleeve也不是我的回答如下,但我想最好的方式向前发展是开始使用新的StackExchange.Redis库)。

Marc Gravell (@MarcGravell) / Stack Exchange have recently released the StackExchange.Redis library that ultimately replaces Booksleeve. This new library, among other things, internally handles reconnections and renders my question redundant (that is, it's not redundant for Booksleeve nor my answer below, but I guess the best way going forward is to start using the new StackExchange.Redis library).

推荐答案

由于我没有得到任何好的答案,我想出了这个解决方案(顺便说一句感谢@Simon和@Alex您的回答!)。

Since I haven't got any good answers, I came up with this solution (BTW thanks @Simon and @Alex for your answers!).

我想与所有社会作为一个参考的共享。当然,任何修正将是非常美联社preciated。

I want to share it with all of the community as a reference. Of course, any corrections will be highly appreciated.

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;
            }
        }
    }
}

这篇关于维护使用BookSleeve开放Redis的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆