sql >> データベース >  >> NoSQL >> Redis

BookSleeveを使用してオープンなRedis接続を維持する

    良い答えが得られなかったので、この解決策を思いつきました(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;
                }
            }
        }
    }
    


    1. ASP.Net MVCサイトをロックせずにセッションを操作する方法はありますか?

    2. Mongo更新配列要素(.NETドライバー2.0)

    3. SaltStackを使用したMongoDBの自動化

    4. MongoDB $ asin