小弟最近在编写
![](/icons/56436yi.gif)
个O/RM组件(当然功能还是相当少
![](/icons/56436de.gif)
).大家都应该清楚把实体对象更新到数据库必须经过
![](/icons/56436yi.gif)
系列
![](/icons/56436de.gif)
转换;特别是SQL语句
![](/icons/56436de.gif)
生成是比较费资源
![](/icons/56436de.gif)
,
![](/icons/56436yinwei.gif)
中间处里
![](/icons/56436de.gif)
东西实大是太多了.在设计
![](/icons/56436de.gif)
过程中我就想如果
![](/icons/56436yi.gif)
个对象插入数据库后把相应
![](/icons/56436de.gif)
Command保存在缓存Cache中;下
![](/icons/56436yi.gif)
次同
![](/icons/56436yi.gif)
个类型
![](/icons/56436de.gif)
对象做这个操作时检测
![](/icons/56436yi.gif)
下缓存Cache如果有就直接拿来用这样效率应该会高些.抱着这种想法就开始设计了(不过心里还是上上下下
![](/icons/56436de.gif)
,毕竟第
![](/icons/56436yi.gif)
次尝试).
![](/icons/56436yinwei.gif)
缓存Cache中
![](/icons/56436de.gif)
对象处理比较复杂点,在多线程中存在共享
![](/icons/56436de.gif)
问题,如果两个线程同时
![](/icons/56436diaoyong.gif)
同
![](/icons/56436yi.gif)
个Command这样
![](/icons/56436yi.gif)
定会产生处理
![](/icons/56436cuowu.gif)
![](/icons/56436de.gif)
!为了更好地控制Command对象
![](/icons/56436de.gif)
共享,特别为Command定义了持久化
![](/icons/56436de.gif)
接口.经过
![](/icons/56436yi.gif)
段时间
![](/icons/56436de.gif)
设计和编写,算有点成果吧,顺把自己做
![](/icons/56436de.gif)
东西共享
![](/icons/56436yi.gif)
下.以下是组件测试
![](/icons/56436de.gif)
情况P4 2.4 1GSqlServer sp3运行
![](/icons/56436de.gif)
代码大概如下:Entitys.Customers customer =
![](/icons/56436new.gif)
Test.Entitys.Customers( );DateTime dt = DateTime.Now;using( HFSoft.Data.IDataSession session = mapcontainer.OpenSession( ) ){ session.Open( ); for(
![](/icons/56436int.gif)
i =0; i<2000; i
![](/icons/56436jiajia.gif)
) { customer.CustomerID = Guid.NewGuid( ).
![](/icons/56436ToString.gif)
( ); customer.CompanyName =
=cs_yinhao>\"henry\"; session.Save( customer ); }}tp1 =
![](/icons/56436new.gif)
TimeSpan( DateTime.Now.Ticks - dt.Ticks );不开启缓存Cache(5个线程运行时间)00:00:05.703125000:00:06.828125000:00:05.015625000:00:06.687500000:00:06.4218750--------------------------------------------------------开启5个命令缓存Cache(5个线程运行时间)00:00:04.890625000:00:03.562500000:00:02.875000000:00:04.937500000:00:05.4843750---------------------------------------------------------以下是缓存Cache类
![](/icons/56436de.gif)
源码
=cs_comment>/// <summary>
=cs_comment>/// 数据缓存Cache保存信息异步处理委托
=cs_comment>/// </summary>delegate void EventSaveCache( object key,object value );
=cs_comment>/// <summary>
=cs_comment>/// 对象缓存Cache类
=cs_comment>/// </summary>public
![](/icons/56436class.gif)
Cache{ private MappingContainer mContainer;
=cs_comment>/// <summary>
=cs_comment>/// 获取或设置当前缓存Cache对象所在
![](/icons/56436de.gif)
关系映象容器
=cs_comment>/// </summary> public MappingContainer Container { get {
![](/icons/56436return.gif)
mContainer; }
![](/icons/56436set.gif)
{ mContainer = value; } }
=cs_comment>/// <summary>
=cs_comment>/// 构造缓存Cache对象
=cs_comment>/// </summary> public Cache( ) {
=cs_comment>// // TODO: 在此处添加构造
![](/icons/56436hanshu.gif)
逻辑 // }
=cs_comment>/// <summary>
=cs_comment>/// 用于缓存Cache数据
![](/icons/56436de.gif)
Hashtable
=cs_comment>/// </summary> protected
![](/icons/56436System.gif)
.Collections.Hashtable _Cache =
![](/icons/56436System.gif)
.Collections.Hashtable( ); protected Object _LockObj =
![](/icons/56436new.gif)
object( );
=cs_comment>/// <summary>
=cs_comment>/// 获取指定键值
![](/icons/56436de.gif)
对象
=cs_comment>/// </summary>
=cs_comment>/// <param name=
=cs_yinhao>\"key\">键值</param>
=cs_comment>/// <
![](/icons/56436return.gif)
s>object</
![](/icons/56436return.gif)
s> public virtual object GetObject( object key ) {
![](/icons/56436if.gif)
( _Cache.ContainsKey( key ) )
![](/icons/56436return.gif)
_Cache[key];
![](/icons/56436return.gif)
null; }
=cs_comment>/// <summary>
=cs_comment>/// 把对象按指定
![](/icons/56436de.gif)
键值保存到缓存Cache中
=cs_comment>/// </summary>
=cs_comment>/// <param name=
=cs_yinhao>\"key\">键值</param>
=cs_comment>/// <param name=
=cs_yinhao>\"value\">保存
![](/icons/56436de.gif)
对象</param> public void SaveCaech( object key,object value ) { EventSaveCache save =
![](/icons/56436new.gif)
EventSaveCache( SetCache ); IAsyncResult ar = save.BeginInvoke( key,value,
![](/icons/56436System.gif)
.AsyncCallback( Results ),null ); } private void Results( IAsyncResult ar ) { EventSaveCache fd = ( EventSaveCache )( ( AsyncResult )ar ).AsyncDelegate; fd.EndInvoke( ar ); }
=cs_comment>/// <summary>
=cs_comment>/// 把对象按指定
![](/icons/56436de.gif)
键值保存到缓存Cache中
=cs_comment>/// </summary>
=cs_comment>/// <param name=
=cs_yinhao>\"key\">键值</param>
=cs_comment>/// <param name=
=cs_yinhao>\"value\">保存
![](/icons/56436de.gif)
对象</param> protected virtual void SetCache( object key ,object value ) { lock( _LockObj ) {
![](/icons/56436if.gif)
( !_Cache.ContainsKey( key ) ) _Cache.Add( key,value ); } } public
![](/icons/56436int.gif)
Count { get {
![](/icons/56436return.gif)
_Cache.Count; } }
=cs_comment>/// <summary>
=cs_comment>/// 在缓存Cache中删除指定键值
![](/icons/56436de.gif)
对象
=cs_comment>/// </summary>
=cs_comment>/// <param name=
=cs_yinhao>\"key\">键值</param> public virtual void DelObject( object key ) {
lock( _Cache.SyncRoot ) { _Cache.Remove( key ); } }
=cs_comment>/// <summary>
=cs_comment>/// 清除缓存Cache中所有对象
=cs_comment>/// </summary> public virtual void Clear( ) { lock( _Cache.SyncRoot ) { _Cache.Clear( ); } }}
=cs_comment>/// <summary>
=cs_comment>///针对
![](/icons/56436yi.gif)
条记录操作命令
![](/icons/56436de.gif)
缓存Cache类
=cs_comment>/// </summary>public
![](/icons/56436class.gif)
CachePersistentCommand:Cache{
=cs_comment>/// <summary>
=cs_comment>/// 把记录操作命令缓存Cache到内存中
=cs_comment>/// </summary>
=cs_comment>/// <param name=
=cs_yinhao>\"key\">标识</param>
=cs_comment>/// <param name=
=cs_yinhao>\"value\">值</param> protected override void SetCache( object key, object value ) { lock( _LockObj ) {
![](/icons/56436int.gif)
count=0;
![](/icons/56436if.gif)
( Container.Config.CommandsCache.ContainsKey( key ) ) count=(
![](/icons/56436int.gif)
) Container.Config.CommandsCache[key];
![](/icons/56436System.gif)
.Collections.IList _list;
=cs_comment>//如果缓存Cache中已经存在这种命令
![](/icons/56436de.gif)
列表
![](/icons/56436if.gif)
( _Cache.ContainsKey( key ) ) { _list = (
![](/icons/56436System.gif)
.Collections.IList )_Cache[key];
![](/icons/56436if.gif)
(count >0 )
=cs_comment>//命令
![](/icons/56436de.gif)
缓存Cache总数 {
![](/icons/56436if.gif)
( _list.Count < count )
=cs_comment>//缓存Cache数据量少于缓存Cache总数 _list.Add( value ); }
![](/icons/56436else.gif)
{
![](/icons/56436if.gif)
( _list.Count < Container.Config.CommandBuffer )
=cs_comment>//缓存Cache数小于组件
![](/icons/56436de.gif)
默认列表 _list.Add( value ); } }
![](/icons/56436else.gif)
=cs_comment>//如果不存在列表 {
![](/icons/56436if.gif)
( count >0 || Container.Config.CommandBuffer >0 )
=cs_comment>//如果组件允许对象缓存Cache { _list =
![](/icons/56436System.gif)
.Collections.ArrayList( ); _list.Add( value ); _Cache.Add( key,_list ); } } } }
=cs_comment>/// <summary>
=cs_comment>/// 从缓存Cache中获取相关命令对象
=cs_comment>/// </summary>
=cs_comment>/// <param name=
=cs_yinhao>\"key\">标识</param>
=cs_comment>/// <
![](/icons/56436return.gif)
s>IPersistentCommand</
![](/icons/56436return.gif)
s> public override object GetObject( object key ) {
![](/icons/56436if.gif)
( _Cache.Contains( key ) )
=cs_comment>//如果命令存在缓冲中 { foreach( IPersistentCommand cmd in (
![](/icons/56436System.gif)
.Collections.IList )_Cache[key] ) {
![](/icons/56436if.gif)
( !cmd.State )
=cs_comment>//命令是否可以过行锁定
![](/icons/56436if.gif)
( cmd.Lock( ) )
=cs_comment>//命令锁定
![](/icons/56436return.gif)
cmd; } }
![](/icons/56436return.gif)
null; }}