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

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

问题描述

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

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

我的意思是:

BookSleeve 的作者 @MarcGravell 推荐 不打开 &每次都关闭连接,而是在整个应用程序中保持一个连接.但是你如何处理网络中断呢?即连接可能首先成功打开,但是当某些代码尝试读取/写入 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.)

我寻找涵盖一般 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.

这个问题表明对问题,但这只是部分问题(例如,它无法恢复丢失的连接),并且接受的答案问题以正确的方式绘制,但没有展示具体的代码.

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 应用程序中使用的 Wiki.

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):

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

Marc Gravell (@MarcGravell)/Stack Exchange 有 最近发布 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!).

我想与所有社区分享它作为参考.当然,任何更正将不胜感激.

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天全站免登陆