NHibernate,二级缓存不与IQuery一起工作

NHibernate,二级缓存不与IQuery一起工作,nhibernate,Nhibernate,我使用的是二级缓存,我使用的是 默认哈希表,因为我正在测试这个特性。 我已按以下方式配置了hibernate.cfg.xml: <property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</ property> <!-- You have to explicitly enable the second level cache --> <

我使用的是二级缓存,我使用的是 默认哈希表,因为我正在测试这个特性。 我已按以下方式配置了hibernate.cfg.xml:

    <property
name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</
property>
    <!-- You have to explicitly enable the second level cache -->
    <property name="cache.use_second_level_cache">true</property>
    <!-- cache will expire in 2 minutes -->
    <property name="cache.default_expiration">120</property>
    <!-- I want also to cache queries -->
    <property name="cache.use_query_cache">true</property>
    <class
          name="CachableProduct"
          table="[CachableProduct]"
          dynamic-insert="true"
          dynamic-update="true">
        <cache usage="read-write"/>
        <id name="ProductId">
          <generator class="guid.comb" />
        </id>
        <property name="Name">

    </property>
    <property name="Price">

    </property>
  </class>
NHibernate.Cache.HashtableCacheProvider
真的
120
真的
我已通过以下方式将我的实体配置为可缓存:

    <property
name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</
property>
    <!-- You have to explicitly enable the second level cache -->
    <property name="cache.use_second_level_cache">true</property>
    <!-- cache will expire in 2 minutes -->
    <property name="cache.default_expiration">120</property>
    <!-- I want also to cache queries -->
    <property name="cache.use_query_cache">true</property>
    <class
          name="CachableProduct"
          table="[CachableProduct]"
          dynamic-insert="true"
          dynamic-update="true">
        <cache usage="read-write"/>
        <id name="ProductId">
          <generator class="guid.comb" />
        </id>
        <property name="Name">

    </property>
    <property name="Price">

    </property>
  </class>

当我尝试运行下面的单元测试时,它总是输出两个 SELECT语句,因此它显然不会缓存实体。但是如果我 编写一个单元测试调用Load或Get from两个不同的 会话,第二级缓存工作,所以我相信我刚才遇到了一个问题 使用查询缓存

     using (var session = factory.OpenSession())
            {
                using (var tx =
session.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    Console.WriteLine("*** FIRST SESSION ***");
                    var result = session
                        .CreateCriteria(typeof(CachableProduct))
                        .SetCacheable(true)
                        .Add(Restrictions.Eq("Name", "PC"))
                        .List<CachableProduct>();
                    tx.Commit();
                }
            }
            using (var session = factory.OpenSession())
            {
                using (var tx =
session.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    Console.WriteLine("*** SECOND SESSION ***");
                    var result = session
                        .CreateCriteria(typeof(CachableProduct))
                        .SetCacheable(true)
                        .Add(Restrictions.Eq("Name", "PC"))
                        .List<CachableProduct>();
                    tx.Commit();
                }
            }
使用(var session=factory.OpenSession())
{
使用(var-tx)=
session.BeginTransaction(IsolationLevel.ReadCommitted))
{
Console.WriteLine(“***第一个会话***”);
var结果=会话
.CreateCriteria(产品类型)
.SetCacheable(真)
.Add(Restrictions.Eq(“名称”、“PC”))
.List();
tx.Commit();
}
}
使用(var session=factory.OpenSession())
{
使用(var-tx)=
session.BeginTransaction(IsolationLevel.ReadCommitted))
{
Console.WriteLine(“***第二个会话***”);
var结果=会话
.CreateCriteria(产品类型)
.SetCacheable(真)
.Add(Restrictions.Eq(“名称”、“PC”))
.List();
tx.Commit();
}
}