自写缓存类
public class MyCache
{
/// <summary>
/// 插入一个缓存
/// </summary>
/// <param name="name">缓存键</param>
/// <param name="value">缓存值</param>
/// <param name="isAbsoulute">是否使用绝对过期,是则为true否则为false</param>
/// <param name="isOverride">如果缓存存在,是否覆盖缓存值</param>
/// <param name="absoluteDate">绝对过期时间</param>
/// <param name="spacing">相对过期的时间间隔</param>
/// <param name="dep">缓存依赖</param>
/// <param name="removeddCallback">缓存被移除时调用的委托</param>
/// <param name="priority">缓存优先级</param>
public static void Insert(string name, object value, bool isAbsoulute, bool isOverride, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback removeddCallback, CacheItemPriority priority)
{
Cache cache = HttpContext.Current.Cache;
//如果此键的缓存已存在
if (cache[name] != null)
{
//如果覆盖缓存值
if (isOverride)
{
cache.Remove(name);
InsertInternal(name, value, isAbsoulute, absoluteDate, spacing, dep, removeddCallback, priority);
}
else
{
return;
}
}
InsertInternal(name, value, isAbsoulute, absoluteDate, spacing, dep, removeddCallback, priority);
}
/// <summary>
/// 根据键名,获取缓存,如果缓存不存在,则调用返回缓存值的委托
/// </summary>
public static object GetCache(string name, Func<object> func, bool isAbsoulute, bool isOverride, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback removeddCallback, CacheItemPriority priority)
{
Cache cache = HttpContext.Current.Cache;
if (cache[name] != null)
{
return cache[name];
}
else
{
object obj = func();
Insert(name, obj, isAbsoulute, isOverride, absoluteDate, spacing, dep, removeddCallback, priority);
return obj;
}
}
/// <summary>
/// 内部方法
/// </summary>
static void InsertInternal(string name, object value, bool isAbsoulute, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback callback, CacheItemPriority priority)
{
Cache cache = HttpContext.Current.Cache;
if (isAbsoulute)
{
cache.Insert(name, value, dep, absoluteDate, TimeSpan.Zero, priority, callback);
}
else
{
cache.Insert(name, value, dep, DateTime.MaxValue, spacing, priority, callback);
}
}
}
{
/// <summary>
/// 插入一个缓存
/// </summary>
/// <param name="name">缓存键</param>
/// <param name="value">缓存值</param>
/// <param name="isAbsoulute">是否使用绝对过期,是则为true否则为false</param>
/// <param name="isOverride">如果缓存存在,是否覆盖缓存值</param>
/// <param name="absoluteDate">绝对过期时间</param>
/// <param name="spacing">相对过期的时间间隔</param>
/// <param name="dep">缓存依赖</param>
/// <param name="removeddCallback">缓存被移除时调用的委托</param>
/// <param name="priority">缓存优先级</param>
public static void Insert(string name, object value, bool isAbsoulute, bool isOverride, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback removeddCallback, CacheItemPriority priority)
{
Cache cache = HttpContext.Current.Cache;
//如果此键的缓存已存在
if (cache[name] != null)
{
//如果覆盖缓存值
if (isOverride)
{
cache.Remove(name);
InsertInternal(name, value, isAbsoulute, absoluteDate, spacing, dep, removeddCallback, priority);
}
else
{
return;
}
}
InsertInternal(name, value, isAbsoulute, absoluteDate, spacing, dep, removeddCallback, priority);
}
/// <summary>
/// 根据键名,获取缓存,如果缓存不存在,则调用返回缓存值的委托
/// </summary>
public static object GetCache(string name, Func<object> func, bool isAbsoulute, bool isOverride, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback removeddCallback, CacheItemPriority priority)
{
Cache cache = HttpContext.Current.Cache;
if (cache[name] != null)
{
return cache[name];
}
else
{
object obj = func();
Insert(name, obj, isAbsoulute, isOverride, absoluteDate, spacing, dep, removeddCallback, priority);
return obj;
}
}
/// <summary>
/// 内部方法
/// </summary>
static void InsertInternal(string name, object value, bool isAbsoulute, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback callback, CacheItemPriority priority)
{
Cache cache = HttpContext.Current.Cache;
if (isAbsoulute)
{
cache.Insert(name, value, dep, absoluteDate, TimeSpan.Zero, priority, callback);
}
else
{
cache.Insert(name, value, dep, DateTime.MaxValue, spacing, priority, callback);
}
}
}