NHibernate系列文章十:NHibernate对象二级缓存下
摘要
上一节对NHibernate二级缓存做了简单介绍,NHibernate二级缓存是由SessionFactory管理的,所有Session共享。这一节介绍二级缓存其他两个方面:二级缓存查询和二级缓存管理。
1. NHibernate二级缓存查询
NHibernate二级缓存查询是指NHibernate将查询记录的集合放到二级缓存中,之后每次查询都可以从二级缓存中拿查询记录信息。
二级缓存使用步骤:
1)在hibernate.cfg.xml文件中,使用cache.use_query_cache定义使用二级缓存查询:<property name="cache.use_query_cache">true</property>
2)在代码中显示地使用IQuery.SetCacheable(true)方法,第一次使用这个方法时,将查询条件和查询结果组成一个字典对象放入二级缓存中,字典的键是查询条件,值是查询结果记录集合。在第二次使用同样查询条件查询时再次调用这个方法,NHibernate直接从二级缓存中读取。如果查询结果记录集合在第一次调用SetCacheable后有变化(添加、修改、删除),则会清空二级缓存中的字典对象,第二次查询从数据库查询。
二级缓存查询有多种定义方式:
1)显示启用缓存查询
只使用IQuery.SetCacheable(true)方法。
程序演示
修改hibernate.cfg.xml文件,添加<property name="cache.use_query_cache">true</property>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.connection_string_name">default</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
<mapping assembly="NHibernateDemoApp"/>
<class-cache class="NHibernateDemoApp.Customer,NHibernateDemoApp" usage="read-write"/>
</session-factory>
</hibernate-configuration>
修改Main函数
static void Main(string[] args)
{
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); using (var session = SessionFactory.OpenSession())
{
IList<Customer> customers = session.CreateQuery("from Customer c where c.Id > 1")
.SetCacheable(true)
.List<Customer>();
Console.WriteLine("list count: {0}", customers.Count);
} using (var session = SessionFactory.OpenSession())
{
IList<Customer> customers = session.CreateQuery("from Customer c where c.Id > 1")
.SetCacheable(true)
.List<Customer>();
Console.WriteLine("list count: {0}", customers.Count);
} Console.WriteLine("Completed");
Console.ReadLine();
}
打开NHibernateProfile,执行程序,监控结果:
第一次查询后将结果存入二级缓存,第二次查询直接从二级缓存中读取。
2)指定命名缓存区域
在代码中显示调用session.SetCacheRegion方法给二级缓存结果一个强命名。
程序演示
修改Main函数
static void Main(string[] args)
{
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); using (var session = SessionFactory.OpenSession())
{
IList<Customer> customers = session.CreateQuery("from Customer c where c.Id > 1")
.SetCacheable(true)
.SetCacheRegion("customer_chache")
.List<Customer>();
Console.WriteLine("list count: {0}", customers.Count);
} using (var session = SessionFactory.OpenSession())
{
IList<Customer> customers = session.CreateQuery("from Customer c where c.Id > 1")
.SetCacheable(true)
.SetCacheRegion("customer_chache")
.List<Customer>();
Console.WriteLine("list count: {0}", customers.Count);
} Console.WriteLine("Completed");
Console.ReadLine();
}
清空NHibernateProfile监控记录,执行程序,得到结果
3)命名查询
在实体关系映射配置文件hibernate.cfg.xml中,定义二级缓存的命名查询。
<query cacheable="true" cache-mode="normal" name="自定义名称">
Hibernate Query Language查询字符串
</query>
cache-mode一般使用normal(默认方式),从二级缓存中读写。
cache-mode有五个枚举值
- Ignore:不使用二级缓存
- Put:只向二级缓存写数据
- Get:只向二级缓存读数据
- Normal:向二级缓存读和写数据
- Refresh:每次强制刷新二级缓存
程序演示
修改Customer.hbm.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateDemoApp" namespace="NHibernateDemoApp">
<class name="Customer" table="Customer">
<id name="Id">
<generator class="native"/>
</id>
<property name="FirstName" not-null="true"/>
<property name="LastName" not-null ="true"/>
<property name="AverageRating"/>
<property name="Points"/>
<property name="HasGoldStatus"/>
<property name="MemberSince"/>
<property name="CreditRating" type="CustomerCreditRating"/>
<property name="Street"/>
<property name="City"/>
<property name="Province"/>
<property name="Country"/>
</class>
<query cacheable="true" cache-mode="normal" name="select_customer">
from Customer
</query>
</hibernate-mapping>
修改Main函数
static void Main(string[] args)
{
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); using (var session = SessionFactory.OpenSession())
{
IList<Customer> customers = session.GetNamedQuery("select_customer")
.List<Customer>();
Console.WriteLine("list count: {0}", customers.Count);
} using (var session = SessionFactory.OpenSession())
{
IList<Customer> customers = session.GetNamedQuery("select_customer")
.List<Customer>();
Console.WriteLine("list count: {0}", customers.Count);
} Console.WriteLine("Completed");
Console.ReadLine();
}
清空Profile,执行程序得到结果
2. NHibernate二级缓存管理
NHibernate二级缓存由SessionFactory管理,SessionFactory提供了多个删除二级缓存内容的方法。
- Evict:从二级缓存中删除类实例
- EvictEntity:从二级缓存中删除指定实例
- EvictCollection:从二级缓存中删除集合
- EvictQueries:从二级缓存中删除查询
程序演示
修改Main函数
static void Main(string[] args)
{
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); using (var session = SessionFactory.OpenSession())
{
var customer = session.Get<Customer>();
}
SessionFactory.Evict(typeof(Customer));
using (var session = SessionFactory.OpenSession())
{
var customer = session.Get<Customer>();
} Console.WriteLine("Completed");
Console.ReadLine();
}
清空NHibernateProfile的Session,执行程序得到结果
也可以强制调用session.SetCacheMode(CacheMode.Refresh),刷新二级缓存内容,第二个session查询时,session重新查询数据库。
程序演示
修改Main函数
static void Main(string[] args)
{
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); using (var session = SessionFactory.OpenSession())
{
IList<Customer> customers = session.CreateQuery("from Customer c where c.Id > 1")
.SetCacheable(true)
.SetCacheRegion("customer_chache")
.List<Customer>();
Console.WriteLine("list count: {0}", customers.Count);
} using (var session = SessionFactory.OpenSession())
{
IList<Customer> customers = session.CreateQuery("from Customer c where c.Id > 1")
.SetCacheable(true)
.SetCacheRegion("customer_chache")
.SetCacheMode(CacheMode.Refresh)
.List<Customer>();
Console.WriteLine("list count: {0}", customers.Count);
} Console.WriteLine("Completed");
Console.ReadLine();
}
执行结果,第二次强制查询数据库
NHibernate系列文章十:NHibernate对象二级缓存下的更多相关文章
- NHibernate系列文章十八:NHibernate关系之一对多(附程序下载)
摘要 这篇文章介绍NHibernate最实用的内容:关系映射. NHibernate的关系映射方式有三种: Set:无序对象集合,集合中每一个元素不能重复. List:有序对象集合,集合中的元素可以重 ...
- NHibernate系列文章十五:NHibernate组件
摘要 前面文章介绍了NHibernate对简单.net数据类型的映射对照表.NHibernate也可以映射复杂数据类型,这里介绍通过组件映射NHibernate值对象. 1. NHibernate引用 ...
- NHibernate系列文章十六:使用程序集管理NHibernate项目(附程序下载)
摘要 在实际的项目中,经常是将NHibernate的实体关系映射类做成独立的工程(assembly dll),只对外提供Session调用的接口.这个程序集作为数据访问层,可以被上面的多个工程(ASP ...
- NHibernate系列文章十二:Load/Get方法
摘要 NHibernate提供两个方法按主键值查找对象:Load/Get. 1. Load/Get方法的区别 Load: Load方法可以对查询进行优化. Load方法实际得到一proxy对象,并不立 ...
- NHibernate系列文章十四:NHibernate事务
摘要 NHibernate实现事务机制非常简单,调用ISession.BeginTransaction()开启一个事务对象ITransaction,使用ITransaction.Commit()提交事 ...
- NHibernate系列文章十九:NHibernate关系之多对多关系(附程序下载)
摘要 NHibernate的多对多关系映射由many-to-many定义. 从这里下载本文的代码NHibernate Demo 1.修改数据库 添加Product表 添加ProductOrder表 数 ...
- NHibernate系列文章目录
第一章:NHibernate基础 NHibernate介绍 第一个NHibernate工程 简单的增删改查询 运行时监控 NHibernate配置 数据类型映射 Get/Load方法 NHiberna ...
- CRL快速开发框架系列教程十(导出对象结构)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- NHibernate系列文章九:NHibernate对象二级缓存上
摘要 NHibernate的二级缓存由SessionFactory管理,由所有Session共享. NHibernate缓存读取顺序: 首先从一级缓存中读取,如果一级缓存对象存在,则读取一级缓存对象并 ...
随机推荐
- Oracle 11gR2 安装教学
官方网址:http://www.oracle.com/index.html 选择你的"操作系统"下载 例如: 环境:x64 Win2012 R2 Oracle:win64_11gR ...
- 12-16php测试题
2. 以下哪个SQL语句是正确的( d )A:insert into users ('p001','张三','男'); B:create table (Code int primary key); C ...
- iOS-硬件声音 ,振动,提示警告
为了引起用户注意发出警告的时候,常常伴随有提示音震动等.系统声音服务提供了一个接口,用于播放不超过30秒的声音文件,他支持的格式有CAF,AIF,WAV. iOS使用该API支持3种不同的通知: 声音 ...
- centos启用ftp功能
1.安装vsftpd组件,安装完后,有/etc/vsftpd/vsftpd.conf 文件,用来配置,还有新建了一个ftp用户和ftp的组,指向home目录为/var/ftp,默认是nologin(不 ...
- 在linux和windows下自动备份数据库
摘要: 详细介绍在windows和linux下自动备份数据库的过程,希望可以让新手立即上手吧! 本文档内容共分为2大部分:linux和windows Linux和windows都分为:准备工作和操作阶 ...
- decimal(a,b)
decimal(a,b)a指定指定小数点左边和右边可以存储的十进制数字的最大个数,最大精度38.b指定小数点右边可以存储的十进制数字的最大个数.小数位数必须是从 0 到 a之间的值.默认小数位数是 0 ...
- 在IOS输入框中 键盘上显示“搜索”
移动端web页面上使用软键盘时如何让其显示“前往”(GO)而不是换行?‘ 用一个 form 表单包裹住就会显示前往,单独的一个 input 就会提示换行.下面是测试地址: 有表单:https://js ...
- 使用as3控制动画的播放与暂停
1.需要两个按钮元件 2.在属性面板为两个按钮元件分别命名为pausebutton与playButton 3.代码 stop(); pausebutton.visible = false; playB ...
- Android画图Path的使用
/** * Paint类介绍 * * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色, * 样式等绘制信息,指定了如何绘制文本 ...
- Sublime Text 3最好的功能、插件和设置(转)
Sublime Text 3 是一个了不起的软件.首先,它是一个干净,实用,可以快速的编写代码编辑器.它不仅具有令人难以置信的内置功能(多行编辑和VIM模式),而且还支持插件,代码片段和其他许多东西. ...