Question

Is it possible to configure expiration of NHibernate's query cache?

For second level cache I can do it from nhibernate.cfg.xml, but I can't find a way for SQL query cache.

EDIT:

ICriteria query = CreateCriteria()
                  .Add(Expression.Eq("Email", identifiant))
                  .SetCacheable(true)
                  .SetCacheRegion("X");



 <syscache>
    <cache region="X" expiration="10" priority="1" />
  </syscache>
Was it helpful?

Solution

Yes, we can set cache expiration via region. Adjust the query like this:

criteria.SetCacheable(true)
    .SetCacheMode(CacheMode.Normal)
    .SetCacheRegion("LongTerm");

And put similar configuration into web.config file

<configSections>
    <section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler, NHibernate.Caches.SysCache" requirePermission="false" />
</configSections>
<syscache>
    <cache region="LongTerm" expiration="180" priority="5" />
    <cache region="ShortTerm" expiration="60" priority="3" />
</syscache>

EDIT: I am just adding this link Class-cache not used when getting entity by criteria To be sure what I mean by SQL Query cache. In the linked answer I am explaining that topic

Just for a clarity. The configuration of the NHibernate "session-factory" must contain:

<property name="cache.use_query_cache">true</property>

This switch will make query cache working. More details: http://nhibernate.info/doc/nh/en/index.html#performance-querycache

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top