Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate 2nd Level Cache Provider Differences

I've been using NHibernate for a while now, I'm still wondering what the differences are between the the Second Level Cache Providers ?

  • Do some perform better\worse ?
  • What is popular and why ?

For clarity I'm talking about:

  • NHibernate.Caches.MemCache
  • NHibernate.Caches.Prevalence
  • NHibernate.Caches.SharedCache
  • NHibernate.Caches.SysCache
  • NHibernate.Caches.SysCache2
  • NHibernate.Caches.Velocity

and I'm sure there are others.

Thanks

like image 847
Mark Broadhurst Avatar asked Sep 27 '10 15:09

Mark Broadhurst


People also ask

How NHibernate cache works?

An NHibernate session has an internal (first-level) cache where it keeps its entities. There is no sharing between these caches - a first-level cache belongs to a given session and is destroyed with it. NHibernate provides a second-level cache system; it works at the session factory level.

What are the two levels of cache system?

Memory Systems Modern systems often use at least two levels of caches, as shown in Figure 8.16. The first-level (L1) cache is small enough to provide a one- or two-cycle access time. The second-level (L2) cache is also built from SRAM but is larger, and therefore slower, than the L1 cache.

What is second level data cache?

A second-level cache is a local store of entity data managed by the persistence provider to improve application performance. A second-level cache helps improve performance by avoiding expensive database calls, keeping the entity data local to the application.


1 Answers

Comparing these cache providers effectively boils down to comparing memcached vs prevalence vs Velocity, etc, and that is not really related to NHibernate.

Here are some reasons (by no means a complete list) to pick one over the others:

If you want to keep it simple and don't run your app in a farm, you might want to use SysCache/Prevalence, which runs in-proc. If you use MS SQL Server, use SysCache2.

If you need a huge cache across many cache-dedicated servers, you might want to use memcached, which can run on Linux so you'd avoid licensing costs.

If your application runs on Azure or already uses AppFabric, you might want to use Velocity.

Personally I prefer to do caching myself at a higher level than data access (only when really necessary), as to make caching more intentional and meaningful than just entities and to embrace more than data access in the cache. In a properly designed system, caching can be easily transparent using decorators or proxies.

like image 182
Mauricio Scheffer Avatar answered Nov 16 '22 00:11

Mauricio Scheffer