2013-11-27 2 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

Смежные вопросы