0

我缓存了一个 .net 应用程序变量。我想要做的是让缓存在每小时、1、1.30、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);
            }

我将 time_to_expire 值测试为 29 的原因是我担心边界情况,我不希望变量在半小时内“变为空白”!关于如何更好地做到这一点的想法?

谢谢

4

1 回答 1

0

不知道它是否“更好”,但我倾向于做这样的事情:

        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);
于 2012-12-07T19:57:32.597 回答