This project is read-only.

NHibernate Second Level Cache

Topics: Administration, Customizing Orchard, General, Writing modules
Jul 1, 2011 at 8:28 PM

Just wondering if the team has looked into utilizing the NHibernate second level cache.  I played around with it some today and got it working on the 1.2 release.  There were some issues with the unused ContentPartRecord proxies on ContentItemRecord or ContentItemVersionRecord that I had to put a hacked workaround in for, but overall it seems like it's pretty solid.  It makes a big difference on the number of queries...on the "Welcome to Orchard" page with the default recipe, on the second page load (primed cache) 25 out of the 36 queries are loaded from the 2nd level cache for an authenticated user, and 19 out of 28 queries are loaded for an authenticated user.

Please let me know if you would like me to submit a patch for the work I've done.

Jul 1, 2011 at 9:04 PM

Excellent! Louis tells me that yes, they investigated that and hit the same roadblocks that you did, which is why it never went in. The reasoning was that we would move to nHib 3 anyways, where those hacks will be unnecessary. The move to nHib 3 should be happening very soon but in the dev branch (that means Orchard 2.0).

Still, it would be useful to submit this patch.


Jul 1, 2011 at 11:16 PM

Alright cool, I created a work item at with the files attached.  Hit me up if you have any questions.

Jul 1, 2011 at 11:18 PM

Also, I'm curious to know what features in NHib 3 make it so that these hacks are unnecessary? I haven't gotten a chance to dig in too far with nh 3, but I used 2 quite heavily.

Jul 2, 2011 at 12:38 AM

I'll let Louis answer that.

Jul 2, 2011 at 1:20 AM

Nothing known specifically, just a general sense or assumption that figuring out a workaround before making the NH2-3 jump would be adding another twist that wouldn't hop up cleanly. (The contentitem/part record relationship is already a sticking point in the upgrade.)

That said, since you've figured out how to make it work, thanks! That seems like a great thing to have if any 1.x sites want to retrofit that in.


Jul 7, 2011 at 8:05 AM

Since it sounds like the team isn't planning on integrating this in 1.x, I packaged this up into a module for people to try out.  I'm not putting it into the gallery because there are a couple big caveats: 1) It hasn't been fully tested, and 2) I can almost guarantee that it will blow things up when anyone tries to upgrade to v2.  It should be easy to remove, it doesn't create anything persistent, it just overrides a couple of the core NHibernate config services.  Let me know if anyone tries it out.

Jul 12, 2011 at 6:04 PM

I wouldn't let those caveats stop you - that sounds like a fine thing to have in the 1.x gallery. Buyer-beware is a perfectly fine state for a young module.


Jul 12, 2011 at 7:52 PM

Louis - I also was able to get the query cache working locally.  In my previous experience, I had trouble with it being smart enough to know when to flush the cache for a specific query, however I think the rigid structure of orchard helped it to work perfectly in my testing.  This got the queries down to about 5% of the default codebase, and it seemed to be spot on in terms of invalidating queries when needed. 

I would love to include this in my module, however I can't for the life of me figure out how to override the DefaultContentQuery.  It's registered completely differently than the other dependencies I've found, see here.

I tried everything I could think of, including creating my own autofac module, but can't for the life of me get orchard to pick up my implementation.

Can you or someone else help me to figure out how to override it?  Thanks so much.

Luke Hertert

Jul 22, 2011 at 9:08 PM

If you'd like I'd say feel free to push out an early module with just the caching by entity identity, even without query caching that would be very nice to have.

For the query it might be interesting to see what sort of alterations or information you need - I'm wondering if the default query could have some more event hooks to allow this type of deep participation/alteration between any number of modules?

Cheers, Lou

Jul 22, 2011 at 9:17 PM

Honestly, I tossed the code because I couldn't get it to work, but it would be simple enough to do again.  But I'm probably not going to spend the time on that unless it's going to able to be used...which brings me back to the question I was asking.  How do I override DefaultContentQuery?  I think someone just asked a very similar question here: