使用 Hibernate + Spring 缓存 - 一些问题 [英] Caching with Hibernate + Spring - some Questions

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

问题描述

我正在使用 Spring 3 和 Hibernate 3.6 开发 Web 应用程序.目前我试图了解使用 Spring 和 Hibernate 进行缓存是如何工作的.我找到了一些关于使用 Hibernate 进行缓存和一些关于 Spring 的资源,我现在尝试将我的信息整合在一起.我仍然对这两个框架有一些疑问,如果有人能回答这些问题或告诉我这里列出的事实是否正确,我会很高兴.

I'm working on developing a web application with Spring 3 and Hibernate 3.6. At the moment I try to understand how Caching with Spring and Hibernate works. I found some sources about Caching with Hibernate and some about Spring and I try to bring my information together now. I still got some questions to both frameworks and I'd be glad if someone could answer them or tell me if the facts listed here are correct.

大多数时候,简短的回答(是/否)就足够了.我认为这个列表对其他人也有用,他们想了解 spring 和 hibernate 缓存的工作原理.

Most of the time, short answers (yes/no) would be sufficient. I think that this list can be useful for others too, who want to understand how caching with spring and hibernate works.

一般

1) Hibernate 支持以下缓存:一级缓存、二级缓存、查询缓存

1) Hibernate supports the following Caches: 1st Level Cache, 2nd Level Cache, Query Cache

2) Spring 本身支持以下缓存可能性:仅方法缓存

2) Spring itself supports the following Caching possibilities: just Method Caching

一级缓存

3) 第一级缓存是每个 Hibernate 应用程序的一部分.

3) The 1st Level Cache is part of EVERY Hibernate application.

4) 第一级缓存是为每个休眠会话创建的.

4) The 1st Level Cache is created for EVERY hibernate-session.

5) 一级缓存中保存了什么?对象还是只是它们的属性值?查询及其结果?

5) What is saved in the 1st Level Cache? Objects or just the values of their properties? queries and their results?

二级缓存

6) 我发现:每个应用程序使用 2 级缓存一次.那不是假的吗?不是每个 SessionFactory 使用一次吗?和:多个 sessionfactorys = 多个二级缓存可能吗?

6) I found out: the 2nd Level Cache is used ONCE per application. isn't that false? isn't it used ONCE per SessionFactory? and: multiple sessionfactorys = multiple 2nd level caches possible?

7) 二级缓存中保存的是什么:在我看来只是属于一个记录的值,而不是对象本身.

7) what is saved in the 2nd Level Cache: in my opinion just the values belonging to one record, not the objects itself.

8) 当在二级缓存中存储一​​条记录的值时,是否也可以用它存储相关值(来自通过外键连接的对象)?

8) when storing values from one record in 2nd Level Cache, its possible to store related values (from objects connected over a foreign key) with it too?

9) 更新二级缓存中一个对象的值时,是否也可以更新缓存中与其关联的对象的值?

9) when updating the values of one object in the 2nd level cache, it's possible to update the values of objects connected with it in the cache too?

10) 当对象的值发生变化时,如何更新二级缓存?冲洗?我可以只更新缓存的一部分还是必须更新整个缓存?

10) when values of an object are changing, how can I update the 2nd level cache? flush? can I just update a part of the cache or must the whole cache be updated?

11) 二级缓存哪里有意义,哪里没有?

11) where does the 2nd level cache make sense and where doesn't it?

12) 缓存模式:每种缓存模式是否提供不同的缓存策略?例如缓存模式只读"不需要同步数据库和缓存?其他缓存模式是否提供同步?我认为同步必须由开发人员自己完成?

12) the Cache Mode: does each cache mode provide a different strategy of caching? for example with cache mode "read-only" no synchronization of database and cache is ever necessary? do other cache modes provide synchronization? I thought synchronization must be done by the developer himself?

查询缓存

13) 查询缓存和二级缓存有什么区别?在我看来:在查询缓存中保存结果集,但不保存它们的值,只保存它们的 id.当再次使用查询并且结果集仍然正确"时,从二级缓存中查询属于 ids 的值

13) what is the difference between the Query Cache and the 2nd Level Cache? in my opinion: in the Query Cache result sets are saved, but not with their values, just with their ids. when the query is used again and the result set is still "correct", the values belonging to the ids are queried from the 2nd Level Cache

14)对于查询缓存,必须使用二级缓存吗?

14) For the Query Cache a 2nd Level Cache MUST be used?

15) 查询缓存在哪些地方有意义,哪些地方没有意义?

15) where does the Query Cache make sense and where doesn't it?

Spring

16) Spring 是否提供比方法缓存更多的缓存可能性?

16) Does Spring provide more Caching possibilities than method caching?

17) 方法缓存与休眠缓存无关

17) method caching is not linked to hibernate caching

18) 但是:对于方法缓存来说,第二级是必要的,比如 Ehcache(也可以被 hibernate 使用)

18) but: for method caching the 2nd level is necessary, like Ehcache (which can be used by hibernate too)

19)可以在没有数据库查询的情况下使用方法缓存吗?

19) can method caching be used without database queries?

混淆

20) 如果将 ehcache 用于 hibernate 作为二级缓存,将 Ehcache 用于 spring 作为方法缓存,我可以使用相同的 Ehcache 实例吗?有没有可能搞混?

20) if using ehcache for hibernate as 2nd level cache and Ehcache for spring for method caching, can I use the same Ehcache-instance? is there a chance that something gets mixed up?

21) 当使用一级缓存和二级缓存时,它们会混淆吗?查询数据库时,结果来自哪里,一级缓存还是二级缓存?一级缓存可以和二级缓存一起使用吗?

21) when using 1st level cache and 2nd level cache, can they get mixed up? when querying the database, where does the result then come from, the 1st or 2nd level cache? does the 1st level cache work with the 2nd level cache?

22) 还有什么可以通过使用我提到的缓存混淆的吗?:-)

22) anything else that can get mixed up by using the caches I mentioned? :-)

谢谢你的回答,不管是什么问题!:-)

Thanks for answering, no matter what question! :-)

推荐答案

Hibernate 支持以下缓存:一级缓存、二级缓存、查询缓存

Hibernate supports the following Caches: 1st Level Cache, 2nd Level Cache, Query Cache

是的.

Spring 本身支持以下缓存可能性:仅方法缓存

Spring itself supports the following Caching possibilities: just Method Caching

Spring 3.1 引入了基于方法注释的新缓存抽象,是的.

Spring 3.1 introduces the new caching abstraction based on annotations around methods, yes.

第一级缓存是每个 Hibernate 应用程序的一部分.

The 1st Level Cache is part of EVERY Hibernate application.

是的.

第一级缓存是为每个休眠会话创建的.

The 1st Level Cache is created for EVERY hibernate-session.

是的,尽管您可以随时手动清除它.

Yes, although you can manually clear it at any given moment.

一级缓存中保存了什么?对象还是只是它们的属性值?查询及其结果?

What is saved in the 1st Level Cache? Objects or just the values of their properties? queries and their results?

它是一个会话生命周期内获取的所有对象的映射,如果您第二次通过 id 加载同一个对象,它将从 L1 加载.

It's a map of all objects fetched during the life of a session, if you load the same object by id for the second time, it will be loaded from L1.

我发现:每个应用程序使用一次二级缓存.那不是假的吗?不是每个 sessionfactory 使用一次吗?和:多个 sessionfactorys = 多个二级缓存可能吗?

I found out: the 2nd Level Cache is used ONCE per application. isn't that false? isn't it used ONCE per sessionfactory? and: multiple sessionfactorys = multiple 2nd level caches possible?

您是对的,通常每个应用程序(数据库)只有一个会话工厂,因此是快捷方式.

You are right, typically there is just one session factory per application (database), hence the shortcut.

二级缓存中保存的是什么:在我看来只是属于一个记录的值,而不是对象本身.

what is saved in the 2nd Level Cache: in my opinion just the values belonging to one record, not the objects itself.

与 L1 中的相同,但它们的寿命更长.L2 通常由一些工业强度的缓存支持,而 L1 只是一个映射(它甚至不必是线程安全的).它存储完整的实体,包括延迟加载的关系.

The same things as in L1, but they live longer. L2 is typically backed by some industrial-strength cache, while L1 is just a map (it doesn't even have to be thread-safe). It stores full entities, including lazily loaded relationships.

当存储来自二级缓存中的一条记录的值时,是否也可以用它存储相关值(来自通过外键连接的对象)?

when storing values from one record in 2nd Level Cache, its possible to store related values (from objects connected over a foreign key) with it too?

您无需手动管理 L2,它会自动进行.

You do not manage L2 manually, it happens automatically.

当更新二级缓存中的一个对象的值时,是否也可以更新缓存中与其连接的对象的值?

when updating the values of one object in the 2nd level cache, its possible to update the values of objects connected with it in the cache too?

见上文.

当对象的值发生变化时,如何更新二级缓存?冲洗?我可以只更新缓存的一部分还是必须更新整个缓存?

when values of an object are changing, how can I update the 2nd level cache? flush? can I just update a part of the cache or must the whole cache be updated?

见上文 - Hibernate 会为您解决这个问题.你从不直接与 L2 交互.

See above - Hibernate will figure this out for you. You never interact with L2 directly.

二级缓存哪里有意义,哪里没有意义?

where does the 2nd level cache make sense and where doesnt it?

测量.在主键读取大量数据且读写系数非常高的应用中,L2 对您的性能有显着影响.

Measure. In application that read a lot of data by primary key and read-to-write factor is very high, L2 has a significant impact on your performance.

缓存模式:每种缓存模式是否提供不同的缓存策略?例如缓存模式只读"不需要同步数据库和缓存?其他缓存模式是否提供同步?我认为同步必须由开发人员自己完成?

the Cache Mode: does each cache mode provide a different strategy of caching? for example with cache mode "read only" no synchronization of database and cache is ever necessary? do other cache modes provide synchronization? I thought synchronization must be done by the developer himself?

缓存模式帮助 Hibernate 选择最佳缓存和失效策略.例如,如果缓存是只读的,Hibernate 不会费心使它失效(或者它不会经常这样做).但是只读缓存(只读实体)当然会禁止任何更新.

Cache mode helps Hibernate to choose best strategy for caching and invalidating. For instance if cache is read only, Hibernate won't bother invalidating it (or it won't do this that often). But read only cache (read only entity) will of course forbid any updates.

查询缓存和二级缓存有什么区别?在我看来:在查询缓存中保存结果集,但不保存它们的值,只保存它们的 id.当再次使用查询并且结果集仍然正确"时,从二级缓存中查询属于 id 的值.

what is the difference between the Query Cache and the 2nd Level Cache? in my opinion: in the Query Cache result sets are saved, but not with their values, just with their ids. when the query is used again and the result set is still "correct", the values belonging to the ids are queried from the 2nd Level Cache.

没错,但这是一个非常广泛的话题.尤其是结果集仍然是正确"的部分.

Exactly, but this is a very broad topic. Especially the result set is still "correct" part.

对于查询缓存,必须使用二级缓存吗?

For the Query Cache a 2nd Level Cache MUST be used?

是的,如果没有 L2 缓存,查询缓存就没有意义,并且会显着降低应用程序的速度.

Yes, without the L2 cache, the query cache has no sense and will slow down the application dramatically.

查询缓存在哪里有意义,哪里没有意义?

where does the Query Cache make sense and where doesnt it?

困难的问题,通常当您多次执行相同的查询并且查询参数的范围很少时(对于每组查询参数,都会创建新的查询缓存,其中所有记录的 ID 都是结果).

Hard question, typically when you are executing the same query a lot of times and the universe of query parameters is low (for each set of query parameters new query cache is created with all ids of records being the results).

Spring 是否提供比方法缓存更多的缓存可能性?

Does Spring provide more Caching possibilities than method caching?

不,Spring 或多或少只是您自己代码的粘合剂.

No, Spring is more-or-less just a glue for your own code.

方法缓存与休眠缓存无关.

method caching is not linked to hibernate caching.

Spring 没有链接到 Hibernate,所以...

Spring is not linked to Hibernate, so...

但是:对于方法缓存来说,第二级是必要的,比如 ehcache(也可以被 hibernate 使用)

but: for method caching a 2nd level is necessary, like ehcache (which can be used by hibernate too)

L2 是 Hibernate 的概念.如果你想缓存方法,你需要一些底层缓存.让它成为 EhCache,没关系.当然它必须是线程安全的.

L2 is Hibernate concept. If you want to cache methods, you need some underlying cache. Let it be EhCache, never mind. of course it must be thread-safe.

可以在没有数据库查询的情况下使用方法缓存吗?

can method caching be used without database queries?

Spring 与 Hibernate 无关.您可以缓存与数据库无关的计算.

Spring has nothing to do with Hibernate. You may cache computations that have nothing to do with database.

如果将 ehcache 用于 hibernate 作为二级缓存,将 ehcache 用于 spring 进行方法缓存,我可以使用相同的 ehcache-instance 吗?有没有可能搞混?

if using ehcache for hibernate as 2nd level cache and ehcache for spring for method caching, can I use the same ehcache-instance? is there a chance that something gets mixed up?

您可以使用与 Hibernate 相同的 CacheManager 和缓存配置来简化部署.只要缓存名称不重叠,它们就完全独立,甚至可以在同一个管理器中工作.

You may use the same CacheManager and cache configuration as Hibernate to ease deployment. As long as cache names do not overlap, they are completely independent, even thought working within the same manager.

当使用一级缓存和二级缓存时,它们会混淆吗?查询数据库时,结果来自哪里,一级缓存还是二级缓存?一级缓存可以和二级缓存一起使用吗?

when using 1st level cache and 2nd level cache, can they get mixed up? when querying the database, where does the result then come from, the 1st or 2nd level cache? does the 1st level cache work with the 2nd level cache?

它们只是工作,只要一些抽象不泄漏:-).当您按主键查询时,首先检查 L1(速度更快),然后检查 L2.

They just work, as long as some abstraction does not leak :-). When you query by primary key, first L1 is examined (it is faster), then L2.

还有什么可以通过使用我提到的缓存混淆的吗?:-)

anything else that can get mixed up by using the caches I mentioned? :-)

见上文,抽象往往会泄漏.但是当您更改数据库而 Hibernate 不知道它时,会出现最糟糕的问题.没有正确复制的集群也会让你头疼.最大的问题 - 通常不正确的缓存实际上会降低应用程序的速度(查询缓存在这里是最危险的).

See above, abstractions tend to leak. But the worst problems come when you change the database and Hibernate does not know about it. Also clustering without proper replication will cause you a headache. And the biggest problem - very often incorrect caching actually slows down the application (query cache is the most dangerous here).

这篇关于使用 Hibernate + Spring 缓存 - 一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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