0

TWEMPROXY サーバーの IP アドレスを使用して StackExchange redis で以下の C# コードを実行しようとすると、以下のエラーが発生します。

タイプ 'StackExchange.Redis.RedisConnectionException' の未処理の例外が StackExchange.Redis.dll で発生しました

追加情報: redis サーバーに接続できませんでした。切断されたマルチプレクサを作成するには、AbortOnConnectFail を無効にします。PING での SocketFailure

しかし、ローカルホストを使用すると正常に動作し、データをローカルの Redis キャッシュに保存します

「localhost」を使用したコード サンプルは次のとおりです。

using System;
namespace WinRedis
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            new MainClass().testingCache();
            Console.ReadLine();
        }
        public void testingCache()
        {
            SimpleCache<User> cache = new RedisCache<User>("mycache", "localhost:6379");
            cache.Put ("user1", new User () { Name = "test", Email = "test@email.com", Password = "secured" });
            User user = cache.Get("user1");
            Console.WriteLine(user);
        }
    }
    [Serializable]
    class User{
        public string Name { set; get; }
        public string Email { set; get; }
        public string Password {set;get;}

        public override string ToString()
        {
            return "User(Name: " + Name + ", Email: " + Email + ", Password: " + Password + ")";
        }
    }
}


using System;
using StackExchange.Redis;

namespace WinRedis
{
    public class RedisCache<T> : SimpleCache<T>
    {
        private ConnectionMultiplexer redisConnection = null;
        private IDatabase redis = null;
        private string name = null;
        public RedisCache(string name = "redis-cache",
            string connectionOptions = "localhost:6379")
        {
            this.redisConnection = ConnectionMultiplexer.Connect(connectionOptions); 
            this.redis = redisConnection.GetDatabase ();
            this.name = name;

        }

        public T Get(string key)
        {
            byte[] result = this.redis.HashGet (name, key);

            if (result == null)
                return default(T);
            else
                return result.Deserialize<T>();

        }

        public void Put(string key, T value)
        {
            this.redis.HashSet (name, key, value.SerializeToByteArray() );
        }

        public void Close()
        {
            this.redisConnection.Close ();
        }
    }
}

上記の同じコードで、localhost を TWEMPROXY IP アドレスに置き換えると、エラーが発生します。

4

1 に答える 1