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

  • SEARCH
  • Home
  • 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 6979859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:56:22+00:00 2026-05-27T17:56:22+00:00

I am just doing some experiments on Castle AR and 2nd level cache of

  • 0

I am just doing some experiments on Castle AR and 2nd level cache of NH. In the following two methods, I can see caching working fine but only for the repetition of the call of each. In other words if I call RetrieveByPrimaryKey twice for same PK, the object is found in cache. And if I call RetrieveAll twice, I see SQL issued only once.

But if I call RetrieveAll and then RetrieveByPrimaryKey with some PK, I see two SQL statements getting issued. My question is, Why AR does not look for that entity in cache first? Sure it would have found it there as a result of previous call to RetrieveAll.

    public static T RetrieveByPrimaryKey(Guid id)
    {
        var res = default(T);
        var findCriteria = DetachedCriteria.For<T>().SetCacheable(true);
        var eqExpression = NHibernate.Criterion.Expression.Eq("Id", id);
        findCriteria.Add(eqExpression);
        var items = FindAll(findCriteria);
        if (items != null && items.Length > 0)
            res = items[0];
        return res;
    }

    public static T[] RetrieveAll()
    {
        var findCriteria = DetachedCriteria.For<T>().SetCacheable(true);
        var res = FindAll(findCriteria);
        return res;
    }
  • 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-05-27T17:56:23+00:00Added an answer on May 27, 2026 at 5:56 pm

    You’re using caching on specific queries. that means that cache lookup is done in the following way:
    search the cahce for results of a query with identical syntax AND the same parameters. If found- use cached results.

    nHibernate (this has nothing to do with AR, by the way) doesn’t know that logically, one query ‘contains’ the other. so this is why you’re getting 2 db trips.

    I would suggest using ISession.Get to retreive items by ID (it’s the recommended method). I think (not tested it though) that Get can use items cached by other queries.
    here’s a nice blog post from ayende about it.

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

Sidebar

Related Questions

So I'm working on just a learning project to expose myself to doing some
Just doing some work with RIA services and I see in the MSDN documentation
I was just doing some stuff and wrote this program. I got the following
I've been doing some experiments and here's something I found. Consider the following C
Just doing some testing/prototyping with ClickOnce. Does anyone know why I can publish with
I've just started doing some PowerShell scripting, and I'm running into a problem testing
I've just started doing some real-world performance testing on my Fluent NHibernate / SQLite
I am doing some significant refactoring and feature-adding on a project, and have just
i'm currently doing some reports for SSRS, and i just confirm what i already
I've just installed the 3.5 .NET Framework and while doing some cleaning I've notice

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.