首页 > 解决方案 > 更新服务器缓存,即使服务器正在获取任何请求 asp.net core 2.1

问题描述

现在我正在使用每 3 分钟触发一次的 Ajax。但是,当我们将网站保持在理想模式 4-5 小时时,它的缓存会变为空。我已经验证了这些东西在我的本地开发系统上运行,但在暂存环境中没有运行。我无法得到它的原因。这是我的代码:

$(document).ready(function () {
RefreshBhTokens();
function RefreshBhTokens() {
    $.ajax({
        type: "GET",
        url: "/home/refreshToken",
        complete: function (response) {
            setTimeout(function () { RefreshBhTokens(); }, 200000);
        },
        failure: function (response) {
            alert(response);
        }
    });
 }
});

控制器代码:

[HttpGet]
    public IActionResult refreshToken()
    {
        CommonFunctions.CacheHandling objCache1 = new CommonFunctions.CacheHandling(_cache);
        dynamic test = objCache1.GenerateBhRestToken("");
        return Json(null);
    }

通用函数文件:

public dynamic GenerateBhRestToken(string code)
    {
        dynamic AccessCode = AccessCodeOrRefreshCode(code);
        if (AccessCode != null)
        {
            JObject cacheEntry;
            if (!_cache.TryGetValue("BhRestToken", out cacheEntry))
            {
                // Key not in cache, so get data.
                cacheEntry = GenerateAccessTokens.BhRestToken(AccessCode["access_token"].Value);
                // Save data in cache.
                _cache.Set("BhRestToken", cacheEntry, cacheEntryOptions);
                _cache.Set("BhMemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
            }
            else
            {
                if (DateTime.Now > Convert.ToDateTime(GetMemoryCacheKeyValue("BhMemCahceExpiry")))
                {
                    cacheEntry = GenerateAccessTokens.BhRestToken(AccessCode["access_token"].Value);

                    // Save data in cache.
                    _cache.Set("BhRestToken", cacheEntry, cacheEntryOptions);
                    _cache.Set("BhMemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
                }
                else
                {
                    cacheEntry = (JObject)GetMemoryCacheKeyValue("BhRestToken");

                    if (cacheEntry == null)
                    {
                        cacheEntry = GenerateAccessTokens.BhRestToken(AccessCode["access_token"].Value);

                        // Save data in cache.
                        _cache.Set("BhRestToken", cacheEntry, cacheEntryOptions);
                        _cache.Set("BhMemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
                    }
                    else
                    {
                        cacheEntry = (JObject)GetMemoryCacheKeyValue("BhRestToken");
                    }

                }

            }
            return cacheEntry["BhRestToken"].Value<string>();
        }
        else { return null; }
    }

并生成 Access_code:

public dynamic AccessCodeOrRefreshCode(string code)
    {
        JObject cacheEntry;

        if (!_cache.TryGetValue("Access_Code", out cacheEntry))
        {
            // Key not in cache, so get data.
            cacheEntry = GenerateAccessTokens.GenerateAccessTokenOrRefreshToken(true, code);

            // Save data in cache.
            _cache.Set("Access_Code", cacheEntry, cacheEntryOptions);
            _cache.Set("MemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
        }
        else
        {

            if (DateTime.Now > Convert.ToDateTime(GetMemoryCacheKeyValue("MemCahceExpiry")))
            {
                string refresh_token = cacheEntry["refresh_token"].ToString();
                cacheEntry = GenerateAccessTokens.GenerateAccessTokenOrRefreshToken(false, refresh_token);
                _cache.Set("Access_Code", cacheEntry, cacheEntryOptions);
                _cache.Set("MemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
            }
            else
            {
                if (cacheEntry == null)
                {
                    cacheEntry = GenerateAccessTokens.GenerateAccessTokenOrRefreshToken(true, code);

                    // Save data in cache.
                    _cache.Set("Access_Code", cacheEntry, cacheEntryOptions);
                    _cache.Set("MemCahceExpiry", DateTime.Now.AddMinutes(5), cacheEntryOptions);
                }
                else
                {
                    cacheEntry = (JObject)GetMemoryCacheKeyValue("Access_Code");
                }
            }
        }
        return cacheEntry;
    }

我没有得到导致此问题的原因。请你帮助我好吗。

标签: c#asp.netcachingasp.net-core-2.1

解决方案


推荐阅读