I've enabled query cache for one of my queries, it works fine (query result is taken from query cache and entities from second level cache) when I run a test or a console application. However, if I run the same queries through a WCF service, the cache is never hit.
I've also checked the logs in both cases and they are practically identical until the cache miss.
Here's NH's log showing the cache put:
[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: 0@-891130694&value=System.Collections.Generic.List`1[System.Object]
Here's NH's log showing the cache miss:
[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: 0@519257116' 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
Here's NH's log showing the cache hit, when not in a WCF service:
[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: 0@-369095952' 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
Here's the code I'm using to do the query:
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;
}
Why isn't the cache being hit?