【发布时间】:2020-08-03 07:23:16
【问题描述】:
我对 C# 还很陌生,我正在尝试了解以下方法的作用,作为我不再使用它的同事,我创建了它。
public static void setInventoryCache(string type, string att, int id, float val)
{
string cacheKey = "inv-" + type + "-" + att + "-" + id.ToString();
debugLog("set " + cacheKey + " = " + val);
// expire cached items randomly so they don't all expire at the same time and need to be
// re-cached
int expire = random.Next(30, 90);
CacheItemPolicy cip = new CacheItemPolicy
{
RemovedCallback = new CacheEntryRemovedCallback(removeCallBack),
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(expire),
};
InventoryCache.Set(cacheKey, val, cip);
}
在我看来,缓存被告知每 30-90 分钟过期一次。它是否正确?因为在我的程序中,它似乎仍然会抓取旧的缓存数据,即使它已经超过 90 分钟了
我目前遇到的问题是我的缓存数据不等于我的数据库中的数据,它应该是。这让我觉得数据没有正确过期。为什么缓存的数据一开始会有用,为什么不只是从数据库中提取数据是正确的?
【问题讨论】:
标签: c# asp.net-mvc caching