thanks for the reply Fabio. I understand that the values are cached.
My issue is that the following:
           var query = session.Linq<Promoter>();
           query.QueryOptions.SetCachable(true);
           query.QueryOptions.SetCacheMode(CacheMode.Normal);
           var p = query.ToList();

will place the items into the cache as expected and subsequest queries
will use the cache. BUT the following will not hit the cache and
always hits the database:

           var query = session.Linq<Promoter>();
           query.QueryOptions.SetCachable(true);
           query.QueryOptions.SetCacheMode(CacheMode.Normal);
           var p = query.Select(x=>x).ToList();

Don't understand this behaviour. My fluent config:

                  Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                            
.ConnectionString(ConfigurationService.SqlConnectionString)
                            .ShowSql()
                            .Cache(c => c
                                .UseQueryCache()
                                
.ProviderClass(typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName))//
for test purposes
                              )
                .Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<EventMap>()
                                   
.Conventions.Setup(MappingConventions.GetConventions()))
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();

My Test:
        [Test]
        public void Can_Use_Cache()
        {
           ISessionFactory factory =
Storage.Application.Get<ISessionFactory>("SessionFactory");
           // initial hit on the database and load into cache
            using (var session = factory.OpenSession())
           {
               Console.WriteLine("");
               Console.WriteLine("First Query");
               var query = session.Linq<Promoter>();
               query.QueryOptions.SetCachable(true);
               query.QueryOptions.SetCacheMode(CacheMode.Normal);
               var p = query.ToList();
           }
            // no hit on the database and retrieved from cache as
expected
           using (var session = factory.OpenSession())
           {
               Console.WriteLine("");
               Console.WriteLine("Second Query");
               var query = session.Linq<Promoter>();
               query.QueryOptions.SetCachable(true);
               query.QueryOptions.SetCacheMode(CacheMode.Normal);
               var p = query.ToList();
           }
            // hits the db - should have come from the cache
           using (var session = factory.OpenSession())
           {
               Console.WriteLine("");
               Console.WriteLine("Third Query");
               var query = session.Linq<Promoter>();
               query.QueryOptions.SetCachable(true);
               query.QueryOptions.SetCacheMode(CacheMode.Normal);
               var p = query.Select(x=>x).ToList();
           }
           // hits the db again - should have come from the cache
           using (var session = factory.OpenSession())
           {
               Console.WriteLine("");
               Console.WriteLine("Fourth Query");
               var query = session.Linq<Promoter>();
               query.QueryOptions.SetCachable(true);
               query.QueryOptions.SetCacheMode(CacheMode.Normal);
               var p = query.Select(x => x).ToList();
           }
        }


On Feb 27, 10:49 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> what is in cache is the result of the query not the single entity
>
> 2010/2/27 Chuck Boyde <evilrab...@gmail.com>
>
>
>
> > Hi all
>
> > I have come across a problem and don't believe this is by design. I am
> > unable to get nhibernate to use the cache when using a query like:
>
> >  var acc = session.CreateQuery("from
> > Account").SetCacheable(true).List();
>
> > I have set the following in the config:
> >  <property
> > name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</
> > property>
> >    <property name="cache.use_second_level_cache">true</property>
> >    <property name="cache.use_query_cache" >true</property>
>
> > My test:
> >  [Test]
> >        public void Can_Cache_Query()
> >        {
> >            Console.WriteLine("1st query");
> >            using (var session = SessionFactory.OpenSession())
> >            {
> >                var acc = session.CreateQuery("from
> > Account").SetCacheable(true).List();
> >                acc.ShouldNotBeNull();
> >            }
> >            Console.WriteLine("2nd query");
> >            using (var session = SessionFactory.OpenSession())
> >            {
> >                var acc = session.CreateQuery("from
> > Account").SetCacheable(true).List();
> >                acc.ShouldNotBeNull();
> >            }
> >        }
>
> > unless I use session.Get, which works fine, the query hits the db
> > instead of the cache. This is ok:
>
> >  [Test]
> >        public void account_should_be_in_second_level_cache()
> >        {
> >            using (var session = SessionFactory.OpenSession())
> >            {
> >                Console.WriteLine("--> Now loading account");
> >                var acc = session.Get<Account>(newAccount.Id);
> >                acc.ShouldNotBeNull();
> >                acc.Name.ShouldEqual(newAccount.Name);
> >            }
> >        }
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhus...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+unsubscr...@googlegroups.com<nhusers%2bunsubscr...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.
>
> --
> Fabio Maulo

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

Reply via email to