【问题标题】:Fluent nHibernate - Query Cache not working with "session.Query" and Cacheable()Fluent nHibernate - 查询缓存不适用于“session.Query”和 Cacheable()
【发布时间】:2012-11-04 16:22:29
【问题描述】:

没有得到任何 QueryCacheHitCount。源代码如下所示: nHibernate 3.3.1.4000,FluentnHibernate 1.3.0.733

配置:

Factory = Fluently.Configure()
    .ExposeConfiguration(c =>
        c.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, "true"))
    .Database(
    MsSqlConfiguration.MsSql2008.ConnectionString(c => c.Is("DATA SOURCE=localhost;PERSIST SECURITY INFO=True;USER ID=AAA;Password=AAA"))
            .ShowSql()
    )
    .Mappings(x => x.FluentMappings.AddFromAssemblyOf<Localization.NHibernate.Article>())
    .ExposeConfiguration(BuildDatabase)
    .Cache(
        x => x.UseSecondLevelCache()
                .UseQueryCache()                         
                .ProviderClass<NHibernate.Cache.HashtableCacheProvider>())
    .BuildSessionFactory();

执行:

using (var tx = session.BeginTransaction())
{
    Factory.Statistics.Clear();

    for (int i = 0; i < 10; i++)
    {
        Article s = session.Query<Article>().Cacheable().Where(x => x.Name == "O").SingleOrDefault();
    }

    Console.WriteLine(Factory.Statistics.QueryCacheHitCount);
    Console.WriteLine(Factory.Statistics.SecondLevelCacheHitCount);
    Console.WriteLine(Factory.Statistics.QueryExecutionCount);
}

只有当我更改缓存配置并添加我的私人QueryCacheFactory.QueryCacheFactory&lt;CacheFactory&gt;() 才有效(但我忽略了IsUpToDate 检查)

public class CacheFactory : NHibernate.Cache.IQueryCacheFactory
{
    public NHibernate.Cache.IQueryCache GetQueryCache(string regionName, NHibernate.Cache.UpdateTimestampsCache updateTimestampsCache, Settings settings, IDictionary<string, string> props)
    {
        return new MyStandardQueryCache(settings, props, updateTimestampsCache, regionName);
    }

    private class MyStandardQueryCache : NHibernate.Cache.StandardQueryCache
    {
        public MyStandardQueryCache(Settings settings, IDictionary<string, string> props, NHibernate.Cache.UpdateTimestampsCache updateTimestampsCache, string regionName)
            : base(settings, props, updateTimestampsCache, regionName) { }

        protected override bool IsUpToDate(Iesi.Collections.Generic.ISet<string> spaces, long timestamp)
        {
            return true; // SET TO TRUE than it's hitting the cache
        }
    }
}

【问题讨论】:

    标签: nhibernate caching fluent


    【解决方案1】:

    我有一个类似的问题,据我记得,查询的结果是使用会话或事务的创建时间缓存的。尝试对第一个查询使用一个会话,并为以下查询使用不同的会话,这样做您应该会收到缓存命中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多