spring4.x hibernate4.x 整合 ehcache 注解 annotate
来源:互联网 发布:演唱会票从淘宝哪里要 编辑:程序博客网 时间:2024/05/21 01:28
spring4.x hibernate4.x 整合 ehcache 注解 annotate
spring4.x hibernate4.x 整合 ehcache 基于 注解 annotate
废话不说 直接贴源码链接 : https://git.oschina.net/alexgaoyh/alexgaoyh.git
使用 ehcache 来提高系统的性能,现在用的非常多, 也支持分布式的缓存,在 hibernate 当中作为二级缓存的实现产品,可以提高查询性能。
pom.xml
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.1.6.Final</version> </dependency>
在项目的 src 下面添加 ehcache 的配置文件 ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!-- Subdirectories can be specified below the property e.g. java.io.tmpdir/one --> <diskStore path="java.io.tmpdir"/> <!-- Mandatory Default Cache configuration. These settings will be applied to caches created programmtically using CacheManager.add(String cacheName) --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="org.hibernate.cache.spi.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true" overflowToDisk="true" /> <cache name="org.hibernate.cache.internal.StandardQueryCache" maxElementsInMemory="10000" eternal="false" timeToLiveSeconds="120" overflowToDisk="true" /> <!-- java文件注解查找cache方法名的策略:如果不指定java文件注解中的region="ehcache.xml中的name的属性值", 则使用name名为com.lysoft.bean.user.User的cache(即类的全路径名称), 如果不存在与类名匹配的cache名称, 则用 defaultCache 如果User包含set集合, 则需要另行指定其cache 例如User包含citySet集合, 则也需要 添加配置到ehcache.xml中 --> <cache name="javaClassName" maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> </ehcache>
在 spring 集成 hibernate 的配置文件中,添加如下配置
<!-- 开启查询缓存 --><prop key="hibernate.cache.use_query_cache">true</prop><!-- 开启二级缓存 --><prop key="hibernate.cache.use_second_level_cache">true</prop><!-- 高速缓存提供程序 --> <!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 --><prop key="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
Spring 也使用 ehcache, 所以也需要在 spring 配置文件中添加 ehcache 的配置
<!-- cacheManager, 指定ehcache.xml的位置 --> <bean id="cacheManagerEhcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>classpath:ehcache.xml</value> </property> <!-- 由于hibernate也使用了Ehcache, 保证双方都使用同一个缓存管理器 --> <property name="shared" value="true"/> </bean>
在类中定义:
@Entity @Table(name = "t_user") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="javaClassName") public class User implements Serializable { }
默认情况下二级缓存只会对load get 之类的方法缓存, 想list iterator 之类的方法也使用缓存 必须跟查询缓存一起使用, 重写查询方法
.setCacheable(true)
criteria.setCacheable(true).list();
之后进行验证
0 0
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
- SPRING4.X HIBERNATE4.X 整合 EHCACHE 注解 ANNOTATE
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
- Spring4.x+Hibernate4.x整合
- Spring4.x+Hibernate4.x整合出现的问题
- spring4+hibernate4 基于注解整合
- 使用注解配置ehcache,环境:Spring4、hibernate4
- spring4.x + hibernate4.x 配置详解
- spring4.x + hibernate4.x 配置详解
- spring4.x + hibernate4.x 配置详解
- spring4.x + hibernate4.x 配置详解
- Spring4.x+Struts2.x整合
- spring4.x注解概述
- spring4.x注解概述
- spring4.x注解概述
- spring4.x注解详述
- spring4.x注解概述
- 【git】【开源中国】【coding.net…
- 【git】【开源中国】【coding.net…
- OTG(usb2.0)强制Host调试全记录
- 【git】【开源中国】【coding.net…
- 【html5-css3游戏开发】js框架选择
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
- 【seo】网站优化从title,keyword,d…
- 【js_javascript函数】eval强制计…
- 【mac apache】今天把mac升级…
- 【linux系统】ubuntu下设置静态固…
- ADF中af:forEach和af:iterator标签的区别
- 10000电信固话如何设置呼叫转移
- 【git】放弃本地修改强制下载拉取…
- 【git】放弃本地修改强制下载拉取…