.Net 缓存代理类 的初步考虑.
2024-07-10 12:58:41
供稿:网友
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 继承过来.