缓存
EntityData为实体数据类
我们这里将实体数据进行缓存,缓存方法是
//从Web.Config取得对缓存过期的设置
ICacheItemExpiration[] ice=ExpireTerm.GetExpirationPolicy();
//获得缓冲对象
CacheManager cm=CacheManager.GetCacheManager();
存储
cm.Add(EntityKeyCreator.CreateKey(entity),entity,ice,CacheItemPriority.Normal,null);
此类可以做实体代理类
using Microsoft.ApplicationBlocks.Cache;
/// <summary>
/// 缓冲实体
/// </summary>
/// <param name="entity">实体数据</param>
public static void CacheEntity(EntityData entity)
{
if(entity.IsEmpty)
return;
ICacheItemExpiration[] ice=ExpireTerm.GetExpirationPolicy();
//获得缓冲对象
CacheManager cm=CacheManager.GetCacheManager();
//存储
cm.Add(ExpireTerm.CreateKey(entity),entity,ice,CacheItemPriority.Normal,null);
}
ExpireTerm.class
using Microsoft.ApplicationBlocks.Cache;
internal static ICacheItemExpiration[] GetExpirationPolicy()
{
if(Object.Equals(configSetting,null))
{
configSetting = (ICacheItemExpiration[])ConfigurationSettings.GetConfig("TopCoolExpireTerm");
}
return configSetting;
}
internal static string CreateKey(EntityData entity)
{
DataSet ds = new DataSet();
DataColumn[] keyColumns;
if(entity.Tables.Contains(entity.MainTableName))
keyColumns = entity.Tables[entity.MainTableName].PrimaryKey;
else
keyColumns = entity.CurrentTable.PrimaryKey;//
StringBuilder sb = new StringBuilder(entity.EntityTypeName);
//用:分隔实体类列名
for(int i=0;i<keyColumns.Length;i++)
{
sb.Append(":");
sb.Append(entity[keyColumns[i].ColumnName].ToString());
}
return sb.ToString();
}
注意:EntityData从DataSet 继承过来.