c# - 更新服务器缓存,即使服务器正在获取任何请求 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;
}
我没有得到导致此问题的原因。请你帮助我好吗。
解决方案
推荐阅读
- python - 对于非方形图像,Deeplabv3 重新训练结果偏斜
- tensorflow - 将json文件转换为图像格式..?
- c# - 如何使用属性路由实现 X.PagedList PagedListPager?
- vue.js - 如何使用
- python - Python While 函数
- python - 在 macos 上使用 pyinstaller 编译 mediapipe 的问题
- asp.net - 如何在 ASP.Net Gridview 中动态启用/禁用 ItemTemplate
- postgresql - postgres/sequelize 中的自动增量
- javascript - 我想从文本文件中读取一定数量的行,并在使用 JavaScript 读取行后与它们一起创建一个对象
- ruby - Bundler 配置以在自定义路径中查找 gem 或从自定义源下载