using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
namespace CY.Infrastructure.Cache
{
///
/// 默认缓存管理类(使用本地IIS缓存)
///
public class DefaultCacheStrategy : ICacheStrategy
{
private static readonly DefaultCacheStrategy instance = new DefaultCacheStrategy();
protected static volatile System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache;
protected int _timeOut = 1440; // 默认缓存存活期为1440分钟(24小时)
private static object syncObj = new object();
///
/// 构造函数
///
static DefaultCacheStrategy()
{
//lock (syncObj)
//{
// //System.Web.HttpContext context = System.Web.HttpContext.Current;
// //if(context != null)
// // webCache = context.Cache;
// //else
// webCache = System.Web.HttpRuntime.Cache;
//}
}
///
/// 设置到期相对时间[单位:/分钟]
///
public int TimeOut
{
set { _timeOut = value > 0 ? value : 6000; }
get { return _timeOut > 0 ? _timeOut : 6000; }
}
public static System.Web.Caching.Cache GetWebCacheObj
{
get { return webCache; }
}
///
/// 加入当前对象到缓存中
///
/// 对象的键值
/// 缓存的对象
public void AddObject(string objId, object o)
{
if (objId == null || objId.Length == 0 || o == null)
{
return;
}
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
if (TimeOut == 6000)
{
webCache.Insert(objId, o, null, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, callBack);
}
else
{
webCache.Insert(objId, o, null, DateTime.Now.AddMinutes(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
}
}
///
/// 加入当前对象到缓存中
///
/// 对象的键值
/// 缓存的对象
public void AddObjectWith(string objId, object o)
{
if (objId == null || objId.Length == 0 || o == null)
{
return;
}
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
webCache.Insert(objId, o, null, System.DateTime.Now.AddHours(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
}
///
/// 加入当前对象到缓存中,并对相关文件建立依赖
///
/// 对象的键值
/// 缓存的对象
/// 监视的路径文件
public void AddObjectWithFileChange(string objId, object o, string[] files)
{
if (objId == null || objId.Length == 0 || o == null)
{
return;
}
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
CacheDependency dep = new CacheDependency(files, DateTime.Now);
webCache.Insert(objId, o, dep, System.DateTime.Now.AddHours(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
}
///
/// 加入当前对象到缓存中,并使用依赖键
///
/// 对象的键值
/// 缓存的对象
/// 依赖关联的键值
public void AddObjectWithDepend(string objId, object o, string[] dependKey)
{
if (objId == null || objId.Length == 0 || o == null)
{
return;
}
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
CacheDependency dep = new CacheDependency(null, dependKey, DateTime.Now);
webCache.Insert(objId, o, dep, System.DateTime.Now.AddMinutes(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
}
///
/// 建立回调委托的一个实例
///
///
///
///
public void onRemove(string key, object val, CacheItemRemovedReason reason)
{
switch (reason)
{
case CacheItemRemovedReason.DependencyChanged:
break;
case CacheItemRemovedReason.Expired:
{
//CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(this.onRemove);
//webCache.Insert(key, val, null, System.DateTime.Now.AddMinutes(TimeOut),
// System.Web.Caching.Cache.NoSlidingExpiration,
// System.Web.Caching.CacheItemPriority.High,
// callBack);
break;
}
case CacheItemRemovedReason.Removed:
{
break;
}
case CacheItemRemovedReason.Underused:
{
break;
}
default: break;
}
//如需要使用缓存日志,则需要使用下面代码
//myLogVisitor.WriteLog(this,key,val,reason);
}
///
/// 删除缓存对象
///
/// 对象的关键字
public void RemoveObject(string objId)
{
//objectTable.Remove(objId);
if (objId == null || objId.Length == 0)
{
return;
}
webCache.Remove(objId);
}
///
/// 返回一个指定的对象
///
/// 对象的关键字
/// 对象
public object RetrieveObject(string objId)
{
//return objectTable[objId];
if (objId == null || objId.Length == 0)
{
return null;
}
return webCache.Get(objId);
}
}
}