java 使用 Hibernate 作为 ORM 机制的 Web 应用程序中的 L1 和 L2 缓存有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4438245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:22:55  来源:igfitidea点击:

What's the difference between L1 and L2 caches in web-applications with Hibernate as ORM mechanism?

javahibernatecachingorm

提问by Roman

I just want some general info about standard purpose of using L1 cache and L2 cache.

我只想要一些关于使用 L1 缓存和 L2 缓存的标准目的的一般信息。

I'm curious because I'm investigating the system with terracotta as 2nd level cache and I've found that it also has 1st-level cache.

我很好奇,因为我正在研究使用 terracotta 作为二级缓存的系统,我发现它也有一级缓存。

回答by Chandan

L1 Cache is the cache that exists per Hibernate session, and this cache is not shared among threads. This cache makes use of Hibernate's own caching.

L1 Cache 是每个 Hibernate 会话存在的缓存,并且该缓存不在线程之间共享。这个缓存使用了 Hibernate 自己的缓存。

L2 Cache is a cache that survives beyond a Hibernate session, and can be shared among threads. For this cache you can use either a caching implementation that comes with Hibernate like EHCache or something else like JBossCache2

L2 缓存是一种在 Hibernate 会话之后仍然存在的缓存,并且可以在线程之间共享。对于此缓存,您可以使用 Hibernate 附带的缓存实现,如 EHCache 或其他类似 JBossCache2 的缓存实现

回答by GaryF

In JPA/Hibernate (and other similar ORM tools), the L1 cache is the transactional cache i.e. the entities stored from when you open a transaction to when you close it. This is almost never a shared cache (other threads can't make use of it). In JPA, this would usually be held by the EntityManager.

在 JPA/Hibernate(和其他类似的 ORM 工具)中,L1 缓存是事务缓存,即从打开事务到关闭事务所存储的实体。这几乎从来都不是共享缓存(其他线程无法使用它)。在 JPA 中,这通常由 EntityManager 持有。

The L2 cache is a full (typically) shared cache. If you have multiple threads/queries pulling in data, then they can make use of entities that have already been retrieved by other threads that are still live in the cache. In JPA, this would usually be held by the EntityManagerFactory.

L2 缓存是一个完整的(通常)共享缓存。如果您有多个线程/查询拉入数据,那么它们可以使用已经被其他线程检索到的实体,这些线程仍然存在于缓存中。在 JPA 中,这通常由 EntityManagerFactory 持有。

回答by jpkrohling

GaryF is not wrong, but is not technically right :-) Anton is more correct on this, but to complement his answer:

GaryF 没有错,但在技术上并不正确:-) Anton 对此更正确,但要补充他的回答:

First Level Cache: this is a "cache" which stores all the entities known by a specific session. So, if you have 3 transactions inside this session, it'll hold all entities touched by all three transactions. It gets cleared when you close the session or when you perform the "clear" method.

一级缓存:这是一个“缓存”,用于存储特定会话已知的所有实体。因此,如果您在此会话中有 3 个事务,它将保存所有三个事务涉及的所有实体。当您关闭会话或执行“清除”方法时,它会被清除。

Second Level Cache: this is a "real" cache and is delegated to an external provider, such as Infinispan. In this cache, you have full control over the contents of the cache, meaning that you are able to specify which entries should be evicted, which ones should be retained longer and so on.

二级缓存:这是一个“真正的”缓存,并委托给外部提供者,例如 Infinispan。在这个缓存中,您可以完全控制缓存的内容,这意味着您可以指定哪些条目应该被驱逐,哪些条目应该保留更长时间等等。

回答by Anton Gogolev

If Hibernate is anything similar to NHibernate (which it is, except the other way round), the Sessionis the first-level cache. Except that it is not cache in a general sense, but rather an identity map.

如果 Hibernate 类似于 NHibernate(它是,但反过来除外),则它Session是一级缓存。除了它不是一般意义上的缓存,而是身份映射。

回答by Anton Gogolev

L1 By default enabled, you have to add some third party library like EH cache, Redis for L2.

L1 默认启用,您必须添加一些第三方库,如 EH 缓存、用于 L2 的 Redis。

You can't disable L1 in hibernate.

您不能在休眠状态下禁用 L1。