我有以下型号。
<class name="Navigation" table="`navigation`">
<cache usage="nonstrict-read-write" region="LongTerm" />
<id name="Id" column="`navigation_id`" type="Int16" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" column="`navigation_name`" type="String" not-null="true" />
<property name="DateCreated" column="`navigation_date-created`" type="DateTime" not-null="true" />
<property name="DateSaved" column="`navigation_date-saved`" type="DateTime" not-null="true" />
<property name="Active" column="`navigation_active`" type="Boolean" not-null="true" />
<property name="RestrictToWebMaster" column="`navigation_web-master-restrict`" type="Boolean" not-null="true" />
<many-to-one name="User" column="user_id" class="User" />
<set name="Items" order-by="`navigation-item_index` asc" inverse="true" cascade="all-delete-orphan">
<cache usage="nonstrict-read-write" region="LongTerm" />
<key column="`navigation_id`" />
<one-to-many class="Navigation+Item" />
</set>
</class>
然后我有一个查询集合的test.aspx。
DataProvider provider = new DataProvider(SessionManager.GetSession());
protected void Page_Load(object sender, EventArgs e)
{
IList<Navigation> navs = provider.Session.QueryOver<Navigation>()
.Cacheable()
.List();
foreach (Navigation nav in navs) {
litTest.Text += nav.Name + "<br />";
}
}
当我重新加载页面时,重新运行查询。为什么不从缓存中检索数据?
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2-x-factories">
<session-factory name="Default">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="current_session_context_class">web</property>
<property name="show_sql">true</property>
<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.use_second_level_cache">true</property>
</session-factory>
</hibernate-configuration>
<syscache>
<cache region="LongTerm" expiration="3600" priority="5" />
<cache region="MediumTerm" expiration="900" priority="3" />
<cache region="ShortTerm" expiration="180" priority="1" />
</syscache>
答案 0 :(得分:5)
您应该在事务中运行它并在最后提交事务。
仅当事务已成功提交时,NHibernate才会更新第二级缓存。如果你没有启动NHibernate事务,NHibernate将不知道结果是否足以被缓存。