Disclaimer: I am quite new to Redis and StackExchange.Redis client.
Scenario: I've got an ASP.NET MVC web app that uses ASP.NET Session State Provider for Azure Redis Cache.
This works like a charm!
Now, my controllers call my services layer (it's just a DLL reference but may become a Web Api call one day). I am trying to configure my redis client with Autofac as per below:
var redis = ConnectionMultiplexer.Connect(ConfigurationManager.AppSettings["redis.connectionstring"]);
builder.RegisterInstance(redis).AsSelf().SingleInstance();
builder.RegisterType<RedisCacheClient>().As<ICacheClient>().WithParameter("db", redis.GetDatabase());
The connection is set up successfully and I can see a new client when I run client list on redis-cli.exe.
My RedisCacheClient class is just a thin wrapper on 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: I can store objects successfully but can never retrieve then! I keep getting connections timeout no matter what I do (current timeout is 30 secs). I've spent the last 4 hours trying to figure this out and no luck. Really struggling :(
Some of the resources I have already gone through: http://azure.microsoft.com/blog/2015/02/10/investigating-timeout-exceptions-in-stackexchange-redis-for-azure-redis-cache/
Timeout performing SET {Key}, inst: 0, mgr: Inactive, queue: 2, qu=1, qs=1, qc=0, wr=1/1, in=0/0
StackExchange.Redis with Azure Redis is unusably slow or throws timeout errors
Azure Redis cache - timeouts on GET calls
https://github.com/StackExchange/StackExchange.Redis/issues/122
https://github.com/StackExchange/StackExchange.Redis/issues/83
P.S: I get the very same behaviour running this locally or on azure.