Alachisoft.com
Download NCache Now!
Contact Us
Live Chat

NCache as NHibernate Second Level Cache for Scalability

NHibernate is a very popular object-relational mapping engine for .NET. It speeds up your development by relieving you from developing a significant portion of relational data persistence code.

NHibernate is frequently used in high transaction applications where performance and scalability are critical. To handle this, NHibernate provides built-in First Level (L1) cache that is in-process and tied to a user’s “Session Object”. NHibernate also provides a Second Level (L2) cache framework at “Session Factory” level (meaning at application level) where you can plug-in third party caches.

NCache is an extremely fast and scalable in-memory distributed cache for .NET applications. It lets you cache application data, reduce those expensive database trips, and improve your application performance and scalability. And, NCache provides an NHibernate Second Level Cache implementation. Please see details of NCache on NCache Homepage.

Why Use NCache as NHibernate Second Level Cache?

If your NHibernate application is running in a multi-server environment then you need distributed cache as a NHibernate Second Level Cache provider. And, NCache is an ideal NHibernate Second Level Cache for multi-server configurations and you should use it for these reasons:

  • NCache synchronizes across servers: NCache ensures that its cache is consistent across multiple servers and all cache updates are synchronized correctly so no data integrity issues arise.
  • Cache size can grow: NCache pools memory of all cache servers together. So, you can grow the cache size by simply adding more servers to the cache cluster.
  • Linearly scale to handle extreme transaction loads: Database quickly becomes a bottleneck as you try to increase transaction load. NCache allows you to add more servers to the cache cluster as you need more transaction load so the cache is never a bottleneck.
  • Cache reliability through data replication: NCache lets you intelligently replicate cached data across multiple servers and without compromising performance. So, you won't lose any data even if a cache server goes down.

Use NCache as NHibernate Second Level Cache without Programming

The best thing about using NCache with NHibernate is that there is no programming required on your part. You simply modify your app.config and specify NCache as your Second Level Cache provider. Here is an example of app.config changes:

    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    ...
    <session-factory>
        <property name="cache.provider_class">
            Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheProvider,
            Alachisoft.NCache.Integrations.NHibernate.Cache
        </property>
    ...
    </session-factory>
   </hibernate-configuration>

NCache Features for NHibernate Second Level Cache

By plugging in NCache NHibernate Second Level Cache provider through app.config changes, you are going to gain an enterprise level distributed cache for your application. Here are some features NCache provides for NHibernate apps.

  1. Synchronize cache with database: Some data that is in the cache may be changed in the database without your application’s knowledge or involvement. And, for this data, you can specify the corresponding classes for database synchronization feature of NCache.  This allows NCache to then connect with the database, monitor data changes, and then update the cache automatically to ensure that data in the cache is always synchronized with the database. NCache provides SqlDependency for SQL Server, OracleDependency for Oracle, and DbDependency for any OLEDB compliant databases.
  2. Use Async operations: Many NCache operations have their asynchronous equivalent. These operations are useful when your application does not want to wait for the cache to be updated on multiple servers and wants the control returned to it immediately and the cache update to be done in the background. You can enable this feature with NHibernate as well.
  3. Priority for cache items: NCache provides priority based evictions where cached items are evicted based on their priority. So, higher priority items are evicted in the end and in fact you can specify a priority that does not get evicted at all. You can enabled this feature through app.config changes for NHiberante.
  4. Absolute Expiration: Absolute expiration in NCache is specified separately for each cached item and is a date-time value when NCache automatically expires the item. For NHibernate, NCache asks you to specify an “interval” value and then uses “Now() + interval” formulate to calculate the date-time value for absolute expiration.
  5. Sliding Expiration: Sliding expiration in NCache is specified separately for each cached item and is an interval value. NCache expires the cached item if it has neither been fetched nor updated for this interval. You can specify this interval through app.config for NHiberante.
  6. Dynamic Compact Serialization: Whenever any .NET object is cached in an out-of-process or distributed cache, it must first be serialized. And, regular .NET serialization is slow because it uses .NET Reflection at runtime. NCache provides a way for you to register your classes with NCache and NCache generates serialization code when your application connects to the cache. NCache then compiles this code in memory and uses it for serialization. This is almost 10 times faster than regular .NET serialization. And, you can use this feature with NHibernate. Just register your NHibernate Entity classes with NCache.

Use NCache Directly from NHibernate Apps

Although, using NCache as NHibernate Second Level Cache is very quick and easy, it only provides you a limited set of NCache features. The reason for this is that NCache is bound by NHibernate Second Level Cache provider specification that is based on least common denominator principle. There are numerous NCache features that you are unable to use if you decide to use NCache as NHibernate Second Level Cache Provider.

The alternative for you is to use NCache directly from within your NHibernate application by making NCache API calls like all other non-NHibernate .NET applications do. Although, there is a small programming effort to take this path, you might decide that the benefits outweigh the extra programming effort.

NCache Features Missed in NHibernate Second Level Cache

Below is a list of features you would miss if you used it as NHibernate Second Level Cache versus if you used NCache directly from your application.

  1. Bulk operations
  2. Cache Dependencies
    • Key based
    • File based
    • Custom
  3. Groups and subgroups
  4. Tags
  5. Named Tags
  6. Object Query Language (OQL) and LINQ based searches
  7. Lock and Unlock of cached items
  8. Cache item versioning
  9. Streaming API
  10. Read-through, Write-through, and Write-behind
  11. Event Notifications
    • Item based (update and remove)
    • General purpose (add, update, remove)
    • Custom
  12. Continuous Query
  13. Runtime Data Sharing
  14. Cache Loader

What to Do Next?