Hibernate L2 Cache 之 Ignite
来源:互联网 发布:淘宝卖家营销策略 编辑:程序博客网 时间:2024/05/17 03:22
1.概述
启动内存数据结构可以用作Hibernate二级缓存(或L2缓存),它可以极大地加速应用程序的持久层。Hibernate是一个众所周知的、广泛使用的对象关系映射框架(ORM)。与SQL数据库交互时,它会缓存检索到的数据,以最小化昂贵的数据库请求。
所有使用Hibernate数据库映射的对象都是在一个会话中完成的,通常绑定到一个工作线程或一个Web会话。默认情况下,Hibernate只使用每个会话(L1)缓存,因此,在一个会话中缓存的对象在另一个会话中是不可见的。但是,可以使用全局二级(L2)缓存,其中缓存对象在所有使用相同L2缓存配置的会话中都可以看到。这通常会带来更大的性能增益,因为每一个新创建的会话都可以充分利用L2缓存内存中已经存在的数据(它比任何session-local L1缓存更重要)。尽管L1缓存始终是由Hibernate内部的Hibernate来启用和实现的,但是L2缓存是可选的,并且可以有多个可插接实现。ignite可以很容易作为一个L2缓存的实现而插入进其中,可以在所有访问模式中使用(READ_ONLY, READ_WRITE, NONSTRICT_READ_WRITE和TRANSACTIONAL),支持多种相关特性: >缓存到内存和磁盘,以及非堆内存。 >缓存事务,使事务模式成为可能。 >集群,有两种不同的复制模式:复制和分区要开始使用GridGain作为Hibernate L2缓存,您需要执行三个简单的步骤: >将ignite的库添加到应用程序的类路径中。 >启用L2缓存并指定在L2缓存配置中激活实现类 >为L2缓存区域配置ignite缓存,并启动嵌入的ignite节点(以及,可选地,外部ignite节点)。在下一节中,我们将更详细地讨论这些步骤。
2.L2 Cache Configuration
为了将内存中的数据结构配置为Hibernate L2缓存,无需对现有Hibernate代码进行任何更改,您需要:1.根据依赖于是否使用Hibernate5或Hibernate4,将ignite-hibernate_5.1或ignite-hibernate_4.2模块添加到您的项目。或者,如果您从命令行启动Apache ignite节点,您可以复制JAR文件从{apache_ignite_relese}/libs/optional到{apache_ignite_relese}/libs文件夹中。2.配置Hibernate本身使用ignite作为L2缓存。3.适当地配置ignite缓存。
2.1 Maven Configuration
要添加Apache点燃Hibernate集成到您的项目,请添加以下依赖项到您的pom。xml文件:
//hibernate5<dependency> <groupId>org.apache.ignite</groupId> <artifactId>ignite-hibernate_5.1</artifactId> <version>${ignite.version}</version></dependency>
2.2 Hibernate Configuration Example
用于L2缓存的典型Hibernate配置如下所示:
<hibernate-configuration> <session-factory> ... <!-- Enable L2 cache. --> <property name="cache.use_second_level_cache">true</property> <!-- Generate L2 cache statistics. --> <property name="generate_statistics">true</property> <!-- Specify Ignite as L2 cache provider. --> <property name="cache.region.factory_class">org.apache.ignite.cache.hibernate.HibernateRegionFactory</property> <!-- Specify the name of the grid, that will be used for second level caching. --> <property name="org.apache.ignite.hibernate.ignite_instance_name">hibernate-grid</property> <!-- Set default L2 cache access type. --> <property name="org.apache.ignite.hibernate.default_access_type">READ_ONLY</property> <!-- Specify the entity classes for mapping. --> <mapping class="com.mycompany.MyEntity1"/> <mapping class="com.mycompany.MyEntity2"/> <!-- Per-class L2 cache settings. --> <class-cache class="com.mycompany.MyEntity1" usage="read-only"/> <class-cache class="com.mycompany.MyEntity2" usage="read-only"/> <collection-cache collection="com.mycompany.MyEntity1.children" usage="read-only"/> ... </session-factory></hibernate-configuration>
这里,我们做以下工作:>启用L2缓存(也可以选择L2缓存统计数据生成)>指定点火为L2缓存实现。>指定缓存网格的名称(应该对应于ignite配置中的一个)。>为每个类指定实体类和配置缓存(对应于在ignite中配置的缓存区域)。
2.3 Ignite Configuration Example
Hibernate L2缓存的一个典型的点火配置如下:
<!-- Basic configuration for atomic cache. --><bean id="atomic-cache" class="org.apache.ignite.configuration.CacheConfiguration" abstract="true"> <property name="cacheMode" value="PARTITIONED"/> <property name="atomicityMode" value="ATOMIC"/> <property name="writeSynchronizationMode" value="FULL_SYNC"/></bean><!-- Basic configuration for transactional cache. --><bean id="transactional-cache" class="org.apache.ignite.configuration.CacheConfiguration" abstract="true"> <property name="cacheMode" value="PARTITIONED"/> <property name="atomicityMode" value="TRANSACTIONAL"/> <property name="writeSynchronizationMode" value="FULL_SYNC"/></bean><bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> <!-- Specify the name of the caching grid (should correspond to the one in Hibernate configuration). --> <property name="igniteInstanceName" value="hibernate-grid"/> ... <!-- Specify cache configuration for each L2 cache region (which corresponds to a full class name or a full association name). --> <property name="cacheConfiguration"> <list> <!-- Configurations for entity caches. --> <bean parent="transactional-cache"> <property name="name" value="com.mycompany.MyEntity1"/> </bean> <bean parent="transactional-cache"> <property name="name" value="com.mycompany.MyEntity2"/> </bean> <bean parent="transactional-cache"> <property name="name" value="com.mycompany.MyEntity1.children"/> </bean> <!-- Configuration for update timestamps cache. --> <bean parent="atomic-cache"> <property name="name" value="org.hibernate.cache.spi.UpdateTimestampsCache"/> </bean> <!-- Configuration for query result cache. --> <bean parent="atomic-cache"> <property name="name" value="org.hibernate.cache.internal.StandardQueryCache"/> </bean> </list> </property> ...</bean>
这里,我们为每个L2缓存区域指定缓存配置: >我们使用PARTITIONED 缓存来分隔缓存节点之间的数据。另一种可能的策略是启用REPLICATED 模式,从而在所有缓存节点之间复制一个完整的数据集。有关更多信息,请参见缓存分布模型。 >我们指定对应于L2缓存区域名称的缓存名称(一个完整的类名或一个完整的关联名称)。 >我们使用TRANSACTIONAL 原子性模式来利用缓存事务。 >我们启用FULL_SYNC与备份节点保持完全同步。此外,我们指定缓存用于更新时间戳,这可能是原子性的,以获得更好的性能。在配置了ignite缓存节点之后,我们可以从代码下面的方式开始:
Ignition.start("my-config-folder/my-ignite-configuration.xml");
执行上面的行之后,内部ignite节点启动,并且准备好要缓存的数据。我们还可以通过在控制台运行以下命令来启动其他独立节点:
$IGNITE_HOME/bin/ignite.sh my-config-folder/my-ignite-configuration.xml
对于Windows,在同一个文件夹中使用.bat脚本。PS:节点也可以在其他主机上启动,形成分布式缓存集群。确保在GridGain配置文件中指定正确的网络设置。
3.Query Cache
除了L2缓存之外,Hibernate还提供了一个查询缓存。这个缓存将查询的结果(HQL或Criteria)存储为给定的参数集,因此,当您使用相同的参数集重复查询时,它会在不访问数据库的情况下对缓存进行访问。如果有许多查询,查询缓存可能会有用,这些查询可能重复使用相同的参数值。就像L2缓存一样,Hibernate依赖于一个3 - rd的派对缓存实现,并点燃内存数据结构PS:考虑直接使用Apache Ignite SQL Grid,它应该比通过Hibernate调用更快地执行
4. Query Cache Configuration
以上的配置信息完全适用于查询缓存,但是需要进行一些额外的配置和代码更改。
4.1 Hibernate Configuration
为了在Hibernate中启用查询缓存,您只需要在配置文件中添加一条额外的行:
<!-- Enable query cache. --><property name="cache.use_query_cache">true</property>
然而,需要进行代码修改:对于想要缓存的每个查询,都应该通过setCacheable(true)启用cacheable :
Session ses = ...;// Create Criteria query.Criteria criteria = ses.createCriteria(cls);// Enable cacheable flag.criteria.setCacheable(true);...
完成此操作后,将缓存查询结果。
4.2 Ignite Configuration
为了在ignite中使Hibernate查询缓存可用,您需要指定一个附加的缓存配置:
<property name="cacheConfiguration"> <list> ... <!-- Query cache (refers to atomic cache defined in above example). --> <bean parent="atomic-cache"> <property name="name" value="org.hibernate.cache.internal.StandardQueryCache"/> </bean> </list></property>
注意,缓存是为更好的性能而进行的。
5. 样例
https://github.com/apache/ignite/blob/master/examples/src/main/java-lgpl/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java高兴了吧~~
阅读全文
0 0
- Hibernate L2 Cache 之 Ignite
- MyBatis L2 Cache ---ignite
- Cache, L2 Cache, CacheRangeFlush
- Hibernate之cache管理
- C64x L1 L2 cache
- 二级缓存(L2 CACHE)
- L1 L2 L3 cache
- Ignite
- GPU架构基础 之 L1 data cache & Unified L2 cache IN Fermi Arch
- Ignite Debug之put操作
- Cache vs EDMA+L2 SRAM ???
- 关闭L2 cache的方法
- Apache Ignite之集群应用测试
- hibernate cache
- hibernate cache
- Hibernate cache
- Hibernate Cache
- TMS320C64x DSP L1 L2 Cache架构
- Java注解学习
- Spring Cloud构建微服务架构:服务消费(基础)【Dalston版】
- 报数
- Make Your Own Neural Network简介
- SpringMVC + thymeleaf + html5 乱码种种坑
- Hibernate L2 Cache 之 Ignite
- java新手上路(一):输出三菱图标
- Struts2笔记06 封装获取表单数据方式
- ConcurrentModificationException 分析
- android 不同机型因录音权限出现的不同问题
- TCP握手与挥手
- linux tar 解压命令总结
- 织梦模板下载:政府供销社类部门织梦网站模板
- const关键字