<property name="hibernate.cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="expiration">120</property>NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache还可以替换为NHibernate.Caches.Prevalence.PrevalenceCacheProvider, NHibernate.Caches.Prevalence,代表缓存的实现类,在bin目录中有这样两个dllNHibernate.Caches.SysCache.dll,NHibernate.Caches.Prevalence.dll用哪个就把哪个拷贝到应用程序的bin目录下
expiration代表缓存过期时间,单位S
设置完后,还需要在对象的映射文件中配置二级缓存的策略,比如我在User.hbm.xml中如下配置
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NhibernateSample1.User,NhibernateSample1" table="Users" lazy="false">
<cache usage="read-write"/>
<id name="Id" column="Id" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" column="Name" type="string" length="64" not-null="true" unique="true"></property>
<property name="Pwd" column="Pwd" type="string" length="64" not-null="true"></property>
<many-to-one name="Role" class="NhibernateSample1.Role,NhibernateSample1" column="RoleID"></many-to-one>
</class>
</hibernate-mapping>NHibernateHelper.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NHibernate;
using NHibernate.Cfg;

namespace WebApp

{
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;
static NHibernateHelper()
{
string cfgPath = @"E:\my project\nhibernate study\simple4\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml";
sessionFactory = new NHibernate.Cfg.Configuration().Configure(cfgPath).BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}
return currentSession;
}
public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
// No current session
return;
}
currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}
public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}
}
页面代码:
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
ISession session = NHibernateHelper.GetCurrentSession();
ITransaction tra = session.BeginTransaction();
session.Load(typeof(NhibernateSample1.User), 1);
tra.Commit();
sw.Stop();
Response.Write(sw.ElapsedTicks+"<br>");
sw.Reset();
sw.Start();
session = NHibernateHelper.GetCurrentSession();
tra = session.BeginTransaction();
session.Load(typeof(NhibernateSample1.User), 1);
tra.Commit();
sw.Stop();
Response.Write(sw.ElapsedTicks + "<br>");
sw.Reset();
sw.Start();
session = NHibernateHelper.GetCurrentSession();
session.Close();
sw.Stop();
Response.Write(sw.ElapsedTicks + "<br>");第一次运行,用SQL事件探查器,结果为
从截图中的SQL语句看,第一次从数据库中加在User数据
第二次
从图中可以看出,第二次加载User对象,并没有从数据中获取数据,而是将没有设置Cache的Role信息从User里面获取。
当然在利用缓存的时候,缓存不会知道另外一个进程存储的实体发生变化,应该自己建立一些策略来及时地更新缓存快照。而且当ISessionFactory销毁的时候,二级缓存也会随之销毁,这也是应用的时候,应该注意的。