电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

企业库缓存依赖的实现-基于文件依赖


发布日期:2021/8/11
 

最近在做项目的时候采用用Codesmith和Nettiers生成的框架来实现生成的代码核心是基于企业库的所以最近在恶补企业库对于缓存的学习当然是必不可少的尤其是经常要用到得缓存依赖这里我用到的是文件依赖来举例子其他的都大同小异主要就是要实现ICacheItemExpiration中的返回值类型为bool类型的HasExpired方法来控制到期与否实现此方法是关键所在下面是程序清单欢迎大家指正

step 实现缓存到期接口此类就为缓存项依赖的类为缓存依赖的核心尤其是其中HasExpired方法的定义此类的核心就是使用lastCount是否变化来判断缓存是否到期如果有变化则HasExpired方法返回true否则返回false

Codeusing System;

using SystemWeb;

using MicrosoftPracticesEnterpriseLibraryCaching;

/// <summary>

///CacheItemDependency 的摘要说明

/// </summary>public class CacheItemDependency : ICacheItemExpiration

{

//依赖缓存项键 private readonly string dependencyCacheKey;

//依赖缓存项值 private SystemInt lastCount; #region Constructor /// <summary>

/// 初始化依赖缓存项如果此缓存管理对象存在则取出缓存的数据若不存在就要对此缓存管理赋值

/// </summary>

/// <param name=cacheKey>依赖缓存项的键</param>

public CacheItemDependency(string cacheKey)

{

dependencyCacheKey = cacheKey; ICacheManager cacheManager = CacheFactoryGetCacheManager();

lastCount = IntMinValue;

if (cacheManager != null)

{

if (cacheManagerContains(cacheKey))

{

object o = cacheManagerGetData(cacheKey);

if (o != null)

{

thislastCount = (int)o;

}

lastCount = (int)cacheManagerGetData(cacheKey);

}

else

{

cacheManagerAdd(cacheKey lastCount);

}

}

}

#endregion #region Properties public string DependencyCacheKey

{

get

{

return dependencyCacheKey;

}

}

public SystemInt LastCount

{

get

{ r

eturn lastCount;

}

}

#endregion #region ICacheItemExpiration Members public bool HasExpired()

{

ICacheManager cacheManager = CacheFactoryGetCacheManager();

if (cacheManager == null)

{

return true;

}

SystemInt currentCount = (int)cacheManagerGetData(dependencyCacheKey);

if (currentCount != lastCount)

{ return true; }

else

{

return false;

}

}

public void Notify()

{ }

public void Initialize(CacheItem owningCacheItem) { }

#endregion}

using System;

using SystemWeb;

using MicrosoftPracticesEnterpriseLibraryCaching;

///

///CacheItemDependency 的摘要说明

///

public class CacheItemDependency : ICacheItemExpiration

{

//依赖缓存项键

private readonly string dependencyCacheKey;

//依赖缓存项值

private SystemInt lastCount;

#region Constructor

///

/// 初始化依赖缓存项如果此缓存管理对象存在则取出缓存的数据若不存在就要对此缓存管理赋值

///

/// 依赖缓存项的键

public CacheItemDependency(string cacheKey)

{

dependencyCacheKey = cacheKey;

ICacheManager cacheManager = CacheFactoryGetCacheManager();

lastCount = IntMinValue;

if (cacheManager != null)

{

if (cacheManagerContains(cacheKey))

{

object o = cacheManagerGetData(cacheKey);

if (o != null)

{

thislastCount = (int)o;

}

lastCount = (int)cacheManagerGetData(cacheKey);

}

else

{

cacheManagerAdd(cacheKey lastCount);

}

}

}

#endregion

#region Properties

public string DependencyCacheKey

{

get { return dependencyCacheKey; }

}

public SystemInt LastCount

{

get { return lastCount; }

}

#endregion

#region ICacheItemExpiration Members

public bool HasExpired()

{

ICacheManager cacheManager = CacheFactoryGetCacheManager();

if (cacheManager == null)

{

return true;

}

SystemInt currentCount = (int)cacheManagerGetData(dependencyCacheKey);

if (currentCount != lastCount)

{

return true;

}

else

{

return false;

}

}

public void Notify()

{

}

public void Initialize(CacheItem owningCacheItem)

{

}

#endregion

}

step 定义修改依赖项缓存的方法

Codeusing System;using SystemWeb;using MicrosoftPracticesEnterpriseLibraryCaching;

/// <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 = CacheFactoryGetCacheManager();

foreach (string cacheKey in cacheKeys)

{

if (cacheManager != null && cacheManagerContains(cacheKey))

{

int lastCount = (int)cacheManagerGetData(cacheKey); if (lastCount < IntMaxValue) { lastCount++;

}

else

{

lastCount = IntMinValue;

} // 这一句的作用在于更新以cacheKey为key的缓存项从而使依赖于此缓存项的缓存项失效 cacheManagerAdd(cacheKey lastCount);

}

}

}

}

using System;

using SystemWeb;

using MicrosoftPracticesEnterpriseLibraryCaching;

///

///DataAccessUtil 的摘要说明

///

public class DataAccessUtil

{

public DataAccessUtil()

{

//

//TODO: 在此处添加构造函数逻辑

//

}

///

/// 更新所有以cacheKeys中元素为key的缓存项

///

/// 缓存项的key的数组

public static void UpdateCacheDependency(string[] cacheKeys)

{

ICacheManager cacheManager = CacheFactoryGetCacheManager();

foreach (string cacheKey in cacheKeys)

{

if (cacheManager != null && cacheManagerContains(cacheKey))

{

int lastCount = (int)cacheManagerGetData(cacheKey);

if (lastCount < IntMaxValue)

{

lastCount++;

}

else

{

lastCount = IntMinValue;

}

// 这一句的作用在于更新以cacheKey为key的缓存项从而使依赖于此缓存项的缓存项失效

cacheManagerAdd(cacheKey lastCount);

}

}

}

}

step 测试实体下面只是个简单的测试大家可以发散一下写出更加有复用性的方法

Codeusing System;

using SystemCollectionsGeneric;

using SystemLinq;

using SystemWeb;

using SystemWebUI;

using SystemWebUIWebControls;

using MicrosoftPracticesEnterpriseLibraryCaching;

public partial class Test : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

if (!IsPostBack)

{

ICacheManager cacheManager = CacheFactoryGetCacheManager();

cacheManagerAdd(s TextBoxText CacheItemPriorityNormal null new CacheItemDependency(s));

thisLabelText = cacheManagerGetData(s) as string;

}

} protected void Button_Click(object sender EventArgs e)

{

ICacheManager cacheManager = CacheFactoryGetCacheManager();

DataAccessUtilUpdateCacheDependency(new string[] { s });

if (cacheManagerGetData(s) == null)

{

cacheManagerAdd(s TextBoxText CacheItemPriorityNormal null new CacheItemDependency(s));

} thisLabelText = cacheManagerGetData(s) as string;

}

}

上一篇:如何循序渐进向DotNet架构师发展

下一篇:获取GridView的rowdata