2 ljy274476846 ljy274476846 于 2014.12.17 10:10 提问

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();
    }
}

}

2个回答

ljy274476846
ljy274476846   2014.12.17 10:11

这里 我发现需要过20S UpdataCallback回调函数才会被调用 请问 是什么原因导致的 是不是我代码写的有问题

save4me
save4me   Ds   Rxr 2014.12.17 11:21

为什么时间有些是DateTime.Now,有些是DateTime.UtcNow?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!