SysCache2 and FluentNhibernate on MVC aplication

440 views Asked by At

I am having a problem with SysCache/SysCache2 on my MVC application. My configuration seems to be correct. I have set it up just like countless examples on the web.

On my class I have put: Cache.Region("LongTerm").NonStrictReadWrite().IncludeAll(); Here is a Test I made for the application cache.

[Test]
        public void cache()
        {
            using (var session = sessionFactory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                var acc = session.QueryOver<Log>().Cacheable().List();
                tx.Commit();
            }

            var test = sessionFactory.Statistics.SecondLevelCacheHitCount;

            using (var session = sessionFactory.OpenSession())
            {
                var acc = session.QueryOver<Log>().List();
            }

            var test1 = sessionFactory.Statistics.SecondLevelCacheHitCount;
        }

The first statement is cached as I see in the session factory statistics (for example 230 records). If i understand it right second statement that is below shouldnt hit the db but the Cache. Problem here is that it goes to DB anyway. Checked with profiler to be 100% sure.

I don't know what am I doing wrong here. Anyone has an idea?

1

There are 1 answers

1
AdrianCogiel On BEST ANSWER

I have managed to solve this problem. It had to do with my session creation. I didn't use session per request which triggered not going to cache. I created transaction on begining and it lasted through entire session. I managed to trigger entering cache if i opened the session again within using mark like: using(var sess = session.SessionFactory.OpenSession()) but this solution was only a workaround which didn't suit me so I changed how I created sessions in the first place and it works fine now! :)