Layer 2 cache not caching filtered collections in NHibernate?

I am setting up second level cache with NHibernate 3.0. Layer 2 cache works great for objects and collections, but I also have some objects that have filtered collections .

 <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" />
        </bag>

NHibernate Level 2 Caching does not cache the above filtered collection . I see in NHProf that for filtered queries, collections are sent to the database. My NHibernate configuration file has the following entries.

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

Do I need to add something else to cache the filtered collection?

+3
source share
1 answer

NHibernate .

. (CollectionLoadContext.cs ~ 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!!!!!
}
+8

All Articles