NHibernate is a popular object-relational mapping (ORM) solution for .NET applications because it simplifies your database programming. As a result, many high-traffic applications use NHibernate and, thus, face scalability bottlenecks in the database.
To tackle this, NHibernate provides a caching infrastructure so that applications can use an in-memory cache store instead and prevent exhausting their databases with such a high request load. NHibernate caching includes first-level cache and second-level cache.
NCache Details NHibernate Caching-Docs Using Dapper Micro-ORM with NCache
NHibernate First Level Cache and its Limitations
NHibernate First Level (1st level) Cache provides a basic standalone (in-proc) cache associated with the session object and is limited to the current session only. By default, this 1st level cache reduces the number of SQL queries on the database per session. But, there are a lot of limitations with this 1st level cache, as follows:
- Each process has its own 1st level cache that is not synchronized with other 1st level caches making data integrity a struggle.
- Cache size is limited to the process memory and cannot scale.
- Worker process recycling causes the cache to be flushed. Then, you have to reload it, which reduces your application performance.
NCache Details Synchronize DB with NHibernate NHibernate Query Caching
Solution: A Distributed Cache for NHibernate
NHibernate Second Level Cache exists at the Session Factory level, which means multiple user sessions can access a shared cache. Additionally, NHibernate 2nd level cache has a plug-able architecture so you can plug-in a third party distributed cache to it without any programming. NCache has implemented NHibernate Second Level Cache Provider and you can use it as your distributed cache for NHibernate without any code changes.
Following example shows you how to use NCache in your NHibernate application as its 2nd level cache:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<hibernate-configuration> <session-factory> <property name="cache.provider_class">Alachisoft.NCache.Integrations.NHibernate.Cache.NCacheProvider, Alachisoft.NCache.Integrations.NHibernate.Cache </property> <property name="proxyfactory.factory_class"> NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate </property> <property name="cache.use_second_level_cache">true </property> </session-factory> </hibernate-configuration> <ncache> <cache-regions> <region name="AbsoluteExpirationRegion" cache-name="myCache" priority="Default" expiration-type="sliding" expiration-period="180" use-async="false"> <region name="default" cache-name="myCache" priority="default" expiration-type="none" expiration-period="0"> </region></region></cache-regions> </ncache> |
Here are some benefits of NCache as NHibernate 2nd level cache:
- NCache synchronizes across processes & servers: NCache is a cache shared across multiple servers. This ensures that the cache is consistent across multiple servers and that all cache updates are synchronized correctly, avoiding data integrity issues.
- Scalable cache size: NCache pools the memory of all cache servers together. So, you can grow the cache size by simply adding more servers to the cache cluster. This means your cache can grow to 100′s of gigabytes and even terabytes.
- Application process recycling does not affect the cache: Your application processes become stateless as all the data is in an out-proc distributed cache. So, application process recycling has no impact on the cache.
- Linear scalability to handle larger transaction loads: You can manage larger transaction loads without worrying about your database becoming a bottleneck. This performance is possible because the distributed cache scales linearly and reduces your database traffic by as much as 90%.
- Use Client Cache to store data In-proc: NCache provides support for client cache, a synchronized local cache, or cache-on-top-of-a-cache, that runs within your worker process. You can use it to attain more performance within your NHibernate applications.
NCache Details NHibernate & NCache Use NHibernate Cache-Video
Conclusion!
As you can see, a distributed cache like NCache enables you to use NHibernate and still run your application in a multi-server configuration. You can also scale your application and handle high transaction loads by using NCache as NHibernate Second Level Cache. Download a fully working 60-day trial of NCache Enterprise and try it out for yourself.