views:

135

answers:

1

Hello all. I want to know how make NHibernate store my queries at 2-nd level cache for specified time I see it only for entities cache.

Thank you for replying.

A: 

The query cache is not enabled by default. To enable it in your hibernate.cfg.xml:

<add key="hibernate.cache.use_query_cache" value="true" />

You should specify a cache region for queries. If not specified, the region will be "NHibernate.Cache.StandardQueryCache".

Session.CreateCriteria<User>()
    .SetCacheRegion("UserQuery")
    .List();

For syscache, cache regions are configured in you app.config:

<configuration>
    <configSections>
        <section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler,NHibernate.Caches.SysCache" />
    </configSections>    
    <syscache>
        <cache region="User" expiration="300" priority="3" />
        <cache region="UserQuery" expiration="60" priority="3" />
    </syscache>
</configuration>
Lachlan Roche