免责声明:我对Redis和StackExchange.Redis客户端非常陌生。
场景:我有一个使用用于Azure Redis缓存的ASP.NET会话状态提供程序的ASP.NET MVC web应用程序。
这就像一种魅力!
现在,我的控制器调用我的服务层(它只是一个DLL引用,但有一天可能会变成一个Web调用)。我正试图使用Autofac配置redis客户端,如下所示:
var redis = ConnectionMultiplexer.Connect(ConfigurationManager.AppSettings["redis.connectionstring"]);
builder.RegisterInstance(redis).AsSelf().SingleInstance();
builder.RegisterType<RedisCacheClient>().As<ICacheClient>().WithParameter("db", redis.GetDatabase());连接已成功设置,当我在redis-cli.exe上运行客户端列表时,我可以看到一个新的客户端。
我的RedisCacheClient类只是IDatabase上的一个薄包装器
public void Add(string key, object value)
{
_db.StringSet(key, Serialize(value));
}
public T Get<T>(string key)
{
byte[] buffer = _db.StringGet(key);
T o = Deserialize<T>(buffer);
return o;
}
private static byte[] Serialize(object o)
{
if (o == null)
{
throw new ArgumentNullException("Cannot cache null values.");
}
var binaryFormatter = new BinaryFormatter();
using (var memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, o);
byte[] buffer = memoryStream.ToArray();
return buffer;
}
}
private static T Deserialize<T>(byte[] stream)
{
if (stream == null)
{
return default(T);
}
var binaryFormatter = new BinaryFormatter();
using (var memoryStream = new MemoryStream(stream))
{
T result = (T)binaryFormatter.Deserialize(memoryStream);
return result;
}
}Problem:我可以成功地存储对象,但永远无法检索!无论我做什么,我都会得到连接超时(当前超时时间是30秒)。我在过去的四个小时里一直在努力想办法解决这个问题,却没有任何运气。真正的挣扎:
我已经使用过的一些资源:http://azure.microsoft.com/blog/2015/02/10/investigating-timeout-exceptions-in-stackexchange-redis-for-azure-redis-cache/
超时执行集{Key},inst: 0,mgr: Inactive,queue: 2,qu=1,qs=1,qc=0,wr=1/1,in=0/0
带有Azure的StackExchange.Redis非常慢,或者抛出超时错误。
https://github.com/StackExchange/StackExchange.Redis/issues/122
https://github.com/StackExchange/StackExchange.Redis/issues/83
P.S:我在本地或在天蓝色上都有同样的行为。
发布于 2015-07-02 13:14:20
虽然我真的很不喜欢这个问题,但我不得不说,这个问题早就过去了,我也不知道为什么。,我认为这是某种与我的设置有关的间歇性问题(在并行VM上运行MacBook Pro窗口),有时可能会被最大化,使得Redis拒绝或不处理其队列中的项。嗯,这是我唯一能想到的,因为我已经运行了几天相同的软件,没有任何问题。
https://stackoverflow.com/questions/30584721
复制相似问题