Query Caching with Nhibernate
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 is cached to its respective regions, 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
.
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.
Enabling 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 qry = session.CreateQuery("from Customer c").SetCacheable(true);
NCache now provides support for asynchronous tasks for add, update, remove and clear with NHibernate as second level cache.
Using Async Operations with NHibernate
The examples below show the usage of asynchronous operations with NCache as NHibernate second level cache.
ListAsync()
performs the retrieval of data from the database and caches the result in NCache asynchronously.
customers = qry.ListAsync().Result;
FlushAsync
synchronizes the underlying persistent store with the state held in memory asynchronously.
session.Save(customers);
session.FlushAsync();
EvictAsync
removes 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 Synchronous Operations with NHibernate
The examples below show the usage of synchronous operations with NCache as NHibernate second level cache.
List()
performs the retrieval of data from the database and caches the result in NCache synchronously.
customers = qry.List().Result;
Flush()
synchronizes the underlying persistent store with the state held in memory.
session.Save(customers);
session.Flush();
Evict
removes 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
AppFabric to NCache Migration