Rohit
Rohit

Reputation: 3638

NHibernate cache expiration

I use custom developed ORM currently and am planing to move to nhibernate.

Currently, I use both L1 - session level caching and L2 - Application level caching.

Whenever an object is requested from L2 cache by L1 cache, it checks database for modified since last load, and loads only if it has been modified.

Can I do this with NHibernate. In short, caching does not hurt me as it always gets most recent data and saves me object creation and load times.

Upvotes: 7

Views: 4443

Answers (2)

Ravian
Ravian

Reputation: 64

The build-in Level1 cache in NHibernate is not very sophisticated as it stand alone and in-proc in nature. So you definitely need to have a second level cache in order to enhance the performance of the NHibernate app. It reduces time taking trips to database. There are many third party integrations available that plug in for NHibernate secondary level cache. NCache is one fine example of it where no code change is required. Read more from here,

http://www.alachisoft.com/ncache/nhibernate-l2cache-index.html

Upvotes: 0

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99720

IMHO it's pointless to have an L2 cache if it needs to hit the DB anyway. That's precisely the entire point of caching, avoid hitting the DB as much as possible.

AFAIK there is no caching strategy implemented like the one you describe, but NHibernate L2 caches are entirely pluggable so you could implement it. However, I wouldn't, for the reasons I mentioned above.

Getting outdated data is only an issue if there are other apps or other DALs hitting the same DB besides NHibernate. If that's the case, you could use the SysCache2 implementation, which internally uses SqlCacheDependencies to invalidate cache regions when data in the underlying table changes.

If it's a single app running in a farm, use the Velocity provider.

If there's only one NHibernate app instance hitting the DB, any cache strategy will do and you don't have to worry about getting outdated data.

See also:

Upvotes: 14

Related Questions