有人能用简单的话解释一下Hibernate/NHibernate中的一级缓存和二级缓存是什么吗?


当前回答

1.1)一级缓存

一级缓存始终与Session对象关联。Hibernate默认使用这个缓存。这里,它处理一个 一个接一个的交易,意味着不会处理一个交易很多 次了。主要是它减少了SQL查询的数量 在给定事务中生成。这代替了之后的更新 在事务中所做的每一次修改,都会更新事务 只有在交易结束时。

1.2)二级缓存

Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will be available to the entire application, not bound to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, at that time no need to go for a database transaction. In this way the second level cache works. Here we can use query level cache also.

引用自:http://javabeat.net/introduction-to-hibernate-caching/

其他回答

by default, NHibernate uses first level caching which is Session Object based. but if you are running in a multi-server environment, then the first level cache may not very scalable along with some performance issues. it is happens because of the fact that it has to make very frequent trips to the database as the data is distributed over multiple servers. in other words NHibernate provides a basic, not-so-sophisticated in-process L1 cache out of box. However, it doesn’t provide features that a caching solution must have to have a notable impact on the application performance.

所以所有这些问题的问题是使用与会话工厂对象相关的L2缓存。它减少了访问数据库的时间消耗,因此最终增加了应用程序的响应时间。

一级缓存

Hibernate试图将持久化上下文的刷新推迟到最后可能的时刻。这种策略传统上称为事务性后台写。

write-behind与Hibernate刷新更相关,而不是任何逻辑或物理事务。在事务期间,刷新可能发生多次。

刷新的更改仅对当前数据库事务可见。在提交当前事务之前,其他并发事务看不到任何更改。

由于有了一级缓存,Hibernate可以做以下几个优化:

JDBC语句批处理 防止丢失更新异常

第二级缓存

一个合适的缓存解决方案必须跨越多个Hibernate session,这就是Hibernate支持额外的二级缓存的原因。

二级缓存绑定到SessionFactory生命周期,因此只有在SessionFactory关闭时(通常是在应用程序关闭时)才会销毁它。二级缓存主要是面向实体的,尽管它也支持可选的查询缓存解决方案。

当加载一个实体时,Hibernate将执行以下操作:

如果实体存储在第一级缓存中,则返回缓存的对象引用。这确保了应用程序级的可重复读取。 如果实体没有存储在第一级缓存中,而第二级缓存被激活,那么Hibernate将检查实体是否已经缓存在第二级缓存中,如果是,则将其返回给调用者。 否则,如果实体没有存储在第一级或第二级缓存中,它将从DB中加载。

这里有一些hibernate缓存的基本解释…

First level cache is associated with “session” object. The scope of cache objects is of session. Once session is closed, cached objects are gone forever. First level cache is enabled by default and you can not disable it. When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session. If we query same object again with same session object, it will be loaded from cache and no sql query will be executed. The loaded entity can be removed from session using evict() method. The next loading of this entity will again make a database call if it has been removed using evict() method. The whole session cache can be removed using clear() method. It will remove all the entities stored in cache.

Second level cache is apart from first level cache which is available to be used globally in session factory scope. second level cache is created in session factory scope and is available to be used in all sessions which are created using that particular session factory. It also means that once session factory is closed, all cache associated with it die and cache manager also closed down. Whenever hibernate session try to load an entity, the very first place it look for cached copy of entity in first level cache (associated with particular hibernate session). If cached copy of entity is present in first level cache, it is returned as result of load method. If there is no cached entity in first level cache, then second level cache is looked up for cached entity. If second level cache has cached entity, it is returned as result of load method. But, before returning the entity, it is stored in first level cache also so that next invocation to load method for entity will return the entity from first level cache itself, and there will not be need to go to second level cache again. If entity is not found in first level cache and second level cache also, then database query is executed and entity is stored in both cache levels, before returning as response of load() method.

在streamlined Logic博客上有一个关于一级缓存的很好的解释。

基本上,第一级缓存发生在每个会话的基础上,而第二级缓存可以跨多个会话共享。

一级缓存

会话对象保存第一级缓存数据。默认启用。第一级缓存数据将对整个应用程序不可用。一个应用程序可以使用多个会话对象。

二级缓存

SessionFactory对象保存二级缓存数据。存储在第二级缓存中的数据将对整个应用程序可用。但是我们需要显式地启用它。