Question

I am trying to figure out how to cache a joined query using nhibernate and it doesn't seem like its working properly

Here is my code:

   public CacheTestViewModel GetCacheTestViewModel()
    {
        var vm = new CacheTestViewModel();
        var session = Repository.Session;
        using (var tx = session.BeginTransaction())
        {
            vm.Projects = Repository.Session.Query<Project>()
                .FetchMany(r=>r.ProjectApplications)
                .ThenFetch(r=>r.Application)
                .Cacheable().CacheMode(CacheMode.Normal)
                .ToList();

            tx.Commit();
        }
        return vm;
    }

I run this over and over again and it seems to be loading the Project objects from the second level cache but it still goes back to the db to query the ProjectApplication objects which is quite slow

Is it possible for nhibernate to cache this entire query so the whole graph get returned from cache?

NOTE: I do have the query cache turned on as well as all of the entities set with Cache.ReadWrite()

Here is my cache configuration

       return configuration
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ApplicationMap>().Conventions.Add(typeof(Conventions)))
            .ExposeConfiguration(
                c => {
                   // c.SetProperty("proxyfactory.factory_class", proxyFactory);
                    c.SetProperty("cache.provider_class", "NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache");
                    c.SetProperty("cache.use_second_level_cache", "true");
                    c.SetProperty("cache.use_query_cache", "true");
                    c.SetProperty("expiration", "86400");
                })
            .BuildSessionFactory();
Was it helpful?

Solution

Maybe you´re having some problem with the configuration of your cache provider. I´ve been able to do want you want using Couchbase as 2nd level cache provider, as described here:

http://blog.couchbase.com/introducing-nhibernate-couchbase-2nd-level-cache-provider

If your deployment enviroment is on Azure, i guess this might be useful. Note that The SysCache module can’t co-exist with the AzureMemcached module.

http://www.webmoco.com/webmoco-development-blog/orchard-cms-second-level-caching

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top