NCache 4.6 - Online Documentation

How to Use Query Caching

 
NHibernate also provides a feature of query caching. Enabling query caching caches queries made by NHibernate to database, along with query result sets. Query caching makes use of second level cache to store queries, so that whenever the same query is needed to be executed again, it can be fetched from cache.
 
Any object retrieved as a result of query are cached to their respective regions, therefore objects must be marked as cacheable for efficient use of query caching. However query along with the primary keys of result sets is stored in a default query region named NHibernate.Cache.StandardQueryCache.
 
Since not every query's results remain same for a period of time. Therefore query caching should only be used with queries whose results are likely to be changed less frequently.
 
How to Enable Query Caching
 
To enable query caching, add the following property in NHibernate configuration section's session-factory tag:
 
    <property name="cache.use_query_cache">true</property>
 
Enabling query cache does not cache each query by default. Instead queries needed to be cached are to be set cacheable in code. To set a query cacheable, call SetCacheable(true) function of query while creating query, e.g.,
 
IQuery query = session.CreateQuery("from Customer c").SetCacheable(true);
 
 
See Also