扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:ghost 来源:CSDN 2007年9月24日
关键字: ghost NHibernate 二级缓存
<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目录中有这样两个dll
NHibernate.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>");
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1741108
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者