NHibernate and WCF Query Cache

I turned on the query cache for one of my queries, it works fine (the query result is taken from the query cache and the entity from the second level cache) when starting a test or console application. However, if I run the same requests through the WCF service, the cache never hits.

I also checked the logs in both cases, and they are almost identical until the cache disappears.

Here is NH log showing cache:

[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 NH log shows the skipped cache:

[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 is NH log showing cache hit, if not in 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 is the code I use to execute the request:

    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 didn't the cache hit?

+3
source share
1 answer

, SessionFactory ? ?

+1

Source: https://habr.com/ru/post/1719518/


All Articles