首先,我使用Azure Redis缓存服务和StackExchange.Redis(1.0.371)客户端使用我的MVC 5和Web Api 2应用程序。我的行为非常有趣。当我使用异步转换同步调用时,使用同步请求获得超时并且响应速度慢。让我给你举个例子。这是我的RedisCacheService,
public class RedisCacheService : ICacheService
{
private readonly IDatabase _cache;
private static readonly ConnectionMultiplexer ConnectionMultiplexer;
static RedisCacheService()
{
var connection = ConfigurationManager.AppSettings["RedisConnection"];
ConnectionMultiplexer = ConnectionMultiplexer.Connect(connection);
}
public RedisCacheService(ISettings settings)
{
_cache = ConnectionMultiplexer.GetDatabase();
}
public bool Exists(string key)
{
return _cache.KeyExists(key);
}
public Task<bool> ExistsAsync(string key)
{
return _cache.KeyExistsAsync(key);
}
public void Save(string key, string value, int timeOutInMinutes)
{
var ts = TimeSpan.FromMinutes(timeOutInMinutes);
_cache.StringSet(key, value, ts);
}
public Task SaveAsync(string key, string value, int timeOutInMinutes)
{
var ts = TimeSpan.FromMinutes(timeOutInMinutes);
return _cache.StringSetAsync(key, value, ts);
}
public string Get(string key)
{
return _cache.StringGet(key);
}
public async Task<string> GetAsync(string key)
{
string result = null;
var val = await _cache.StringGetAsync(key);
if(val.HasValue)
{
result = val;
}
return result;
}
......................................................................
}
这是我调用缓存的方法。
public async Task<IList<XX>> GetXXXXX(XXXX)
{
var key = string.Format("{0}/XX{1}_{2}", XXXX, XX, XX);
var xxx = _cacheService.Get(key);
if (xxx != null)
{
return JsonConvert.DeserializeObject<IList<XX>>(xxx);
}
var x = await _repository.GetXXXXX(XXXXX);
var contents = JsonConvert.SerializeObject(x);
_cacheService.Save(key, JsonConvert.SerializeObject(x));
return x;
}
以上方法总是给我,
System.TimeoutException
Timeout performing GET XXX, inst: 0, mgr: Inactive, queue: 3, qu=2, qs=1, qc=0, wr=1/1, in=0/0
或
System.TimeoutException
Timeout performing SETEX XXX, inst: 0, mgr: Inactive, queue: 2, qu=1, qs=1, qc=0, wr=1/1, in=0/0
让我们将其更改为异步,
public async Task<IList<XX>> GetXXXXX(XXXX)
{
var key = string.Format("{0}/XX{1}_{2}", XXXX, XX, XX);
var xxx = await _cacheService.GetAsync(key);
if (xxx != null)
{
return JsonConvert.DeserializeObject<IList<XX>>(xxx);
}
var x = await _repository.GetXXXXX(XXXXX);
var contents = JsonConvert.SerializeObject(x);
await _cacheService.SaveAsync(key, JsonConvert.SerializeObject(x));
return x;
}
以上方法有效但至少需要5-10秒。如果没有可用的缓存,则表示10秒;如果缓存可用,则表示5秒。
现在让我的方法完全同步,
public async Task<IList<XX>> GetXXXXX(XXXX)
{
var key = string.Format("{0}/XX{1}_{2}", XXXX, XX, XX);
var xxx = _cacheService.Get(key);
if (xxx != null)
{
return Task.FromResult(JsonConvert.DeserializeObject<IList<XX>>(xxx));
}
//var x = await _repository.GetXXXXX(XXXXX);
var x = (IList<XX>)new List<XX>();
var contents = JsonConvert.SerializeObject(x);
_cacheService.Save(key, JsonConvert.SerializeObject(x));
return Task.FromResult(x);
}
请注意调用存储库方法的注释。上述方法立即起作用,意味着我在不到1秒的时间内得到结果。显然Azure或StackExcahge.Redis客户端有问题。
更新:我的上一个方法(异步)也像魅力一样(快速且无错误),
public async Task<IList<XX>> GetXXXXX(XXXX)
{
var key = string.Format("{0}/XX{1}_{2}", XXXX, XX, XX);
var xxx = await _cacheService.GetAsync(key);
if (xxx != null)
{
return JsonConvert.DeserializeObject<IList<XX>>(xxx);
}
//var x = await _repository.GetXXXXX(XXXXX);
var x = (IList<XX>)new List<XX>();
var contents = JsonConvert.SerializeObject(x);
await _cacheService.SaveAsync(key, JsonConvert.SerializeObject(x));
return x;
}
请注意,我仍然评论了存储库代码。
答案 0 :(得分:1)
看起来Azure中的这些超时可能是open issue ..您是否针对本地(非Azure)服务器尝试过此代码?你得到相同的结果吗?
答案 1 :(得分:0)
我通过在Redis缓存连接字符串中设置syncTimeout
属性来解决Redis缓存中的超时问题。
例如
var conn = ConnectionMultiplexer.Connect("contoso5.redis.cache.windows.net,ssl=true,password=password,syncTimeout=5000");
参考连接字符串属性https://stackexchange.github.io/StackExchange.Redis/Configuration