2011-06-03 111 views
3

我正在配置第二級緩存與NHibernate 3.0。二級緩存很棒的作品,適用於Entities and Collections,但我也有實體有已過濾的收藏二級緩存不會在NHibernate中緩存過濾的集合?

<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的二級緩存並不緩存上述過濾收集。我可以在NHProf中看到過濾收集查詢被髮送到數據庫。我的NHibernate配置文件包含以下條目。

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

我需要添加更多東西來緩存已過濾的集合嗎?

回答

8

目前NHibernate不支持過濾集合的二級緩存。

首先我發現這個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!!!!! 
}