简体   繁体   English

Hibernate L2查询缓存:缓存未命中时不要命中数据库

[英]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) . 我有一个表, Users的主键id :: int4和自然键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. 我想使用Hibernate通过数据库中的compund idpassword来检查行是否存在。

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. 我看到一个选项,可以直接从缓存中加载User,然后使用诸如session.merge()东西。 But maybe there is a better way? 但是也许有更好的方法?

PS. 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). 如果密码不相等,则在User对象脱水时,我的性能会有很小的下降(尚未分析)。 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. 您正在使用L2高速缓存违背其意图:高速缓存遇到未命中总是合法的。 By its nature the cache does not guarantee to be a 100% replica of an entire table. 从本质上说,缓存不能保证是整个表的100%复制。

If you want a reliable replica of the complete User table, then construct your own HashMap<String,String> . 如果您想要完整的User表的可靠副本,则构造自己的HashMap<String,String>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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