spring注解配置ehcache

spring采用注解方式配置ehcache2.x版本

导入jar包:

 <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>2.7.2</version>
    </dependency>

2.ehcache.xml放入resources下

<?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/ehcache"/>
    
   <!--  默认的管理策略 
	maxElementsOnDisk: 在磁盘上缓存的element的最大数目,默认值为0,表示不限制。 
	eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
	diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。 
	diskExpiryThreadIntervalSeconds:对象检测线程运行时间间隔。标识对象状态(过期/持久化)的线程多长时间运行一次。 
	-->
    <defaultCache maxElementsInMemory="10000"
                  eternal="false"
                  timeToIdleSeconds="300"
                  timeToLiveSeconds="10"
                  overflowToDisk="false"
                  diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU"/>
    
     <cache name="userCache"
           maxElementsInMemory="2000"
           eternal="false"
           overflowToDisk="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="1800"
           memoryStoreEvictionPolicy="LFU">
     </cache>
     
     <cache name="ehCache"
           maxElementsInMemory="100000"
           eternal="false"
           overflowToDisk="false"
           timeToIdleSeconds="1800"
           timeToLiveSeconds="3600"
           memoryStoreEvictionPolicy="LFU">
     </cache>
     
    <!-- 组播方式:multicastGroupPort需要保证与其他系统不重复,进行端口注册  -->
	<!-- 若因未注册,配置了重复端口,造成权限缓存数据异常,请自行解决  -->
   <!--  
    <cacheManagerPeerProviderFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
            properties="peerDiscovery=automatic,
                        multicastGroupAddress=230.0.0.1,
                        multicastGroupPort=4546, timeToLive=1"/>-->
     
<!-- replicatePuts=true | false – 当一个新元素增加到缓存中的时候是否要复制到其他的peers. 默认是true。 -->
<!-- replicateUpdates=true | false – 当一个已经在缓存中存在的元素被覆盖时是否要进行复制。默认是true。 -->
<!-- replicateRemovals= true | false – 当元素移除的时候是否进行复制。默认是true。 -->
<!-- replicateAsynchronously=true | false – 复制方式是异步的(指定为true时)还是同步的(指定为false时)。默认是true。 -->
<!-- replicatePutsViaCopy=true | false – 当一个新增元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。 -->
<!-- replicateUpdatesViaCopy=true | false – 当一个元素被拷贝到其他的cache中时是否进行复制(指定为true时为复制),默认是true。 -->
 
</ehcache>

编写ehcache配置类

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheFactoryBean;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

/**
 * 〈ehcacheConfig配置类〉
 *
 * @author 
 * @create 2018/7/30
 * @since 1.0.0
 */
@Configuration
@EnableCaching//开始缓存
public class EhCacheConfig {


    /**
     * ehCache 缓存配置管理
     * @return
     */
    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource ehcacheResource = resolver.getResource("classpath:ehcache.xml");
        factoryBean.setConfigLocation(ehcacheResource);
        factoryBean.setShared(true);
        factoryBean.setCacheManagerName("ehcacheManager");

        return factoryBean;

    }

    /**
     * 对应ehcache.xml中的ehCache配置
     * @return
     */
    @Bean
    public EhCacheFactoryBean ehCache(){
        EhCacheFactoryBean ehCacheFactoryBean = new EhCacheFactoryBean();
        ehCacheFactoryBean.setCacheName("ehCache");
        ehCacheFactoryBean.setCacheManager(ehCacheManagerFactoryBean().getObject());
        return ehCacheFactoryBean;
    }


    /**
     * 对应ehcache.xml中的userCache配置
     * @return
     */
    @Bean
    public EhCacheFactoryBean userCache(){
        EhCacheFactoryBean ehCacheFactoryBean = new EhCacheFactoryBean();
        ehCacheFactoryBean.setCacheName("userCache");
        ehCacheFactoryBean.setCacheManager(ehCacheManagerFactoryBean().getObject());
        return ehCacheFactoryBean;
    }

}

2.使用缓存

/*
*    userCache 对应配置的userCache
*/  
@Cacheable(value="userCache", key = "'customer:'+#uid" ,condition = "true ")
    public Map<String, Object> getXXX(String uid) {
        logger.info("缓存中没有,去DB中存储,并放入缓存");
       ......
    }

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值