2013-02-04 51 views
0

我希望缓存元素中的项目应该在特定时间每天下午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或者有其他方法。

+0

计算时间差并设置absoluteexpiration – user2031802

回答

0

我发现我已经创建了一个功能,如下

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; 
} 

,做:)

0

请检查在SO这个答案。它使用ASP.NET计时器控件在一天中的特定时间引发事件。我建议你保持这个值作为配置条目。此外还有其他建议。

How to use the .NET Timer class to trigger an event at a specific time?

+0

其实我的建议使用absoluteExpiration只有 –

+0

感谢您的答案。但它会帮助我。 –

+0

是的计时器事件只是无效/刷新/重新加载/删除缓存。 –

相关问题