linq查询的nhibernate缓存问题

时间:2010-02-28 09:53:59

标签: nhibernate fluent-nhibernate

我看到了nhibernate缓存的一些奇怪的行为,无法理解推理。在执行

等选择操作时,我无法缓存查询
query.Select(x=>x).ToList() 

但可以在执行时缓存:

var query = session.Linq<Promoter>();
var p = query.ToList();

两者都产生相同的sql查询,应该做同样的事情。以下测试解释了问题。

    [Test]
    public void Can_Use_Cache()
    {
       ISessionFactory factory = Storage.Application.Get<ISessionFactory>("SessionFactory");
       // initial hit on the database and load into cache - all fine
        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 - all fine
       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 - not working 
       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 - again not working
       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();
       }
    }

我的测试结果显示第二个查询的数据库命中。查询3和4不应该命中db:

2010-02-28 12:05:23,046 INFO Started Logging

First Query
2010-02-28 12:05:23,156 DEBUG SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_
NHibernate: SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_

Second Query

Third Query
2010-02-28 12:05:23,315 DEBUG SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_
NHibernate: SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_

Fourth Query
2010-02-28 12:05:23,318 DEBUG SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_
NHibernate: SELECT this_.Id as Id11_0_, this_.Version as Version11_0_, this_.Name as Name11_0_ FROM Promoters this_

使用流畅的配置缓存:

SessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
                          .ConnectionString(ConfigurationService.SqlConnectionString)
                          .ShowSql()
                         .Cache(c => c

                                    .UseQueryCache()
                                    .ProviderClass(typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName))
                          )
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<EventMap>()
                               .Conventions.Setup(MappingConventions.GetConventions()))
            .ExposeConfiguration(BuildSchema)
            .BuildSessionFactory();

2 个答案:

答案 0 :(得分:2)

使用.Select()似乎是一个问题。选择(x =&gt; x)由于某种原因,使用select时会绕过缓存。任何其他语句都可以正常工作,如OrderBy(),Where()等

以下示例代码:

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
           }

答案 1 :(得分:0)