using System;
using System.Web;
using System.Web.Caching;
namespace Common
{
/// <summary>
/// 缓存辅助类
/// </summary>
public static class CacheHelper
{
/// <summary>
/// 根据键获取缓存数据
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static object GetCache(string cacheKey)
{
var objCache = HttpRuntime.Cache;
return objCache.Get(cacheKey);
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheKey">缓存键</param>
/// <param name="objValue">缓存键</param>
public static void SetCache(string cacheKey, object objValue)
{
var cache = HttpRuntime.Cache;
cache.Insert(cacheKey, objValue);
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheKey">缓存键</param>
/// <param name="objValue">缓存的值</param>
/// <param name="timeout">过期时间</param>
public static void SetCache(string cacheKey, object objValue, TimeSpan timeout)
{
var cache = HttpRuntime.Cache;
cache.Insert(cacheKey, objValue, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheKey">缓存键</param>
/// <param name="objValue">缓存的value</param>
/// <param name="absoluteExpiration">绝对过期时间</param>
/// <param name="slidingExpiration">滑动过期时间</param>
public static void SetCache(string cacheKey, object objValue, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
var cache = HttpRuntime.Cache;
cache.Insert(cacheKey, objValue, null, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 移除指定的缓存
/// </summary>
/// <param name="cacheKey"></param>
public static void RemoveCache(string cacheKey)
{
var cache = HttpRuntime.Cache;
cache.Remove(cacheKey);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
var cache = HttpRuntime.Cache;
var cacheEnum = cache.GetEnumerator();
while (cacheEnum.MoveNext())
{
if (cacheEnum.Key != null) cache.Remove(cacheEnum.Key.ToString());
}
}
}
}
CacheHelper
最新推荐文章于 2022-12-20 10:46:31 发布