问题描述
- C#缓存绝对过期时间失效
-
我的代码如下 求高手哇哇
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class index : System.Web.UI.Page
{
protected string strError = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
strFileError("刷新时间:" + DateTime.Now.ToString("hh:mm:ss"), true);
CacheItemUpdateCallback onUpdate = new CacheItemUpdateCallback(UpdataCallback);
//30秒后就到期,立即移除
HttpRuntime.Cache.Insert("DD", "绝对过期测试", null, DateTime.Now.AddSeconds(5), System.Web.Caching.Cache.NoSlidingExpiration, onUpdate);
}//缓存回调函数 private static void UpdataCallback(string key, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration) { try { strFileError("调用时间:" + DateTime.Now.ToString("hh:mm:ss"), false); expensiveObject = DateTime.Now.ToString(); } catch (Exception) { expensiveObject = DateTime.Now.ToString(); } dependency = null; absoluteExpiration = DateTime.UtcNow.AddSeconds(5); slidingExpiration = Cache.NoSlidingExpiration; } public static void strFileError(string ss, bool isFirst = false) { StreamWriter sr = null; string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string fullName = filePath + "/error.text"; if(isFirst){ sr = File.CreateText(fullName); }else { sr = File.AppendText(fullName); } if(isFirst){ sr.WriteLine("开始记录:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); }else { sr.WriteLine(ss); } sr.Close(); } }
}
解决方案
这里 我发现需要过20S UpdataCallback回调函数才会被调用 请问 是什么原因导致的 是不是我代码写的有问题
解决方案二:
为什么时间有些是DateTime.Now,有些是DateTime.UtcNow?
时间: 2024-10-31 03:55:44