Hi,

I'm trying to wrap my head around the caching in NHibernate,
specifically the QueryCache. In a small test I've setup a second level
cache which seems to be working just fine with entities,
but there are some cache-misses with the QueryCache that I don't
really understand, consider the following code where we'll be using 4
different sessions (Session 1 will just create the database):

------------------------
// Session2 here will persist 3 Survey-entities (and cascade save 3
children for each Survey-entity)
ISurveyRepository surveyRepository = new SurveyRepository(session2);
surveyRepository.Transact(() => {
        surveyRepository.SaveOrUpdate(s1);
        surveyRepository.SaveOrUpdate(s2);
        surveyRepository.SaveOrUpdate(s3);
});
surveyRepository.GetSurveysWithNameLike("Sur"); // I would assume that
after this call the query gets put in the cache, no?
surveyRepository.GetSurveysWithNameLike("Sur"); // Cache miss, why?
surveyRepository.GetSurveysWithNameLike("Sur"); // Cache miss, why?
// See NHProf capture log at: http://dl.dropbox.com/u/5154149/Session2.PNG

// Let's continue and open a new session, and see what ahppe
ISurveyRepository surveyRepository2 = new SurveyRepository(session3);
surveyRepository2.Get(s1.Id);                    // Cache hit, 2nd level enity 
cache
is working atleast.
surveyRepository2.GetSurveysWithNameLike("Sur"); // Cache miss again!
why?
surveyRepository2.GetSurveysWithNameLike("Sur"); // Cache hit
surveyRepository2.GetSurveysWithNameLike("Sur"); // Cache hit
// See NHProf capture log at: http://dl.dropbox.com/u/5154149/Session3.PNG

// Let's try one last time, with a new session:
ISurveyRepository surveyRepository3 = new SurveyRepository(session4);
surveyRepository3.Get(s1.Id);                    // Cache hit
surveyRepository3.GetSurveysWithNameLike("Sur"); // Cache hit
surveyRepository3.GetSurveysWithNameLike("Sur"); // Cache hit
surveyRepository3.GetSurveysWithNameLike("Sur"); // Cache hit
// See NHProf capture log at: http://dl.dropbox.com/u/5154149/Session4.PNG
// So after 2 sessions, the query seeems to be in the cache properly.
-----------------------

The code in the repostitory is something like this:
public IList<Survey> GetSurveysWithNameLike(string surveyName)
{
        return Transact(() => {
                return this.Session.QueryOver<Survey>()
                                        .WhereRestrictionOn(s => s.Name)
                                        .IsLike(surveyName, MatchMode.Anywhere)
                                        .Cacheable()
                                        .List<Survey>();
        });
}


Am I missing something, or is this the way it's supposed to work? Why
isn't the query cached after the first call? It's not even cached in
the first session, why?
The closest hint I've found is on Ayende's blog:
http://ayende.com/Blog/archive/2008/01/24/NHibernate-and-the-second-level-cache-tips.aspx,
where he mentions how the timestamps work. Relating to this test, it
would mean that my queries in "Session2" would never get cached,
hence the first call to the repository in Session 3 would be the one
putting the query in the QueryCache. But if that's the case, then why
is it working with the EntityCache?

Any hints?

Cheers!

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to