1

I query my NHibernate datasources using a Linq Expression. The problem I have with that is that no two invocations (or evaluations, sorry for the bad vocabulary) of the expression ever match in a comparison. As a consequence, NHibernate executes the same SQL with the same parameters way too often.

I shipped around this issue by using NHibernate Caches, fluently configured like so:

Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
                    .ConnectionString(c => c.Is(cStr))
                    .ShowSql())
                    .Mappings(x => x.FluentMappings.AddFromAssemblyOf<MyClassMap>())
                    .Cache(x=>x.UseQueryCache()
                               .ProviderClass("NHibernate.Cache.HashtableCacheProvider"))

I will take care of the provider later, so that this won't end up in production. Next, all my queries are denoted as being cacheable:

session.Query<MyClass>().Cacheable().Where(filter).ToList();

As far as I have read, caching queries happens in the second level cache, which has a one-per-SessionFactory scope. That, on the other hand, means that in order to reload my application data, I would have to reinstatiate the SessionFactory, which I think is much more costly than doing the same thing with sessions.

Is an efficient way to cache the queries without messing (not literally, y'know what I mean) with the SessionFactory?

Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60

1 Answers1

0

You can clear second level-cache (http://stackoverflow.com/questions/2660714/how-to-clear-the-entire-second-level-cache-in-nhibernate) without recreating SessionFactory but i am not sure that provided code clears cached queries too. And also there are methods to clear specific providers with their api. But as i know you have to manually clear cache only if you change data in db manually (not with NHibernate). If you change something with NH it will update cache itself. But if you change queried data very often than cache won't help you because it will have to be updated too often.

Nikolay
  • 3,658
  • 1
  • 24
  • 25
  • Thanks for the link. Well, not manually, but eventually with the same consequence: data is edited all across the company from different computers, each with their own SessionFactories and Sessions...if I wouldn't clear caches, I'd be working with possibly stale objects – Sebastian Edelmeier Mar 30 '12 at 09:39
  • 1
    @SebastianEdelmeier use a distributed cache then – Diego Mijelshon Mar 31 '12 at 13:20
  • @DiegoMijelshon are you referring to another product like NCache? – Sebastian Edelmeier Apr 02 '12 at 06:12
  • NHibernate can use different distributed caches (Microsoft Velocity, Memcached) as a second level cache. – Nikolay Apr 02 '12 at 06:17
  • @SebastianEdelmeier I'm not aware of a NCache provider for NHibernate. There are several available as part of [nhcontrib](http://sourceforge.net/projects/nhcontrib); one that yu can install easily is [memcached](http://memcached.org/), whose provider has a [nuget package](http://nuget.org/packages/NHibernate.Caches.MemCache) – Diego Mijelshon Apr 02 '12 at 11:43