简体   繁体   English

如果我们有多个会话对象,一级缓存如何工作?

[英]how first level cache works, if we have multiple session objects?

I have a question regarding 1st level and 2nd level caches. 我对一级缓存和二级缓存有疑问。

2nd level cache 2级缓存

It is associated with the sessionFactory. 它与sessionFactory关联。 Suppose we have two sessions session1 and session2 and have one table student . 假设我们有两个会话session1session2,并且有一个table student We called get() from session1 , then it will hit DB and store the object in session1 and in the sessionFactory. 我们从session1调用get() ,然后它将命中数据库并将对象存储在session1和sessionFactory中。 if we called get() from session2 , then sessionFacory object will copied to session2. 如果我们从session2调用get() ,则sessionFacory对象将复制到session2。 If in future we call get() it will not hit DB. 如果将来我们调用get() ,它将不会访问数据库。 It will take it from session objects(1st level cache). 它将从会话对象(一级缓存)中获取。

Upto this I am clear. 到目前为止,我很清楚。

After that another session called session3 want to update the same record, then if we call get() on session1 and session2 . 之后,另一个名为session3的会话要更新同一条记录,那么如果我们在session1session2上调用get() what will happen? 会发生什么?

1st level cache 一级缓存

It is associated with a session. 它与会话关联。 If we have two sessions session1 and session2 . 如果我们有两个会话session1session2 We called get() from session1 , it will hit DB and store the object in session1. 我们从session1调用get() ,它将命中数据库并将对象存储在session1中。 If we update using session2 , what will happen if we call get() next time from session1 ? 如果我们使用session2更新,如果下次从session1调用get()会发生什么?

I heard 2nd level cache has some disadvantages. 我听说二级缓存有一些缺点。 If this is true what are they? 如果这是真的,那是什么?

You are asking three questions at once. 您一次要问三个问题。

1. Question 1.问题

I'm not sure what you mean by 我不确定你是什么意思

session3 want to update the same record session3要更新相同的记录

Sessions don't have free will. 会议没有自由意志。 And if they had just from wanting something pretty much nothing happens. 如果他们只是想要些什么,就什么也不会发生。 But if you use a third session to update the entities already cached in session1 and session2 they will keep there cached (now outdated) version. 但是,如果您使用的是第三次会议,以更新会话1已经被缓存的实体和工作阶段他们将继续存在缓存(现在已经过时)的版本。 If you use session1 or session2 to update the same entity, the update will fail with an optimistic locking exception. 如果使用session1session2更新同一实体,则更新将失败,并出现乐观锁定异常。

The 2nd level cache will get updated with the new version stored by session3 though. 不过,第二级缓存将使用session3存储的新版本进行更新。

2. Question 2.问题

This is the same as the first scenario: session1 now has a stale object which it will continue to use. 这与第一种情况相同: session1现在有一个陈旧的对象,它将继续使用。

3. Questions (Drawbacks of the 2nd level cache) 3.问题(第二级缓存的缺点)

All kinds of caches have the same challanges: 各种缓存具有相同的挑战:

  1. If you put stuff into cache but don't need it again you are wasting CPU time for putting it there and memory for keeping it there. 如果将内容放入缓存,但又不再需要它,那么您将浪费CPU时间将其放置在缓存中,而浪费了内存以将其保留在缓存中。

  2. If you look into a cache but don't find what you are looking for you are wasting CPU time for looking there. 如果您查找高速缓存,但是找不到所需的内容,那是在浪费CPU时间来查找高速缓存。

  3. If you keep stuff in a cache after it changed, you will work with stale data. 如果在更改后将内容保留在缓存中,则将处理陈旧的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM