– 二级缓存不会缓存NHibernate中的过滤集合?

我正在使用NHibernate 3.0配置二级缓存.二级缓存适用于实体和集合,但我也有一些已过滤集合的实体.

 <bag name="EntityTrans" cascade="all-delete-orphan" table="EntityTrans">
            <key column="entityId" not-null="true" />
            <one-to-many class="MyDomain.EntityTran, MyDomain" />
            <filter name="cultureFilter" condition=":cultureCode = CultureCode" />


<class-cache class="MyDomain.EntityTran, MuDomain" region="MyRegion" usage="read-only"/>
<collection-cache collection="MyDomain.Entity.EntityTrans" region="MyRegion" usage="read-only"/>



首先我找到了这个forum post.然后我查看了代码(CollectionLoadContext.cs ~line 299)并找到了以下内容:

if (!(session.EnabledFilters.Count == 0) && persister.IsAffectedByEnabledFilters(session))
    // some filters affecting the collection are enabled on the session, so do not do the put into the cache.
    log.Debug("Refusing to add to cache due to enabled filters");
    // todo : add the notion of enabled filters to the CacheKey to differentiate filtered collections from non-filtered;
    //      but CacheKey is currently used for both collections and entities; would ideally need to define two separate ones;
    //      currently this works in conjunction with the check on
    //      DefaultInitializeCollectionEventHandler.initializeCollectionFromCache() (which makes sure to not read from
    //      cache with enabled filters).
    return; // EARLY EXIT!!!!!

转载注明原文 – 二级缓存不会缓存NHibernate中的过滤集合? - 代码日志