Reputation: 3288
I have the following model.
<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>
Then I have a test.aspx that querys a collection.
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 />";
}
}
When, I reload the page the query is re-run. Why isn't the data retrieved from cache?
<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>
Upvotes: 1
Views: 950
Reputation: 163
You should run this in a transaction and commit the transaction at the end.
NHibernate updates the 2nd level cache only if the transaction has been committed successfully. If you don't start a NHibernate transaction, NHibernate will not know if the results are good enough to be cached.
Upvotes: 5