Seems to be a problem with using .Select() as in .Select(x=>x) For
some reason the cache is bypassed when using the Select statement. Any
other statements work fine like OrderBy(), Where() etc

Sample code below:

    using (var session = factory.OpenSession())
               {
                   Console.WriteLine("");
                   var query = session.Linq<Promoter>();
                   query.QueryOptions.SetCachable(true);
                   query.QueryOptions.SetCacheMode(CacheMode.Normal);
                   var p = query.OrderBy(x => x.Name).ToList();//
works fine
                   //var p = query.OrderBy(x =>
x.Name).Select(x=>x).ToList();// will hit the db
               }



On Feb 28, 11:44 pm, Chuck Boyde <[email protected]> wrote:
> Just downloaded latest build of Linq and same problem. Here is the
> test:
>
> [Test]
>         public void Can_Cache()
>         {
>             #region Works
>             using (var session = SessionFactory.OpenSession())
>             {
>                 Console.WriteLine("1st Query");
>                 var query = session.Linq<Promoter>();
>                 query.QueryOptions.SetCachable(true);
>                 query.QueryOptions.SetCacheMode(CacheMode.Normal);
>                 var p = query.ToList(); // load into cache
>                 foreach (var promoter in p)
>                 {
>                     Console.WriteLine(promoter.Name);
>                 }
>             }
>             using (var session = SessionFactory.OpenSession())
>             {
>                 Console.WriteLine("2nd Query");
>                 var query = session.Linq<Promoter>();
>                 query.QueryOptions.SetCachable(true);
>                 query.QueryOptions.SetCacheMode(CacheMode.Normal);
>                 var p = query.ToList(); // gets from the cache
>                 foreach (var promoter in p)
>                 {
>                     Console.WriteLine(promoter.Name);
>                 }
>             }
>             #endregion
>
>             #region Does not Work as Expected
>             using (var session = SessionFactory.OpenSession())
>             {
>                 Console.WriteLine("3rd Query");
>                 var query = session.Linq<Promoter>();
>                 query.QueryOptions.SetCachable(true);
>                 query.QueryOptions.SetCacheMode(CacheMode.Normal);
>                 var p = query.OrderBy(x => x.Name).Select(x =>
> x).ToList(); // hit the db
>                 foreach (var promoter in p)
>                 {
>                     Console.WriteLine(promoter.Name);
>                 }
>             }
>             using (var session = SessionFactory.OpenSession())
>             {
>                 Console.WriteLine("4th Query");
>                 var query = session.Linq<Promoter>();
>                 query.QueryOptions.SetCachable(true);
>                 query.QueryOptions.SetCacheMode(CacheMode.Normal);
>                 var p = query.OrderBy(x => x.Name).Select(x =>
> x).ToList(); // hits the db and not cache
>                 foreach (var promoter in p)
>                 {
>                     Console.WriteLine(promoter.Name);
>                 }
>             }
>             #endregion
>         }
>
> On Feb 27, 10:49 pm, Fabio Maulo <[email protected]> wrote:
>
> > what is in cache is the result of the query not the single entity
>
> > 2010/2/27 Chuck Boyde <[email protected]>
>
> > > 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 [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<nhusers%[email protected]>
> > > .
> > > 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 [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