2010-01-07 93 views
2

我一直在這裏待了好幾天。我已經配置了我的web/app配置,使用Memcached服務器和NHContrib提供者的二級緩存。我在測試中沒有遇到任何異常,我發現它沒有使用緩存來處理我設置了cacheable = true的查詢。 如果我將提供者切換到NHibernate.Cache.HashtableCacheProvider並測試它的工作方式如預期。NHibernate的Memcached Provider不能正常工作

這裏是我使用

<configuration> 
    <configSections> 
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" /> 
    <section name="memcache" type="NHibernate.Caches.MemCache.MemCacheSectionHandler,NHibernate.Caches.MemCache" /> 
    </configSections> 
    <memcache> 
    <memcached host="192.168.215.60" port="11211" /> 
    </memcache> 

    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
     <property name="connection.provider"> 
     NHibernate.Connection.DriverConnectionProvider 
     </property> 
     <property name="dialect"> 
     MT.Core.Persistence.Dialect, MT.Core 
     </property> 
     <property name="connection.driver_class"> 
     NHibernate.Driver.SqlClientDriver 
     </property> 
     <property name="connection.connection_string"> 
     Server=192.168.1.1;Initial Catalog=Test;User ID=TestUser;Password=fakepassword; 
     </property> 
     <property name="show_sql">true</property> 
     <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu</property> 
     <property name="cache.provider_class">NHibernate.Caches.MemCache.MemCacheProvider,NHibernate.Caches.MemCache</property> 
     <!--<property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</property>--> 
     <property name="cache.use_second_level_cache">true</property> 
     <property name="cache.use_query_cache">true</property> 
    </session-factory> 

    </hibernate-configuration> 
</configuration> 

回答

0

相關配置部分我認爲到期屬性應爲在會話工廠級別的內存緩存提供商設置的,而不是像其他人(SysCache)

提供商配置
<property name="expiration">300</property> 
+0

會話工廠下有一個屬性,用於cache.default_expiration,但添加此操作不會使緩存工作。在測試中,我仍然可以看到數據庫的兩個查詢,我應該看到一個。 – CountCet 2010-01-07 17:32:03

+0

你有沒有嘗試過期,因爲我使用了memcache,它對我有用。 – MatthieuGD 2010-01-07 17:44:01

+0

是在裏面我添加了那個確切的屬性,它拋出異常。我在Nhibernate 2.1.2 – CountCet 2010-01-07 18:10:29

2

該問題最終是由於連接問題。我使用log4net將任何錯誤記錄到控制檯和應用程序日誌中。那時我終於看到了連接到memcached服務器的錯誤。一旦將代碼提升到同一位置的服務器,錯誤就消失了。我應該已經學會了在使用log4net之前。

2

對於memcache,屬性是'default_expiration'而不是'過期'。我不確定SysCache。但是我已經將這個屬性用於memcache,並且適用於我。

Initailly我也遇到了CountCet提到的同樣的錯誤。 MemCache提供程序無法識別'expiration'屬性。後來我檢查了代碼,發現它使用屬性'default_expiration',其默認值爲300秒。