当前位置:编程学习 > C#/ASP.NET >>

asp.net下Cache 缓存操作类代码

答案:
复制代码 代码如下:

using System.Collections.Generic;
using System.Web;
using System;
namespace DataAccess
{
/// <summary>
/// 缓存控制类
/// </summary>
public class CacheControl
{
public static List<string> AllUseCacheKey = new List<string>();
/// <summary>
/// 添加缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="absoluteExpiration"></param>
public static void AddCache(string key, object value, DateTime absoluteExpiration)
{
if (!AllUseCacheKey.Contains(key))
{
AllUseCacheKey.Add(key);
}
HttpContext.Current.Cache.Add(key, value, null, absoluteExpiration, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
}
/// <summary>
/// 移除缓存
/// </summary>
/// <param name="key"></param>
public static void RemoveCache(string key)
{
if (AllUseCacheKey.Contains(key))
{
AllUseCacheKey.Remove(key);
}
HttpContext.Current.Cache.Remove(key);
}
/// <summary>
/// 清空使用的缓存
/// </summary>
public static void ClearCache()
{
foreach (string value in AllUseCacheKey)
{
HttpContext.Current.Cache.Remove(value);
}
AllUseCacheKey.Clear();
}
}
}

上一个:IIS处理Asp.net请求和Asp.net页面生命周期说明
下一个:在vs2008中使用AJAX开发.net 2.0下的Web程序的方法

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,