asp.net html5 缓存,ASP.NET 缓存有效时间设置解决思路

C# codeusing System;

using System.Web.Caching;

using System.Web;

using System.Collections;

using System.Text.RegularExpressions;

///

/// 缓存常用操作

/// LastEditTime:2011-10-10

///

public class CacheUtil

{

public CacheUtil()

{

//...在此处添加构造函数逻辑

}

///

/// 增加一个缓存对象

///

/// 键值名称

/// 被缓存对象

/// 缓存失效时间,默认为3分钟

/// 保留优先级(枚举数值),1最不会被清除,6最容易被内存管理清除,0为default

/// 【1:NotRemovable;2:High;3:AboveNormal;4:Normal;5:BelowNormal;6:Low】

/// 缓存写入是否成功true 、 false

public static bool InsertCach(string strKey, object valueObj, int durationMin, int priority)

{

TimeSpan ts;

if (strKey != null && strKey.Length != 0 && valueObj != null)

{

//建立回调委托的一个实例

//onRemove是委托执行的函数,具体方法看下面的onRemove(...)

CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);

#region 失效时间设置

if (durationMin == 0)

{

ts = new TimeSpan(0, 3, 0);//如果不进行设置则为三分钟

}

else

{

ts = new TimeSpan(0, durationMin, 0);

}

#endregion

#region System.Web.Caching.Cache 对象中存储的项的相对优先级

CacheItemPriority cachePriority;

switch (priority)

{

case 6:

cachePriority = CacheItemPriority.Low;

break;

case 5:

cachePriority = CacheItemPriority.BelowNormal;

break;

case 4:

cachePriority = CacheItemPriority.Normal;

break;

case 3:

cachePriority = CacheItemPriority.AboveNormal;

break;

case 2:

cachePriority = CacheItemPriority.High;

break;

case 1:

cachePriority = CacheItemPriority.NotRemovable;

break;

default:

cachePriority = CacheItemPriority.Default;

break;

}

#endregion

HttpContext.Current.Cache.Insert(strKey, valueObj, null, DateTime.Now.Add(ts), System.Web.Caching.Cache.NoSlidingExpiration, cachePriority, callBack);

return true;

}

else

{

return false;

}

}

///

/// 判断缓存对象是否存在

///

/// 缓存键值名称

/// 是否存在true 、false

public static bool IsExist(string strKey)

{

return HttpContext.Current.Cache[strKey] != null;

}

///

/// 读取缓存对象

///

/// 缓存键值名称

/// 缓存对象,objec类型

public static object GetCache(string strKey)

{//取出值

if (HttpContext.Current.Cache[strKey] != null)

{

object obj = HttpContext.Current.Cache[strKey];

if (obj == null)

{

return null;

}

else

{

return obj;

}

}

else

{

return null;

}

}

///

/// 删除缓存对象

///

/// 缓存键值名称

public static void Remove(string strKey)

{

if (HttpContext.Current.Cache[strKey] != null)

{

HttpContext.Current.Cache.Remove(strKey);

}

}

///

/// 根据设置的正则表达式清除缓存对象;

/// 该方法使用正则匹配要删除的键值对象,如果键值命名统一规范,可批处理清除相关缓存数据O(∩_∩)O

///

/// 匹配键值的正则表达式

public static void RemoveByRegexp(string pattern)

{

if (pattern != "")

{

IDictionaryEnumerator enu = HttpContext.Current.Cache.GetEnumerator();

while (enu.MoveNext())

{

string key = enu.Key.ToString();

if (Regex.IsMatch(key, pattern))

{

Remove(key);

}

}

}

}

///

/// 清除所有缓存对象

///

public static void Clear()

{

IDictionaryEnumerator enu = HttpContext.Current.Cache.GetEnumerator();

while (enu.MoveNext())

{

Remove(enu.Key.ToString());

}

}

public static CacheItemRemovedReason reason;

///

/// 此方法在值失效之前调用,可以用于在失效之前更新数据库,或从数据库重新获取数据

///

///

///

///

private static void onRemove(string strKey, object obj, CacheItemRemovedReason r)

{

reason = r;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值