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

查看:75
本文介绍了用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.

General

1) Hibernate支持以下缓存:1级缓存,2级缓存,查询缓存

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

1st Level Cache

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?

2nd Level Cache

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),当将一条记录中的值存储到2级缓存中时,是否还可以存储相关值(来自通过外键连接的对象)?

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?

Query Cache

13)查询缓存和2级缓存之间有什么区别?我认为:在查询缓存中保存结果集,但不保存其值,仅保存其ID.当再次使用查询并且结果集仍然正确"时,将从第二级缓存中查询属于ID的值

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),但是:对于方法2级缓存是必需的,例如Ehcache(也可以由休眠模式使用)

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?

Getting mixed up

20),如果将ehcache作为ehcachenate用作第二级缓存,将Ehcache用作spring的方法缓存,是否可以使用相同的Ehcache-instance?有什么东西可能会混淆吗?

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支持以下缓存:1级缓存,2级缓存,查询缓存

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.

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

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.

查询缓存和2级缓存之间有什么区别?我认为:在查询缓存中保存结果集,但不保存其值,仅保存其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(也可以由休眠模式使用)

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

L2是休眠概念.如果要缓存方法,则需要 some 基础缓存.随它去吧,是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作为第二级缓存,使用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天全站免登陆