标签:
using System;
using System.Web;
using System.Web.Caching;
using System.Collections.Generic;
using System.Collections;
namespace SZRPP.Common
{
/// <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>
/// 设置当前应用程序指定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;
if (objObject!=null)
objCache.Insert(CacheKey, objObject, null, DateTime.UtcNow.AddDays(7), TimeSpan.Zero);
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></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);
}
/// <summary>
/// 根据依赖项设置缓存
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
/// <param name="cdep"></param>
/// <param name="absoluteExpiration"></param>
/// <param name="slidingExpiration"></param>
public static void SetCache(string CacheKey, object objObject, CacheDependency cdep, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, cdep, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 移除当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
public static void RemoveCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Key.ToString().IndexOf(CacheKey, System.StringComparison.Ordinal) >= 0)
objCache.Remove(enumerator.Key.ToString());
}
}
/// <summary>
/// 移除当前应用程序所有缓存
/// </summary>
public static void RemoveCache()
{
// retrieve application Cache enumerator
IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
// copy all keys that currently exist in Cache
while (enumerator.MoveNext())
{
HttpRuntime.Cache.Remove(enumerator.Key.ToString());
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/zxq311915/p/4280851.html