目录

写在前面

文档与系列文章

二级缓存

Nhibernate二级缓存提供程序

一个例子

总结

写在前面

上篇文章介绍了nhibernate中一级缓存的相关内容,一级缓存过期时间和ISession对象的生命周期相同,并且不同的Session不能共享缓存,一级缓存也可以成为ISession缓存。那么现在我们就学一下nhibernate中的二级缓存,即ISessionFactory级别缓存,可被所有的ISession所共享。二级缓存是可扩展的,在http://sourceforge.net/projects/nhcontrib/上提供了第三方的Nhibernate二级缓存提供程序。

文档与系列文章

[Nhibernate]体系结构

[NHibernate]ISessionFactory配置

[NHibernate]持久化类(Persistent Classes)

[NHibernate]O/R Mapping基础

[NHibernate]集合类(Collections)映射 

[NHibernate]关联映射

[NHibernate]Parent/Child

[NHibernate]缓存(NHibernate.Caches)

[NHibernate]NHibernate.Tool.hbm2net

[NHibernate]Nullables

[NHibernate]Nhibernate如何映射sqlserver中image字段

[NHibernate]基本配置与测试 

[NHibernate]HQL查询 

[NHibernate]条件查询Criteria Query

[NHibernate]增删改操作

[NHibernate]事务

[NHibernate]并发控制

[NHibernate]组件之依赖对象

[NHibernate]一对多关系(级联删除,级联添加)

[NHibernate]一对多关系(关联查询)

[NHibernate]多对多关系(关联查询)

[NHibernate]延迟加载

[NHibernate]立即加载

[NHibernate]视图处理

[NHibernate]N+1 Select查询问题分析

[NHibernate]存储过程的使用(一)

[NHibernate]存储过程的使用(二)

[NHibernate]存储过程的使用(三)

[Nhibernate]SchemaExport工具的使用(一)——通过映射文件修改数据表

[Nhibernate]SchemaExport工具的使用(二)——创建表及其约束、存储过程、视图

[Nhibernate]对象状态

[Nhibernate]一级缓存

二级缓存

关于二级缓存的详细可以参考[NHibernate]缓存(NHibernate.Caches)

NHibernate session有一个内部的(一级)缓存,存放着它的实体。这些缓存没有共享,因此session被销毁时它的缓存也被销毁了。NHibernate提供了二级缓存系统;它在SessionFactory级别工作。因此它被同一个SessionFactory产生的session共享。

在NHibernate中,当我们启用NHibernate二级缓存。

使用ISession进行数据操作时,NHibernate首先从内置缓存(一级缓存)中查找是否存在需要的数据,如果内置缓存不存在需要的数据,则查询二级缓存,如果二级缓存中存在所需数据,则直接使用缓存中数据,否则从数据库中查询数据并放入缓存中。

NHibernate本身提供了一个基于Hashtable的HashtableCache缓存,但是功能非常有限而且性能比较差,不适合在大型应用程序使用,我们可以使用第三方缓存提供程序作为NHibernate二级缓存实现。

使用缓存的缺点:

如果缓存策略设置不当,NHibernate不知道其它应用程序对数据库的修改及时更新缓存。因此,建议只对系统经常使用、数据量不大且不会被其它应用程序修改的只读数据(或很少被修改的数据)使用缓存。

Nhibernate二级缓存提供程序

NHibernate提供了NHibernate.Cache.ICacheProvider接口用来支持第三方缓存提供程序实现。开发缓存提供程序时,需要实现该接口作为NHibernate和缓存实现直接的适配器。NHibernate提供了常见的缓存提供程序的内置适配器,这些适配器都实现了NHibernate.Cache.ICacheProvider接口。

NHibernate.Cache.ICacheProvider定义如下:

 1 namespace NHibernate.Cache
 2 {
 3     // 摘要: 
 4     //     Support for pluggable caches
 5     public interface ICacheProvider
 6     {
 7         // 摘要: 
 8         //     Configure the cache
 9         //
10         // 参数: 
11         //   regionName:
12         //     the name of the cache region
13         //
14         //   properties:
15         //     configuration settings
16         ICache BuildCache(string regionName, IDictionary<string, string> properties);
17         //
18         // 摘要: 
19         //     generate a timestamp
20         long NextTimestamp();
21         //
22         // 摘要: 
23         //     Callback to perform any necessary initialization of the underlying cache
24         //     implementation during ISessionFactory construction.
25         //
26         // 参数: 
27         //   properties:
28         //     current configuration settings
29         void Start(IDictionary<string, string> properties);
30         //
31         // 摘要: 
32         //     Callback to perform any necessary cleanup of the underlying cache implementation
33         //     during NHibernate.ISessionFactory.Close().
34         void Stop();
35     }
36 }
NHibernate.Cache.ICacheProvider

相关文章: