缓存的调度程序任务不起作用

时间:2013-09-26 14:03:50

标签: c# asp.net-mvc caching

如何在以下代码示例中的任务中调用URL get请求。 60秒后不会调用网址http://mysite/SMS/SendText

        private static CacheItemRemovedCallback OnCacheRemove = null;

        private void AddTask(string name, int seconds)
        {
            OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
            HttpRuntime.Cache.Insert(name, seconds, null,
                DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
                CacheItemPriority.NotRemovable, OnCacheRemove);
        }

        public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
        {
            // do stuff here if it matches our taskname, like WebRequest
            // re-add our task so it recurs
            AddTask(k, Convert.ToInt32(v));
        }

        protected void Application_Start()
        {

            AddTask("http://localhost:1901/SMS/SendText", 60);
}

1 个答案:

答案 0 :(得分:0)

基于Cache.Insert Method,您必须在添加日期到期时使用UtcNow,而不是Now功能。

所以“我的猜测”是你有一个你无法直接看到的抛出错误,并且插入不起作用,因为现在60秒,将它们转换为UtcNow已经过期。