最近在做项目的时候,采用用Codesmith和Nettiers生成的框架来实现,生成的代码核心是基于企业库的。所以最近在恶补企业库,对于缓存的学习当然是必不可少的,尤其是经常要用到得缓存依赖,这里我用到的是文件依赖来举例子,其他的都大同小异,主要就是要实现ICacheItemExpiration中的返回值类型为bool类型的HasExpired方法,来控制到期与否,实现此方法是关键所在。下面是程序清单,欢迎大家指正:
step1 实现缓存到期接口,此类就为缓存项依赖的类,为缓存依赖的核心,尤其是其中HasExpired方法的定义,此类的核心就是使用lastCount是否变化来判断缓存是否到期;如果有变化则HasExpired方法返回true,否则返回false。
Code
using System;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Caching;
/// <summary>
///CacheItemDependency 的摘要说明
/// </summary>
public class CacheItemDependency : ICacheItemExpiration
{
//依赖缓存项键
private readonly string dependencyCacheKey;
//依赖缓存项值
private System.Int32 lastCount;
#region Constructor
/// <summary>
/// 初始化依赖缓存项,如果此缓存管理对象存在,则取出缓存的数据;若不存在,就要对此缓存管理赋值
/// </summary>
/// <param name="cacheKey">依赖缓存项的键</param>
public CacheItemDependency(string cacheKey)
{
dependencyCacheKey = cacheKey;
ICacheManager cacheManager = CacheFactory.GetCacheManager();
lastCount = Int32.MinValue;
if (cacheManager != null)
{
if (cacheManager.Contains(cacheKey))
{
object o = cacheManager.GetData(cacheKey);
if (o != null)
{
this.lastCount = (int)o;
}
lastCount = (int)cacheManager.GetData(cacheKey);
}
else
{
cacheManager.Add(cacheKey, lastCount);
}
}
}
#endregion
#region Properties
public string DependencyCacheKey
{
get { return dependencyCacheKey; }
}
public System.Int32 LastCount
{
get { return lastCount; }
}
#endregion
#region ICacheItemExpiration Members
public bool HasExpired()
{
ICacheManager cacheManager = CacheFactory.GetCacheManager();
if (cacheManager == null)
{
return true;
}
System.Int32 currentCount = (int)cacheManager.GetData(dependencyCacheKey);
if (currentCount != lastCount)
{
return true;
}
else
{
return false;
}
}
public void Notify()
{
}
public void Initialize(CacheItem owningCacheItem)
{
}
#endregion
}
step2 定义修改依赖项缓存的方法
Code
using System;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Caching;
/// <summary>
///DataAccessUtil 的摘要说明
/// </summary>
public class DataAccessUtil
{
public DataAccessUtil()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 更新所有以cacheKeys中元素为key的缓存项
/// </summary>
/// <param name="cacheKeys">缓存项的key的数组</param>
public static void UpdateCacheDependency(string[] cacheKeys)
{
ICacheManager cacheManager = CacheFactory.GetCacheManager();
foreach (string cacheKey in cacheKeys)
{
if (cacheManager != null && cacheManager.Contains(cacheKey))
{
int lastCount = (int)cacheManager.GetData(cacheKey);
if (lastCount < Int32.MaxValue)
{
lastCount++;
}
else
{
lastCount = Int32.MinValue;
}
// 这一句的作用在于更新以cacheKey为key的缓存项,从而使依赖于此缓存项的缓存项失效.
cacheManager.Add(cacheKey, lastCount);
}
}
}
}
step3 测试实体,下面只是个简单的测试,大家可以发散一下,写出更加有复用性的方法。
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Caching;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ICacheManager cacheManager = CacheFactory.GetCacheManager();
cacheManager.Add("s", TextBox1.Text, CacheItemPriority.Normal, null, new CacheItemDependency("s1"));
this.Label1.Text = cacheManager.GetData("s") as string;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ICacheManager cacheManager = CacheFactory.GetCacheManager();
DataAccessUtil.UpdateCacheDependency(new string[] { "s1" });
if (cacheManager.GetData("s") == null)
{
cacheManager.Add("s", TextBox1.Text, CacheItemPriority.Normal, null, new CacheItemDependency("s1"));
}
this.Label1.Text = cacheManager.GetData("s") as string;
}
}