2013-11-27 71 views
1

我有以下模型。爲什麼NHibernate不會在第二次請求時使用二級緩存?

<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> 

回答

4

你應該在一個事務中運行此,並在年底提交事務。

僅當事務已成功提交時,NHibernate纔會更新第二級緩存。如果你不啓動一個NHibernate事務,NHibernate將不知道結果是否足夠緩存。

+0

這就是事實!謝謝。 – roydukkey

相關問題