Query NHibernate Cache
NHibernate also provides a feature of query caching. By enabling query caching, queries made by NHibernate to the database, along with query result sets are cached in NHibernate Cache. Query caching makes use of Second-level Cache to store queries, so that whenever the same query executes again, it can be fetched from the cache.
Any object retrieved as a result of query is cached to its respective regions in NHibernate cache, therefore objects must be marked as cacheable for efficient use of query caching. However, the query and primary keys of the result set are stored in a default query region named NHibernate.Cache.StandardQueryCache. The StandardQueryCache represents a cache used for associating keys with timestamps, so a cache can be kept up-to-date centrally.
Note
Not every query's results remain the 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 in NHibernate Cache
Setting up Query Caching lets NCache intercept database queries and fetch data from the backend only if it changes, thus eliminating redundant roundtrips to database.
To enable query caching, add the following property in the 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 that need to be cached are set cacheable in code. To set a query cacheable, call SetCacheable(true) function of query while creating query, e.g.,
IQuery qry = session.CreateQuery("from Customer c").SetCacheable(true);
NCache now provides support for asynchronous tasks for add, update, and remove with NHibernate as Second-level Cache.
Using Async Operations with NHibernate Cache
The use of asynchronous calls such as FlushAsync and EvictAsync helps avoid thread-pool starvation and makes the application more responsive even in a high concurrency environment.
The examples below show the usage of asynchronous operations with NCache as NHibernate Second-level Cache.
ListAsyncperforms the retrieval of data from the database and caches the result in NCache asynchronously.
customers = qry.ListAsync().Result;
FlushAsyncsynchronizes the underlying persistent store with the state held in memory asynchronously.
session.Save(customers);
session.FlushAsync();
EvictAsyncremoves the customer object asynchronously.
ISessionFactory factory;
// Removes a single customer with the provided attribute asynchronously
factory.EvictAsync(typeof(Customer), CustomerID);
// OR
// Removes all customers asynchronously
factory.EvictAsync(typeof(Customer));
Using Sync Operations with NHibernate
Synchronous Operations offer an easy implementation for legacy applications requiring an immediate sequential process. The examples below show the usage of synchronous operations with NCache as NHibernate Second-level Cache.
Listperforms the retrieval of data from the database and caches the result in NCache synchronously.
customers = qry.List().Result;
Flushsynchronizes the underlying persistent store with the state held in memory.
session.Save(customers);
session.Flush();
Evictremoves the customer object synchronously.
ISessionFactory factory;
// Removes a single customer with the provided attribute synchronously
factory.Evict(typeof(Customer), CustomerID);
// OR
// Removes all customers synchronously
factory.Evict(typeof(Customer));
See Also
Using NCache as NHibernate Second-level Cache
Configuring Database Synchronization with NHibernate
Entity Framework Cache Provider