与同步请求,并仅在异步方法异步请求的响应速度慢的Redis缓存越来越超时 [英] Redis Cache getting timeout with sync requests and slow response with async requests only in async method

查看:2069
本文介绍了与同步请求,并仅在异步方法异步请求的响应速度慢的Redis缓存越来越超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我使用的Azure Redis的缓存服务和StackExchange.Redis(1.0.371)客户端与我的MVC 5和网页API应用2第一。我变得非常有趣的行为。掌握同步请求和响应速度慢超时当我将我的同步与异步调用。让我给你举个例子。这里是我的RedisCacheService,

First of all I am using Azure Redis Cache service and StackExchange.Redis(1.0.371) client with my MVC 5 and Web Api 2 app. I am getting very interesting behaviors. getting timeout with sync requests and slow response when I convert my sync calling with async. Let me give you an example. Here is my 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;
    }

    ......................................................................
}

和这里是我的方法,该方法调用缓存中。

and here is my method which invokes the cache.

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;
}

以上方法总是给我,

The above method always gives me,

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秒,如果缓存可用。

This above method works but it takes at least 5-10 seconds. I mean 10 seconds if no cache is available and 5 seconds if cache is available.

现在可以让我的方法完全同步,

Now lets make my method completely sync,

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客户端。

Please note the comment to call a repository method. The above method work instantly, means I get result within less than 1 seconds. Clearly something wrong with Azure or StackExcahge.Redis client.

更新:我的最后一种方法(异步)也在努力像一个魅力(快速和无差错)

Update: My last approach(async) is also working like a charm(fast and no error),

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;
}

请注意,我仍然有评论资源库code。

Note that still I have commented the repository code.

推荐答案

看起来像Azure的这些超时可能是一个的悬而未决的问题 ..你试过对一个本地(非天青)服务器这个code?你得到同样的结果?

Looks like these timeouts in Azure might be an open issue.. Have you tried this code against a local (non-Azure) server? Do you get the same results?

这篇关于与同步请求,并仅在异步方法异步请求的响应速度慢的Redis缓存越来越超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆