Hibernate缓存参考 [英] Hibernate Cache Reference

查看:89
本文介绍了Hibernate缓存参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单,完整,简明的缓存列表,您将使用Hibernate编写JPA代码。



特别我想知道对于每个高速缓存,高速缓存的生命周期(高速缓存何时失效),高速缓存的范围,清除高速缓存(如果有)的方式,高速缓存的内容,默认情况下高速缓存处于打开状态,如何打开它开/关和任何有用的信息。



我试图在另一个问题中找到这些信息,但找不到任何完整的答案。答案也遍布Hibernate文档,但我也很难找到它们。



我计划自己回答这个问题作为一个社区wiki来获取滚球,但我仍然不知道所有的答案,所以会有一些漏洞填补。

隔离级别缓存

摘要:此缓存有时不被称为缓存。但是,为了实现某些隔离级别,数据库本身可能会缓存一些查询结果。



生命周期/范围:此缓存的作用域为一个Session / EntityManager。生命周期被绑定到事务生命周期。



清除缓存:除了开始新的事务外,

获取缓存的内容:查询和结果(如果隔离处于可重复读取或可序列化级别)



默认情况下为:取决于来自数据库的默认隔离级别。默认情况下,MySQL提供可重复读取隔离功能,所以是的,默认情况下这是MySQL的功能。



打开/关闭:可以在创建交易时被指定。也可以通过更改数据库的默认值进行更改。



有用的信息: Hibernate / JPA实际上不能控制操作而不是指定需要哪个隔离级别。

会话级别(第一级)缓存



摘要:此缓存是EntityManager / Session缓存。我相信这也就是所谓的持久化上下文。



生命周期/范围:这个缓存的作用域是一个Session / EntityManager 。生命周期绑定到事务生命周期。



清除缓存:调用 clear() evict()可清除缓存中的单个对象。



所有内容



默认情况下为



打开/关闭:无法关闭



有用信息:只要<调用code> flush()。除非发生这种情况,否则其他事务将无法看到此缓存中的内容。保证 flush()的最好方法是提交事务。



二级缓存



摘要:这是可以启用的二级缓存(通常用于尝试和改善性能)。

生命周期/范围:我相信这是绑定到EntityManagerFactory / SessionFactory。自动逐出该缓存取决于缓存策略。在只读策略中,数据不会自动清除。在读写或不严格的读写策略中,数据将在会话关闭时被逐出。 不是100%确定。

清除缓存:您可以调用 getCache ().evict(class)来驱逐一个特定的类,并且 getCache()。evictAll()来驱逐整个缓存。这些方法位于EntityManagerFactory上。



缓存的内容:您明确配置应缓存的实体。



开启/关闭:开启/关闭:开启/关闭Hibernate配置



有用信息:

查询缓存



摘要:查询缓存是一个缓存,用于存储查询,查询参数和结果。如果查询和查询参数相同,则可以预期结果相同。



生命周期/范围:我不知道此缓存中的数据何时被确定为陈旧。我相信范围在EntityManagerFactory / SessionFactory级别。另外,Hibernate为每个表保留一个Hibernate上次更新时间戳列表。 Hibernate使用这些时间戳来确定查询结果是否过时并自动清除陈旧查询。



清除缓存 使用SessionFactory上的evictQueries()方法可以手动驱逐查询缓存。

缓存的内容:查询及其结果



默认情况下:



车削它打开/关闭:在Hibernate配置中打开/关闭

有用的信息:查询缓存只缓存实体ID。它必须与二级缓存结合使用才能实现 true(无数据库访问)缓存。


I'm looking for a simple, complete, concise listing of the caches you will run into coding JPA with Hibernate.

In particular I'd like to know for each cache, the lifecycle of the cache (when does the cache go stale), the scope of the cache, ways to clear the cache (if any), what gets cached, if the cache is on by default, how to turn it on/off, and any useful information.

I tried finding this information in another question but couldn't find any complete answers. The answers are also spread across the Hibernate documentation but I've had a hard time finding them there too.

I plan to answer this question myself as a community wiki to get the ball rolling but I still don't know all the answers so there will be some holes to fill in.

解决方案

Isolation Level Cache

Summary: This cache is sometimes not really called a cache. However, in order to implement certain isolation levels the database itself may be caching some query results.

Lifecycle/Scope: This cache is scoped to a single Session/EntityManager. The lifecycle is bound to the transaction lifecycle.

Clearing the cache: No way I know of other than starting a new transaction

What gets cached: Queries and result (if isolation is at repeatable read or serializable level)

On by default: Depends on the default isolation level which comes from the database. By default, MySQL ships with repeatable read isolation and so yes, this is on by default for MySQL.

Turning it on/off: Can be specified when creating the transaction. Can also be changed by changing the default on the database.

Useful Information: Hibernate/JPA doesn't really have any control over the operation of this cache other than specifying which isolation level is desired.

Session Level (1st-Level) Cache

Summary: This cache is the EntityManager/Session cache. I believe this is also what is referred to as the persistence context.

Lifecycle/Scope: This cache is scoped to a single Session/EntityManager. The lifecycle is bound to the transaction lifecycle.

Clearing the cache: Calling clear() on the EntityManager or Session clears the entire cache. Calling evict() on the Session clears a single object from the cache.

What gets cached: Everything

On by default: Yes

Turning it on/off: Can't be turned off

Useful Information: This cache gets merged with the database whenever flush() is called. Unless that happens other transactions will not be able to see things in this cache. The best way to guarantee a flush() is to commit the transaction.

2nd-Level Cache

Summary: This is a secondary cache that can be enabled (usually to try and improve performance).

Lifecycle/Scope: I believe this is bound to the EntityManagerFactory/SessionFactory. Automatic eviction of this cache depends on the cache strategy. In a read-only strategy data is never evicted automatically. In a read-write or nostrict read-write strategy data will be evicted when the session closes. Not 100% certain of this.

Clearing the cache: You can call getCache().evict(class) to evict a specific class and getCache().evictAll() to evict the entire cache. These methods are on the EntityManagerFactory.

What gets cached: You explicitly configure which entities should be cached.

On by default: No

Turning it on/off: Turned on/off in the Hibernate configuration

Useful Information:

Query Cache

Summary: Query Cache is a cache which stores queries, query parameters and results. If the query and query parameters are the same, you can expect the result to be the same.

Lifecycle/Scope: I have no idea when data in this cache is determined to be stale. I believe the scope is at the EntityManagerFactory/SessionFactory level. In addition, Hibernate keeps a list of "last update by Hibernate" timestamps for each of the tables. Hibernate uses these timestamps to determine if query results are stale and evict stale queries automatically.

Clearing the cache: The evictQueries() method on the SessionFactory can be used to manually evict the query cache.

What gets cached: Queries and their results

On by default: No

Turning it on/off: Turned on/off in the Hibernate configuration

Useful Information: The query cache only caches entity IDs. It must be used in conjunction with a 2nd-level cache to achieve a true (no DB access) cache.

这篇关于Hibernate缓存参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆