• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

Query NHibernate Cache

NHibernate also provides a feature of query caching. By enabling query caching, queries made by NHibernate to the database, along with the query result sets are cached in NHibernate Cache. Query caching makes use of the Second-level Cache to store queries, so that whenever the same query is executed again, it can be fetched from the cache.

Any object retrieved as a result of a 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 `NHibernate.Cache.StandardQueryCache stores cached query result sets, while UpdateTimestampsCache stores timestamps of the most recent updates to queryable tables and is used to determine whether cached query results are still valid.

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

When a cacheable query is executed, NHibernate can return a valid cached result instead of querying the database. If no valid cached result exists, the query is executed against the database and its result can be cached.

To enable query caching, add the following property to the session-factory tag in the NHibernate configuration section:

<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) method on the query while creating it, e.g.,

IQuery qry = session.CreateQuery("from Customer c").SetCacheable(true);

NHibernate provides asynchronous APIs for operations such as querying, flushing session state, and evicting cached data when NCache is used as the 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 use of asynchronous operations with NCache as NHibernate Second-level Cache.

  • ListAsyncretrieves query results asynchronously and allows cacheable query results to be stored in the configured query cache.
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

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.

  • 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

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top