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

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

问题描述

我正在使用Spring 3和Hibernate 3.6开发Web应用程序。目前我尝试了解如何使用Spring和Hibernate进行缓存。我发现了有关Hibernate的缓存和Spring的一些消息,我现在尝试将我的信息集中在一起。我仍然对这两个框架都有一些问题,如果有人能回答他们或者告诉我这里列出的事实是否正确,我会很高兴。



大多数时候,简短的回答(是/否)就足够了。我认为这个列表对于其他人也很有用,他们希望了解如何使用spring和hibernate进行缓存。

常规 $ b

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



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

1st级缓存



<3> 第一级缓存是EVERY Hibernate应用程序的一部分。

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

5)保存在一级缓存中的是什么?对象还是它们属性的值?查询和它们的结果?
$ b $ p $ 2 code> 2级缓存



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

7)保存在二级缓存中:在我看来,属于一个记录,而不是对象本身。



8)当存储二级缓存中的一条记录的值时,可能存储相关值(从通过外键连接的对象)呢?



9)当更新二级缓存中的一个对象的值时,它有可能在缓存中更新与它连接的对象的值吗?



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



11)在哪里呢?

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

查询缓存



13)查询缓存和二级缓存之间有什么区别?在我看来:在查询缓存中,结果集会保存,但不会与它们的值一起保存,只是使用它们的ID。当再次使用查询并且结果集仍然正确时,属于ID的值将从第二级高速缓存中查询

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



15)在哪里查询缓存有意义, ?
$ b

Spring



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


$ b

17)方法缓存不与hibernate缓存相关



18)但是:对于缓存方法来说,第二级是必要的,比如ehcache(它也可以用于休眠)

19)可以在不使用数据库查询的情况下使用方法缓存吗?
$ b / code>



20)如果使用ehcache作为第二级缓存和ehcache for spring用于方法缓存,我可以使用同样的ehcache-in姿态?有没有可能混淆了一些东西?

21)当使用1级缓存和2级缓存时,它们会混淆吗?当查询数据库时,结果从哪里来,第一级或第二级缓存?第一级缓存是否可以与第二级缓存一起工作?



22)其他任何可以通过使用我提到的缓存混合的其他内容? : - )

感谢您的回答,无论哪个问题! : - )

解决方案


Hibernate支持以下高速缓存:1st Level Cache,2nd Level Cache,Query Cache


是。


支持以下缓存可能性:只是方法缓存


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


第一级缓存是EVERY Hibernate应用程序的一部分。





$ b


第一级缓存是为每个hibernate会话创建的。




是的,尽管您可以在任何特定时间手动清除它。


保存的是什么在第一级缓存中?对象还是它们属性的值?查询和它们的结果?

这是一个在会话生命期间获取的所有对象的映射,如果通过id加载相同的对象第二次,它将从L1加载。


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

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


保存在二级缓存中的内容:在我看来,属于一个记录的值而不是对象本身。


与L1相同,但寿命更长。 L2通常由一些工业级缓存提供支持,而L1仅仅是一张地图(它甚至不必是线程安全的)。它存储完整的实体,包括延迟加载的关系。


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


您不会手动管理L2,它会自动发生。


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


请参阅上面的内容。


正在改变,我如何更新二级缓存?冲洗?我可以只更新缓存的一部分,还是必须更新整个缓存?


请参阅上文 - Hibernate会为您解决这个问题。您不会直接与L2进行交互。


第二级缓存的位置在哪里?

衡量。在通过主键和读写因子读取大量数据的应用程序中,L2对您的性能有重大影响。


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


缓存模式有助于Hibernate选择最佳缓存策略和无效策略。例如,如果缓存是只读的,Hibernate不会打扰它无效(或者它不会经常这样做)。但只读缓存(只读实体)当然会禁止任何更新。


查询缓存和第二级别之间的区别是什么缓存?在我看来:在查询缓存中,结果集会保存,但不会与它们的值一起保存,只是使用它们的ID。当再次使用查询并且结果集仍然正确时,属于ID的值将从第二级缓存查询。




确实如此,但这是一个非常广泛的话题。特别是结果集仍然是正确的部分。


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

是的,没有二级缓存,查询缓存没有意义,并且会显着减慢应用程序的速度。


在哪里查询缓存有意义并且不在哪里?




通常,当你多次执行相同的查询并且查询参数的范围很低时(对于每组查询参数来说,新的查询缓存是以所有记录的结果为结果创建的)。

p>


Spring是否提供了比方法缓存更多的缓存功能?




方法缓存没有链接到hibernate缓存。


Spring未链接到Hibernate,所以......


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



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


可以使用方法缓存而无需数据库查询吗?



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


如果使用ehcache作为hibernate作为第二级缓存,而ehcache作为spring用于方法缓存,我可以使用相同的ehcache实例吗?是否有可能混淆了某些东西?


您可能使用相同的 CacheManager 并将配置缓存为Hibernate以简化部署。只要缓存名称不重叠,它们就是完全独立的,即使在同一个管理器中工作也是如此。


使用第一级缓存和二级缓存,它们可以混合起来吗?当查询数据库时,结果从哪里来,第一级或第二级缓存?第一级缓存是否可以与第二级缓存一起工作?


只要有些抽象不泄漏:-) 。当你通过主键进行查询时,首先检查L1(更快),然后L2。


其他任何可能混淆的东西使用我提到的缓存? : - )

如上所述,抽象往往会泄漏。但是当你改变数据库并且Hibernate不知道时,最糟糕的问题就出现了。如果没有适当的复制也会造成你头痛。最大的问题 - 不正确的缓存实际上减慢了应用程序的速度(查询缓存在这里是最危险的)。


Im working on developing a webapplication 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 informations together now. I still got some questions to both frameworks and Id be glad if someone could answer them or tell me if the facts listed here are correct.

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 supports the following Caches: 1st Level Cache, 2nd Level Cache, Query Cache

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

1st Level Cache

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

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

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) 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) what is saved in the 2nd Level Cache: in my opinion just the values belonging to one record, not the objects itself.

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) 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?

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) where does the 2nd level cache make sense and where doesnt it?

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) 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) For the Query Cache a 2nd Level Cache MUST be used?

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

Spring

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

17) method caching is not linked to hibernate caching

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

19) can method caching be used without database queries?

Getting mixed up

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) 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) anything else that can get mixed up by using the caches I mentioned? :-)

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

解决方案

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

Yes.

Spring itself supports the following Caching possibilities: just Method Caching

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

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

Yes.

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?

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.

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.

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?

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?

See above.

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?

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?

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?

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.

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?

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?

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).

Does Spring provide more Caching possibilities than method caching?

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

method caching is not linked to hibernate caching.

Spring is not linked to Hibernate, so...

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

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 has nothing to do with Hibernate. You may cache computations that have nothing to do with database.

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?

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?

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? :-)

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天全站免登陆