简体   繁体   中英

Hibernate L2 query cache: do not hit database on cache miss

I have a table, Users with primary key id :: int4 and natural key password :: varchar(32) . I'd like to check existence of a row by compund id and password in DB as fast as possible using Hibernate.

So I load all users to L2 cache and do

User u = (User)session.get(User.class, uId);
if (!u.getPassword().equals(pass)) {
   // fail when passwords are not equal
}

This is good when cache was hit, but on cache miss (which means false input data) this will trigger select queries. How can I point hibernate not to hit database, if value not found in cache?

I see an option to load User directly from cache and then use something like session.merge() it. But maybe there is a better way?

PS. I have one more complaint. If passwords are not equal, I have small performance degradation on dehydration of my User object (haven't profiled yet). Can this also be eliminated?

You are using the L2 cache against its intentions: it is always legal for a cache to experience a miss. By its nature the cache does not guarantee to be a 100% replica of an entire table.

If you want a reliable replica of the complete User table, then construct your own HashMap<String,String> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM