Java – hibernate L2 cache does not seem to work properly

I am currently trying to run hibernate using the cache provider that comes with hibernate

net.sf.ehcache.hibernate.SingletonEhCacheProvider

I'm in ecache The default cache and class specific cache are enabled in XML, which is in my hibernate. XML cfg. Reference in XML file Define a class / map file specific cache to handle up to 20000 objects

However, I found that I haven't seen this problem since I opened the cache mapping on one of the mapping files

My tests are as follows

Load 10000 objects of the specific mapping file I tested (this should hit the DB and become a bottleneck) Next, I will load the same 10000 objects because I want the cache to be hit and see a significant performance improvement I have tried to use hibernate to map "read only" and "read write" cache mappings in XML files

I wonder what they need to do to ensure that the cache is hit before the database when loading objects?

Note that as part of the test im pagin, using these 10000 records is similar to the following (paging 1000 records in time)

Criteria crit = HibernateUtil.getSession() .createCriteria( persistentClass );
       crit.setFirstResult(startIndex);
       crit.setFetchSize(fetchSize);
       return crit.list();

I've seen that the standard has a cache mode setter (setcachemode()), so what should I do?

I noticed that using the following statistical code, there are 10000 objects in memory (good hibernate dehydration objects, I imagine???) For some reason, I got 0 clicks and 0 failures, which is more worrying, so it seems that it doesn't enter the cache at all. When it looks, even if the statistical code seems to tell me that there are 10000 objects in memory

Any ideas about what I'm doing? I think the fact of my gains and losses is good because it means that I'm using cache, but I can't figure out why I didn't get any cache hits Whether to use setfirstresult() and setfetchsize() with conditions

System.out.println("Cache Misses = " + stats.getSecondLevelCacheMissCount());
System.out.println("Cache Hits Count = " + stats.getSecondLevelCacheHitCount());

System.out.println("2nd level elements in mem "+ stats.getSecondLevelCacheStatistics("com.someTestEntity").getElementCountInMemory());

Solution

L2 cache is suitable for "find by primary key" For other queries, you need to cache the query (assuming query caching is enabled). In your case, use criteria #setcacheable (Boolean):

Criteria crit = HibernateUtil.getSession().createCriteria( persistentClass );
crit.setFirstResult(startIndex);
crit.setFetchSize(fetchSize);
crit.setCachable(true); // Enable caching of this query result
return crit.list();

I suggest reading:

> Hibernate: Truly Understanding the Second-Level and Query Caches

Yes, they will I explained this in black in the link mentioned above: "please note that query cache does not cache the state of the actual entity in the result set; it only caches the results of identifier value and value type. Therefore, query cache should always be used with L2 cache". Have you read it?

It is different (the "key" used to cache the entry is different) But the query cache depends on L2 cache

I'm just saying that you need to cache queries because you don't "find through primary key" I don't know what I don't know Do you attempt to call setcacheable (true) on a query or condition object? I'm sorry to insist, but have you read the link I posted?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>