清除缓存并在特定时间添加缓存

时间:2012-11-27 08:14:05

标签: c# asp.net caching

我有一个在午夜运行的过程,它会发送生日通知邮件。这只需要每天运行一次,这是我在Global.asax文件中的代码

private const string DummyPageUrl = "http://localhost:4865/automatic/dummy.aspx";
private const string DummyCacheItemKey = "dummmmyyy";
void Application_Start(object sender, EventArgs e)
{
    RegisterCacheEntry();

}
private void RegisterCacheEntry()
{
    if (null != HttpContext.Current.Cache[DummyCacheItemKey]) return;
    HttpContext.Current.Cache.Add(DummyCacheItemKey, "Test", null,       Cache.NoAbsoluteExpiration,
       TimeSpan.FromHours(1), CacheItemPriority.NotRemovable,
        new CacheItemRemovedCallback(CacheItemRemovedCallback));
}
public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason)
{
    DoWork();
    HitPage();
}
private void HitPage()
{
    System.Net.WebClient client = new System.Net.WebClient();
    client.DownloadData(DummyPageUrl);
}
private void DoWork()
 {

}
protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (HttpContext.Current.Request.Url.ToString() == DummyPageUrl)
    {
        RegisterCacheEntry();
    }
}

0 个答案:

没有答案