0
votes

i'm trying to get my head around 2nd Level Caching in Fluent NHibernate. So far i have done the following:

  1. Added a reference to the caching dll
  2. Added the following when i create my session factory:

    c.SetProperty("cache.provider_class", "NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache");

    c.SetProperty("cache.use_second_level_cache", "true");

    c.SetProperty("cache.use_query_cache", "true");

  3. Added Cache.ReadWrite(); against all entities i wish to cache in ClassMap file e.g.

    public class CountryMap : ClassMap { public CountryMap() { Table("Countries"); Id(x => x.CountryID); Map(x => x.CountryName); Cache.ReadWrite(); } }

Now i assumed that anytime i try to get an entity which has Cache.ReadWrite() in the mapping it would cache it for the duration of the session factory (singleton). However it appears that this is not the case as the database is hit on every request. Here's a couple questions i have:

  • What does the CacheMode property on the session do? My session lasts for the duration of a web request. Should i set this when i create the session and if so what should i set it to?
  • I read somewhere that the cache is not updated unless a commit is made. I only commit data when i'm inserting/updating information in the database. For example if i have a simple page with a list of countries i would grab all the data needed but wouldn't commit the data as i don't need to make any changes. Does this mean this data won't be cached? I'm sure i've read this wrong as this would mean data is only cached once it is added the database.

I'd appreciate it if you could help. Thanks

2

2 Answers

3
votes

Here is a very good explanation for second level cache with nhibernate.

1
votes

You have to use transactions for the 2nd level cache to be used.