Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9257911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:15:52+00:00 2026-06-18T12:15:52+00:00

I’m using the second level cache provider SysCache2 with Fluent NHibernate, using a standard

  • 0

I’m using the second level cache provider SysCache2 with Fluent NHibernate, using a standard fluent configuration (with query cache enabled as seems to be the general advice), and having table based dependencies defined in the usual manner.

Fluent:

config.Cache(c => c
 .ProviderClass<NHibernate.Caches.SysCache2.SysCacheProvider>()
 .UseQueryCache()
 .UseSecondLevelCache()
 .UseMinimalPuts()

)

Web.Config:

  <cacheRegion name="User" relativeExpiration="7200">
<dependencies>
  <tables>
    <add name="User" databaseEntryName="OldClient" tableName="tbl_users" />
  </tables>
</dependencies>

User Mapping:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("Users");
        ....
        // caching
        Cache.IncludeAll().ReadWrite().Region("Users");
    }
}

Everything runs as expected for single requests, ie:

Session<User>.Get()

as would be expected from the way that NHibernate caches by ID. Subsequent requests don’t hit the database, and invalidating the record in the database causes the Entity to be invalidated, resulting in a subsequent SQL call during the next request. All ok.

The problem is with query caching. In the first instance, everything works fine. Executing a call such as:

Session<User>.Query(item => item.Active == true) 

results in a SQL call as you would expect (SELECT * FROM Users WHERE Active = true). And subsequent executions result in no SQL. Great. Until such a time when a single record from the query set is changed in the database. Executing the same query then results in a SELECT N+1:

SELECT * FROM Users WHERE ID = 1
SELECT * FROM Users WHERE ID = 2
SELECT * FROM Users WHERE ID = 3
SELECT * FROM Users WHERE ID = 4
...

I’ve found references to this elsewhere, although no solutions:

StackOverflow – How do I make NHibernate cache fetched child collections? See the ‘one more point’ section

Ayende Caching Strategies he mentions ensuring ‘entities are also cached’ at the end

The only way I can avoid this at the moment is to clear the query cache after every request – which almost renders it useless. What I need to for the query cache to clear everything for a particular Entity when that entity is invalidated – not just the individual records.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:15:53+00:00Added an answer on June 18, 2026 at 12:15 pm

    According to both links you posted, the query cache caches the IDs resulting from the query and starts fetching entities one by one: if you have them in your cache, great, if not, bad luck, you now have many selects.

    At this point I presume that the entities that you are selecting are not in the second level cache (anymore?). I never used table based dependencies, we are using command based dependencies, and I just tested that if an entity is marked as invalid, only this cached entity, and not the whole cache region, is thrown out of the cache.

    Check the configuration to be sure that it is correct, maybe when you think you are hitting the second level cache you are actually hitting the session cache? Be sure that you are testing that the cache is working across different sessions.

    In any case I advise you to look into the DEBUG level logs of NHibernate and see what is actually happening with the second level cache, there you will be able to see all cache hits/misses. Another tip I can give you is to enable the generate_statistics flag in your configuration. Fluent:

    config.ExposeConfiguration(c => c.SetProperty("generate_statistics", "true"));
    

    After this you can access interesting data in the session factory such as Session.SessionFactory.Statistics.SecondLevelCacheMissCount and Session.SessionFactory.Statistics.SecondLevelCacheHitCount. With the combination of these two methods you should be able to pinpoint the cause of the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.