ljy274476846 2014-12-17 02:10 采纳率: 0%
浏览 3303

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 2014-12-17 02:11
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器