微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Redis缓存在同步请求中获得超时,并且仅在异步方法中使用异步请求缓慢响应

首先,我使用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;
}

请注意,我仍然评论了存储库代码.

解决方法:

看起来Azure中的这些超时可能是open issue ..您是否尝试过针对本地(非Azure)服务器的此代码?你得到相同的结果吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐