asp.net下的缓存的一个实现以及.net下的HttpRuntime对象的属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
using System.Web.Caching;
namespace CCAS.BLL
{
public class CacheHelper
{
///
/// 建立缓存
///
public static object TryAddCache(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration,
TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemovedCallback)
{
if (HttpRuntime.Cache[key] == null && value != null)
return HttpRuntime.Cache.Add(key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemovedCallback);
else
return null;
}
///
/// 获取缓存
///
///
///
public static object GetCache(string key)
{
return HttpRuntime.Cache[key];
}
///
/// 移除缓存
///
public static object TryRemoveCache(string key)
{
if (HttpRuntime.Cache[key] != null)
return HttpRuntime.Cache.Remove(key);
else
return null;
}
///
/// 移除键中带某关键字的缓存
///
public static void RemoveMultiCache(string keyInclude)
{
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
if (CacheEnum.Key.ToString().IndexOf(keyInclude.ToString()) >= 0)
HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());
}
}
///
/// 移除所有缓存
///
public static void RemoveAllCache()
{
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());
}
}
}
}