29

Does anyone have an example how to set up and what entities to cache in fluent nhibernate. Both using fluent mapping and auto mapping?

And the same for entity relationships, both one-to-many and many-to-many?

Dio F
  • 2,458
  • 1
  • 22
  • 39
Emil C
  • 1,315
  • 4
  • 15
  • 27

1 Answers1

33

I have been working a a similar situation, where I just want to cache specific elements, and want these elements to be loaded once on start up, and kept in cache, until the application is shut down. This is a read only cache, and is used to populate a list of countries, so that a user can select their country from the list.

I used fluentNhibernate Mappings, and defined Country my class with Cache.readonly()

public class CountryMap : ClassMap<Country> {
    public CountryMap() { 
         Schema("Dropdowns");
         Cache.ReadOnly();
         // Class mappings underneath 
    }
}

My user class map looks like this:

public class UserMap : ClassMap<User> {
    Id(x => x.Id).Column("UserId");
    Map(x => x.FirstName);
    Map(x => x.LastName);
    References(x => x.Country)
      .Column("CountryId");
}

I manually configure Fluent Nhibernate to use Second level cache. So in my fluent Confuguration I have:

var sessionFactory = Fluently.Configure()
    .Database (...) // set up db here
    .Mappings(...)  //set up mapping here
    .ExposeConfiguration(c => {
        // People advice not to use NHibernate.Cache.HashtableCacheProvider for production
        c.SetProperty("cache.provider_class", "NHibernate.Cache.HashtableCacheProvider");
        c.SetProperty("cache.use_second_level_cache", "true");
        c.SetProperty("cache.use_query_cache", "true");
    })
    .BuildSessionFactory();

I have checked in SQL profiler, and when I get a list of countrys for a user, the are loaded once, and I get cache hits after every other request. The nice thing is that when displaying the users country name, it loads from the cache, and does not make a request to the database. I got some tips from this posting by Gabriel Schenker. Hope that helps? If you found a better/proper way, please let me know? Thanks!

tinonetic
  • 7,751
  • 11
  • 54
  • 79
Dai Bok
  • 3,451
  • 2
  • 53
  • 70
  • Good answer! I've done what you did successfully too. I prefer Ayende's NhProf for profiling, much better than SQL profiler, but it's not free (there is a trial) and it's NHibernate specific. – James Allen Nov 24 '09 at 22:16
  • Yip, I have seen some screensots of NHProf, looks good. Might give it a try before we go live. – Dai Bok Nov 25 '09 at 09:12
  • 2
    Gabriel Schenker post link appears to be broken now. – longda Sep 04 '11 at 23:13
  • think the URL might have changed to http://nhibernate.hibernatingrhinos.com/28/first-and-second-level-caching-in-nhibernate – Dai Bok Sep 06 '11 at 10:55
  • Do you see anything in the NHProfiler when you get cache hits? Or you just don't see any more select statements for the country data? (No selects means it must be using cache) – Jess Jan 22 '15 at 21:35
  • Sorry I cant remember, but I think you got a notification that you had a cache hit. Why don't you ask the question – Dai Bok Jan 23 '15 at 15:04
  • Is this all of the configuration needed? I thought I remember seeing someplace that in order to utilize NHibernate caching there had to be some config file changes/additions, and SqlDependency had to be used (I'm not able to use SqlDependency via AppHarbor) – ganders Mar 05 '15 at 15:25
  • @ganders I am a bit out of date with this. Currently this is the current configuration on our test/demo servers. – Dai Bok Mar 06 '15 at 15:21