Query and entity caches not getting hit

Visto 34 veces
Saltar al primer mensaje no leído

Eric Rizzo

no leída,
8 ago 2019, 12:55:028/8/19
a nhusers
I'm trying to configure the Level 2 entity and query caches for an existing working application, but they're not being used at all from what I can tell.
I've set the following config in my web.config:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <
session-factory>
    <
property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
    <
property name="adonet.batch_size">50</property>
    <
property name="hbm2ddl.keywords">auto-quote</property>
    <
property name="max_fetch_depth">3</property>
    <
property name="cache.use_second_level_cache">true</property>
    <
property name="cache.provider_class">NHibernate.Caches.RtMemoryCache.RtMemoryCacheProvider,NHibernate.Caches.RtMemoryCache</property>
    <
property name="cache.use_query_cache">true</property>
  </
session-factory>
</
hibernate-configuration>

<!-- Hibernate Level2 cache-->
<rtmemorycache>
 <
cache region="MyEntity" expiration="28800"/> <!-- expiration is in seconds -->
</rtmemorycache>


In my entity mapping I have this:

<cache usage="read-write" region="MyEntity" />


We use the session-per-request pattern, and are using explicit transactions:

ISession session = factory.OpenSession();
CurrentSessionContext.Bind(session);
session.BeginTransaction();

I've stepped through in the debugger and I see the config properties and the cache provider being created.


But when I repeatedly run the same query (using Swagger or Postman) I see the SQL being logged every time and NHibernate Profiler tells me it is indeed querying the database every time.

Here's the query I'm using as my test:

session.Query<T>().FirstOrDefault(e => e.Id == id)


Here is what Profiler is telling me about cache usage:

NHPRofiler 1.png


Any ideas how to troubleshoot the non-use of the caches are welcome. I'm really stumped.


Thanks in advance,

Eric



Gunnar Liljas

no leída,
8 ago 2019, 14:23:328/8/19
a nhu...@googlegroups.com
You must specify that you want the query to be cached/use the cache.

session.Query<T>().WithOptions(x=>x.SetCacheable(true)).FirstOrDefault(e => e.Id == id) 

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nhusers/43bde487-c9e0-481b-9a36-43a141c4f2ec%40googlegroups.com.

Eric Rizzo

no leída,
8 ago 2019, 14:57:318/8/19
a nhusers
Wrong Profiler screen shot above, here's the correct one (with statistics enabled):

NHPRofiler 2.png

Eric Rizzo

no leída,
9 ago 2019, 16:27:109/8/19
a nhusers
Yes, I realized I had that in a different code branch right around the time you answered. Too many branches in flight at the same time. :-)

Anyway, thanks. That's definitely what was missing.


On Thursday, August 8, 2019 at 2:23:32 PM UTC-4, Gunnar Liljas wrote:
You must specify that you want the query to be cached/use the cache.

session.Query<T>().WithOptions(x=>x.SetCacheable(true)).FirstOrDefault(e => e.Id == id) 

To unsubscribe from this group and stop receiving emails from it, send an email to nhu...@googlegroups.com.
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos