码迷,mamicode.com
首页 > 其他好文 > 详细

一个对缓存操作的类DataCache

时间:2014-07-01 16:45:18      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   cti   for   io   

using System;
using System.Web;
using System.Collections;

namespace eBlog.Cache
{
/// <summary>
/// 缓存相关的操作类
/// </summary>
public class DataCache
{
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static object GetCache(string cacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[cacheKey];
}

/// <summary>
/// 是否存在这个缓存
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static bool Exists(string cacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[cacheKey] != null;
}

/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="objObject"></param>
public static void SetCache(string cacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(cacheKey, objObject);
}

/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="objObject"></param>
/// <param name="absoluteExpiration"></param>
/// <param name="slidingExpiration"></param>
public static void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration,TimeSpan slidingExpiration )
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(cacheKey, objObject,null,absoluteExpiration,slidingExpiration);

}
public static void SetCache(string cacheKey, object objObject, int minutes)
{
SetCache(cacheKey, objObject, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero);
}

public static void RemoveCache(string cacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Remove(cacheKey);
}

#region 清除所有缓存
/// <summary>
/// 有时可能需要立即更新,这里就必须手工清除一下Cache
/// Cache类有一个Remove方法,但该方法需要提供一个CacheKey,但整个网站的CacheKey我们是无法得知的
/// 只能经过遍历
/// </summary>
public static void RemoveAllCache()
{

System.Web.Caching.Cache cache = HttpRuntime.Cache;
IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
ArrayList al = new ArrayList();
while (cacheEnum.MoveNext())
{
al.Add(cacheEnum.Key);
}

foreach (string key in al)
{
cache.Remove(key);
}
}
#endregion

#region 显示所有缓存

//显示所有缓存
public static string Show()
{
string str = "";
IDictionaryEnumerator cacheEnum = HttpRuntime.Cache.GetEnumerator();

while (cacheEnum.MoveNext())
{
str += "<br />缓存名<b title=" + cacheEnum.Value.ToString() + ">[" + cacheEnum.Key + "]</b><br />";
}
return "当前总缓存数:" + HttpRuntime.Cache.Count + "<br />" + str;
}

#endregion
}
}

一个对缓存操作的类DataCache,布布扣,bubuko.com

一个对缓存操作的类DataCache

标签:blog   http   os   cti   for   io   

原文地址:http://www.cnblogs.com/zuiyuewentian/p/3817888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!