spring mvc注入形式使用ehcache

1.需要jar

     spring相关jar

     ehcache-spring-annotations-1.1.2

     cglib-2.2.jar

2.配置文件(部分配置文件,相信大家都能看懂)

   

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
 xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring  
  http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

 <import resource="applicationContext-data.xml"/>
 
 <ehcache:annotation-driven cache-manager="ehCacheManager" />
 
  <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
       <property name="configLocation" value="classpath:ehcache.xml" /> 
   </bean>

 <bean id="myKeyGenerator" class="com.lenovo.leme.tool.MyKeyGenerator"/> 

 

3.ehcache.xml

  

<?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"/>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
  <cache name="goodsInstanceCache"
           maxElementsInMemory="10000"
           maxElementsOnDisk="1000"
           eternal="false"
           overflowToDisk="true"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
            />
</ehcache>


 4.java代码中应用

   
 @Cacheable(cacheName="goodsInstanceCache", keyGeneratorName = "myKeyGenerator")
 public Page getAllGoods(Page page,Map<String,String> map){
      方法内容

}

 

public class MyKeyGenerator implements CacheKeyGenerator<Serializable>{

@Override
 public Serializable generateKey(MethodInvocation methodInvocation) {

}

}

注意。红色字体对应关系,是为了设置cache中的key,加入碰到分页,就需要把分页的参数放到key中来区分分页

          此种cache主要是对一个方法的返回值进行缓存,但是因为分页,所以必须自定义key来区分每一页

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值