无法在C#中使用StackExchange redis连接Twemproxy

66bbxpm5  于 8个月前  发布在  Redis
关注(0)|答案(1)|浏览(74)

我试图使用TWEMPROXY服务器的IP地址与StackExchange redis执行下面的C#代码,它给出了下面的错误:
在StackExchange.Redis.dll中发生类型为“StackExchange. Redis.RedisConnectionException”的未处理异常
其他信息:无法连接到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 = "[email protected]", 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 ();
        }
    }
}

对于上面相同的代码,当我用TWEMPROXY IP地址替换localhost时,它会出错。

rkttyhzu

rkttyhzu1#

https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Configuration.md#twemproxy
表明这样的方法可能有用也许

var options = new ConfigurationOptions
{
    EndPoints = { "your_endpoint:port" },
    Proxy = Proxy.Twemproxy
};

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(options);

相关问题