[英]How to deleted cache items at a particular time daily
我希望每天在特定时间(例如晚上11:59:59)删除一次缓存元素中的项目。
我知道缓存中有一个属性absoluteExpiration
,可以在特定时间段内使用。
我正在使用以下代码在缓存中设置值
public static Collection<CProductMakesProps> GetCachedSmartPhoneMake(HttpContext context)
{
var allMake = context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>;
if (allMake == null)
{
allMake = new CModelRestrictionLogic().GetTopMakes();
context.Cache.Insert("SmartPhoneMake", allMake, null,
DateTime.Now.AddHours(Int32.Parse(ConfigurationManager.AppSettings["MakeCacheTime"])),
Cache.NoSlidingExpiration);
}
return allMake;
}
但是我如何设置缓存应该过期的确切时间。
我需要manipulate
时间变量并计算time difference
并设置absoluteExpiration
还是有其他方法。
请在SO中检查此答案。 它使用ASP.NET计时器控件在一天的特定时间引发事件。 我建议您将此值保留为配置条目。 也有其他建议。
我发现我创建函数的方式如下
private static double GetTimeLeft()
{
//create a time stamp for tomorow 00:10 hours
var tomorrow0010Minute = DateTime.Now.AddDays(1).Date.AddMinutes(10);
return Math.Round((tomorrow0010Minute - DateTime.Now).TotalHours);
}
这给了我一个双重价值,我在我的函数中使用如下
public static Collection<CProductMakesProps> GetCachedSmartPhoneMake(HttpContext context)
{
var allMake = context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>;
if (allMake == null)
{
allMake = new CModelRestrictionLogic().GetTopMakes();
context.Cache.Insert("SmartPhoneMake", allMake, null,
DateTime.Now.AddHours(GetTimeLeft()),
Cache.NoSlidingExpiration);
}
return allMake;
}
并做了 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.