简体   繁体   English

每小时获取缓存以每半小时更新一次变量

[英]Getting a cache to update variable every half hour on the hour

I have a .net application variable cached. 我缓存了一个.net应用程序变量。 What I want to do it get the cache to recycle the variable every half hour on the hour, 1, 1.30, 2, 2.30 etc. I have the onRemoveCallback function set and everything works, my real question is what is the best way to generate the correct absoluteExpiration value? 我要执行的操作是使缓存每隔半小时,1、1.3、2、2.30等每隔半小时回收一次变量。我设置了onRemoveCallback函数,并且一切正常,我的真正问题是生成的最佳方法是什么正确的absoluteExpiration值?

DateTime time_to_expire = DateTime.Now;
            if (time_to_expire.Minute < 29)
            {
                time_to_expire = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 30, 0);
            }
            else
            {
                time_to_expire = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour + 1, 0, 0);
            }

The reason I test the time_to_expire value to 29 is that I am worried about boundary cases and I don't want the variable to 'go blank' for a half hour period! 我将time_to_expire值测试为29的原因是,我担心边界情况,并且我不希望变量在半小时内“变为空白”! Ideas on how to do this better? 关于如何更好地做到这一点的想法?

Thanks 谢谢

Don't know that it is any "better" but I would be inclined to do something like this: 不知道这是什么“更好”,但我倾向于这样做:

        DateTime time_to_expire = DateTime.Now.AddMinutes(30);
        time_to_expire = new DateTime(time_to_expire.Year, time_to_expire.Month, time_to_expire.Day, time_to_expire.Hour, time_to_expire.Minute >= 30 ? 30 : 0, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM