2009-10-07 126 views
3

我運行測試或控制檯應用程序時,我爲其中一個查詢啓用了查詢緩存,它工作正常(查詢緩存中的查詢結果和來自第二級緩存的實體)。 但是,如果我通過WCF服務運行相同的查詢,則緩存永遠不會被命中。NHibernate查詢緩存和WCF

我也檢查了兩種情況下的日誌,它們幾乎完全相同,直到緩存未命中。

這裏的顯示緩存放NH日誌:

[25,4244] 16:15:18,237 DEBUG[NHibernate.Cache.StandardQueryCache]: caching query results in region: 'NHibernate.Cache.StandardQueryCache'; sql: <MyQuery>; parameters: <MyParameter>; first row: 0 
[25,4244] 16:15:18,238 DEBUG[NHibernate.Caches.SysCache.SysCache]: adding new data: key=NHibernate-Cache:NHibernate.Cache.StandardQueryCache:sql: <MyQuery>; parameters: <MyParameter> ; first row: [email protected]&value=System.Collections.Generic.List`1[System.Object] 

這裏的NH日誌顯示緩存未命中:

[25,4244] 16:15:29,089 DEBUG[NHibernate.Cache.StandardQueryCache]: checking cached query results in region: 'NHibernate.Cache.StandardQueryCache'; sql: <MyQuery>; parameters: <MyParameter>; first row: 0 
[25,4244] 16:15:29,089 DEBUG[NHibernate.Caches.SysCache.SysCache]: Fetching object 'NHibernate-Cache:NHibernate.Cache.StandardQueryCache:sql: <MyQuery>; parameters: <MyParameter>; first row: [email protected]' from the cache. 
[25,4244] 16:15:29,095 DEBUG[NHibernate.Cache.StandardQueryCache]: query results were not found in cache: sql: <MyQuery>; parameters: <MyParameter>; first row: 0 

這裏的顯示緩存命中NH的日誌,在不WCF服務:

[11,3656] 17:37:48,718 DEBUG[NHibernate.Cache.StandardQueryCache]: checking cached query results in region: 'NHibernate.Cache.StandardQueryCache'; sql: <MyQuery>; parameters: <MyParameter>; first row: 0 
[11,3656] 17:37:48,718 DEBUG[NHibernate.Caches.SysCache.SysCache]: Fetching object 'NHibernate-Cache:NHibernate.Cache.StandardQueryCache:sql: <MyQuery>; parameters: <MyParameter>; first row: [email protected]' from the cache. 
[11,3656] 17:37:48,728 DEBUG[NHibernate.Cache.StandardQueryCache]: Checking query spaces for up-to-dateness <MyTableName> 
[11,3656] 17:37:48,729 DEBUG[NHibernate.Caches.SysCache.SysCache]: Fetching object 'NHibernate-Cache:UpdateTimestampsCache:<MyTableName>@-1403193414' from the cache. 
[11,3656] 17:37:48,729 DEBUG[NHibernate.Cache.StandardQueryCache]: returning cached query results for: sql: <MyQuery>; parameters: <MyParameter>; first row: 0 

下面是我用來執行查詢的代碼:

using (ISession session = SessionFactory.OpenSession()) 
    using (ITransaction transaction = session.BeginTransaction()) 
    { 
     IList<T> entities = session 
      .CreateCriteria(typeof(T)) 
      .SetCacheable(true) 
      .Add(expression) 
      .List<T>(); 
     transaction.Commit(); 
     return entities; 
    } 

爲什麼不打中緩存?

回答

1

您確定您沒有爲每個請求重新創建SessionFactory嗎?或者你通常不會進行不必要的重新初始化?

+1

是的,我有一個會話工廠。 但是,我確實創建了新的會話,您是不是意味着不必要的重新初始化? –