0

我的 Global.asax 上有以下代码。它正常产生电子邮件警报。我的问题是我已设置每 24 小时发送一封电子邮件,但代码每 10 分钟发送一次电子邮件。我已经设定 :

HttpContext.Current.Cache.Add(CacheItemKey, "Test", null,
               DateTime.MaxValue, TimeSpan.FromHours(24)

但是应用程序每 10 分钟发送一次电子邮件。我也试过分钟。

我的代码:

private const string CacheItemKey = "CacheFromMe";

        public void CacheIRemovedCallback(string key,
            object value, CacheItemRemoved reason)
        {


           HitmyPage();

            // Do the service works

           DosomeWork();
        }
        private const string myPageUrl = "myurl.aspx";

        private void HitmyPage()
        {

            WebClient myclient = new WebClient();
            myclient.DownloadData(myPageUrl);
        }
        protected void Application_BegintheRequests(Object sender, EventArgs e)
        {
            // If the dummy page is hit, then it means we want to add another item

           // in cache

           if (HttpContext.Current.Request.Url.ToString() == myPageUrl)
           {
              // Add the item in cache and when succesful, do the work.

                RegistermyCacheEntries();
           }
        }
        private void DosomeWork()
        {

            DoEmailStuff();
           AnotherEmailStuff();

        }
        private void DoEmailStuff()
        {

        //    Statment For Sending The Email under conditions



        }
        private void AnotherEmailStuff()
        {
        //    Another Statment for Sending Email

        }
 void Application_Start(object sender, EventArgs e)
        {
            RegistermyCacheEntries();

        }
        private bool RegistermyCacheEntries()
        {
           if (null != HttpContext.Current.Cache[CacheItemKey]) return false;

           HttpContext.Current.Cache.Add(CacheItemKey, "Test", null,
               DateTime.MaxValue, TimeSpan.FromHours(24),
               CacheItemPriority.Normal,
                new CacheIRemovedCallback(CacheIRemovedCallback));

           return true;
        } 
4

1 回答 1

1

尝试这个:

HttpContext.Current.Cache.Add(CacheItemKey, "Test", null,
              DateTime.Now.AddHours(24), System.Web.Caching.Cache.NoSlidingExpiration,
              CacheItemPriority.Normal,
              new CacheIRemovedCallback(CacheIRemovedCallback));

MSDN 中的语法:

public Object Add(
    string key,
    Object value,
    CacheDependency dependencies,
    DateTime absoluteExpiration,
    TimeSpan slidingExpiration,
    CacheItemPriority priority,
    CacheItemRemovedCallback onRemoveCallback
)

项目保证在缓存中保留至少 10 分钟,这是默认设置。所以每 10 分钟发送一次邮件意味着设置过期时间不成功。您可以使用 absoluteExpiration 和 slipExpiration 参数更改它。然而,我怀疑这个问题出在其他地方。尝试使用 Insert 而不是 Add 方法,如此处所示

于 2013-08-27T18:24:55.477 回答