c#-C#缓存绝对过期时间失效

问题描述

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-08-31 23:59:40

c#-C#缓存绝对过期时间失效的相关文章

nginx设置静态页面压缩缓存和过期时间设置

可以通过nginx对服务器上的静态资源进行过期时间设置和对资源进行压缩传输来减少服务器的带宽开销. 以下是nginx对静态资源过期时间的设置方法:  代码如下 复制代码         location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3) {             root    /var/www/opkeep;             expires 30d;         } 上面的配置可以对ico,gif,bmp,jpg,jpeg

[ASP.NET] 如果将缓存“滑动过期时间”设置为1秒会怎样?

今天编写了一个采用ASP.NET Caching的组件,在为它编写Unit Test的过程中发现了一个有趣的问题,接下来我通过一个简单的实例说明这个问题.我们在一个控制台应用中编写了如下一段程序,这个段程序很简单:我们通过HttpRuntime的静态属性Cache得到表示当前缓存的Cache对象,并调用其Insert方法对当前的时间实施缓存.需要注意的是,我们采用"滑动时间"过期策略,并将这个滑动时间设置为1秒. 1: class Program 2: { 3: static void

|zyciis| 如何查看Context.Cache.Insert添加的缓存的过期时间呢? 谢谢

问题描述 this.Context.Cache.Insert("CacheTest","CacheContent",null,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromSeconds(6000));如上面添加了一个6000秒未访问明清除这个缓存的代码那我现在想查看这个CacheTest还有多久过期,要怎么办呢谢谢 解决方案 解决方案二:凉拌解决方案三:该回复于2009-05-01 10:06

Membercache缓存过期时间不同导致set成功,get失败怪异问题

问题描述 最近项目中用到缓存,过期时间不同遇到set成功,get失败问题,请高手解答,小弟感激不尽,下面为过期时间不同所做的4个测试用例,key都是MD5加密的测试1:stringemailStep1TimeKey1=Common.GetMd5Hash(account.UserId+email+"SafeEmailStep1");codeCache.Delete(emailStep1TimeKey1);DateTimedt1=DateTime.Now.AddSeconds(350);b

Nginx设置静态页面压缩和缓存过期时间的方法_nginx

使用nginx服务器的朋友可能都知道需要设置html静态页面缓存与页面压缩与过期时间的设置了,下面我来给各位同学介绍一下配置方法,包括对ico,gif,bmp,jpg,jpeg,swf,js,css,mp3文件进行本地缓存. 可以通过nginx对服务器上的静态资源进行过期时间设置和对资源进行压缩传输来减少服务器的带宽开销. 以下是nginx对静态资源过期时间的设置方法: location ~* .(ico|gif|bmp|jpg|jpeg|png|swf|js|css|mp3) { root /

memcached-关于Memcached缓存过期时间设置的问题

问题描述 关于Memcached缓存过期时间设置的问题 //不带过期时间的缓存 成功 //bool success = client.Store(StoreMode.Add person.UserName person); //带过期时间的缓存 失败//bool success = client.Store(StoreMode.Add person.UserName person DateTime.Now.AddMinutes(10)); Memcached添加了过期时间后换乘失败!求解! 追问

redis 一二事 - 设置过期时间,以文件夹形式展示key显示缓存数据

在使用redis时,有时回存在大量数据的时候,而且分类相同,ID相同 可以使用hset来设置,这样有一个大类和一个小分类和一个value组成 但是hset不能设置过期时间 过期时间只能在set上设置 1 // 向redis中添加缓存 2 jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_KEY, JsonUtils.objectToJson(item)); 3 // 设置key的过期时间 4 jed

HttpContext.Current.Cache 过期时间

原文:HttpContext.Current.Cache 过期时间 为了更快的读取数据,我们一般会把常用到的数据加载到Cache中 在.NET中,Cache的存在可以依赖多中方式,主要用到HttpContext.Current.Cache类 在这里,我主要写几种依赖方式 1:不依赖任何条件 HttpContext.Current.Cache.Insert(string cacheName,object obj) 理论上是Cache会永久保存,但是当服务器重新启动,内存紧张的时候也会丢失. 2:H

让CodeIgniter数据库缓存自动过期的处理的方法_php实例

CodeIgniter框架是一个非常小巧的PHP框架.CI自带数据库文件缓存,但按官方的说法,缓存设置后永不过期,除非你调用方法主动删除. Cache files DO NOT expire. Any queries that have been cached will remain cached until you delete them. 感觉太弱智了,非常不方便. 修改一下db类,在开启缓存时设置一个过期时间,到期自动缓存自动失效. 1:CI database/DB_dirver.php