Ehcache缓存

Ehcache缓存

一 . 介绍
Ehcache是现在最流行的纯java开源缓存框架,配置简单,结构清晰,功能强大。
二 . 特性
三 . 使用

  1. 加入jar包

    <dependency>
    	<groupId>com.googlecode.ehcache-spring-annotations</groupId>
    	<artifactId>ehcache-spring-annotations</artifactId>
    	<version>1.1.2</version>
    </dependency>
    <dependency>
    	<groupId>cglib</groupId>
    	<artifactId>cglib-nodep</artifactId>
    	<version>3.1</version>
    </dependency>
    <dependency>
    	<groupId>net.sf.ehcache</groupId>
    	<artifactId>ehcache-core</artifactId>
    	<version>2.1.0</version>
    </dependency>
    
  2. ehcache.xml

  <?xml version="1.0" encoding="UTF-8"?> 
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" 
      updateCheck="false"> 
           <diskStore path="java.io.tmpdir" /> 
           <defaultCache eternal="false" maxElementsInMemory="1000" 
           overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" 
           timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" /> 
           <cache name="testCache" eternal="false" maxElementsInMemory="100" 
           overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" 
           timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> 
    </ehcache>

timeToLiveSeconds=“600” : 缓存自创建日期起至失效时的间隔时间;

  1. 在spring配置文件中配置
<beans 
 xmlns:cache="http://www.springframework.org/schema/cache"
 xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"
 >
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven cache-manager="ehcacheManager"/>
<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
<bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<!-- 声明cacheManager -->
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManagerFactory" />
</bean> 
  1. 在service接口上写@Cacheable注解
   @Cacheable(value="testTable")
    	List<Center> GetDataByTableName();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值