gpt4 book ai didi

c# - 数据缓存 .net 在指定时间之前过期

转载 作者:太空宇宙 更新时间:2023-11-03 20:15:30 26 4
gpt4 key购买 nike

我的应用程序中有一个数据缓存,它保存查询结果并在每个“配对小时”(即(08:00、10:00、12:00)绝对到期时过期。

所以:

 public Dictionary<int, List<Int64>> GetCacheDNAOffers()
{
if (_cache["DnaOffersPairs"] == null)
{
Dictionary<int, string> dtDNA = GetCacheActiveOffers().AsEnumerable().Select(d => new { idOferta = Convert.ToInt32(d["nof_id"]), dna = d["nof_dna"].ToString() }).ToDictionary(t => t.idOferta, t => t.dna);
Dictionary<int, List<Int64>> lstDNA = new Dictionary<int, List<Int64>>();

foreach (var dna in dtDNA)
{
lstDNA[dna.Key] = CarregarDNA(dna.Value);
}
_cache.Insert("DnaOffersPairs", lstDNA, null, SetTimeCacheExpires(), TimeSpan.Zero);
}

return (Dictionary<int, List<Int64>>)_cache["DnaOffersPairs"];
}

设置过期时间的函数:

private DateTime SetTimeCacheExpires()
{
try
{

int hour = DateTime.Now.Hour;
int minutes = DateTime.Now.Minute;
int qtdhoursToExpire = 1;

NextHours = (hour + qtdhoursToExpire);

return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, PairHour, minutes, 1);

}
catch (Exception ex)
{
throw ex;
}
}

以及检查“配对时间”的功能:

 public int PairHour
{
get
{
if (NextHours % 2 == 0)
return NextHours;
else
return NextHours + 1;
}
}

有时会出现缓存过期的问题,如果应用程序空闲了一段时间,所有浏览器窗口都关闭了。

我是否需要在 IIS 上设置一个配置,以设置缓存在预配置缓存结束之前一直保持事件状态?还是在代码上正确设置了缓存配置?

最佳答案

您应该检查这是否是由 IIS 池回收引起的。默认情况下,应用程序池会在空闲 20 分钟后回收。当回收发生时,缓存中存储的所有数据都将过期(该过程只是关闭)。 http://technet.microsoft.com/pl-pl/library/cc753179(v=ws.10).aspx

关于c# - 数据缓存 .net 在指定时间之前过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16992741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com