你好,游客 登录 注册 搜索
背景:
阅读新闻

Spring Cache集成EHCache

[日期:2016-09-12] 来源:Linux社区  作者:zhu_tianwei [字体:大 中 小]

EHCache支持内存和磁盘的缓存,支持LRU、LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件。同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用Gzip压缩提高响应速度。

可以通过spring集成提供缓存管理,spring为ehcache提供了如下的实现类:
EhCacheCache实现Cache接口
EhCacheCacheManager实现CacheManager接口
EhCache工厂管理类
EhCacheFactoryBean
EhCacheManagerFactoryBean
1.引入ehcache jar包


<dependency>
   <groupId>net.sf.ehcache</groupId>
   <artifactId>ehcache-core</artifactId>
   <version>2.6.6</version>
  </dependency>
2.ehcache配置


<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
 monitoring="autodetect">
 <!-- <diskStore path="java.io.tmpdir" /> -->
 <diskStore path="C:/cache_tmpdir" />
 
 <defaultCache maxElementsInMemory="10000" eternal="false"
  timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
  maxElementsOnDisk="10000000" diskPersistent="false"
  diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />

 <cache name="userCache" maxElementsInMemory="10000"
  maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
  diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"
  memoryStoreEvictionPolicy="LFU" />
 
</ehcache>
3.spring配置


<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
 <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
 <cache:annotation-driven cache-manager="cacheManager" />

 <bean id="userService" class="org.springframework.cache.demo.UserService" />

 <!-- cacheManager工厂类,指定ehcache.xml的位置 -->
 <bean id="cacheManagerFactory"
  class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
  p:configLocation="classpath:ehcache.xml" />

 <!-- 声明cacheManager -->
 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
  p:cacheManager-ref="cacheManagerFactory" />
</beans>

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/135125.htm

linux
相关资讯       EhCache  Spring cache 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款