Ehcache学习笔记(1)--spring集成

一:spring集成ehcache
1、spring集成的ehcache是2.10.x版,jar包要对。

    <!--ehcache -->
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>2.10.0</version>
    </dependency>

2、spring 的xml配置

    <bean id="cache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml" />
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cache" />
    </bean>
    <cache:annotation-driven cache-manager="cacheManager"/>

3、ehcaceh的xml配置
该配置文件可以在net.sf.ehcache jar包下找到
在这里插入图片描述
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" dynamicConfig="true">

    <diskStore path="java.io.tmpdir"/>

    <defaultCache
            maxEntriesLocalHeap="1000"
            eternal="false"
            overflowToDisk="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskSpoolBufferSizeMB="30"
            maxEntriesLocalDisk="1000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>

    <cache name="demoCache"
           maxEntriesLocalHeap="1000"
           maxEntriesLocalDisk="100"
           eternal="false"
           diskSpoolBufferSizeMB="30"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
        <persistence strategy="localTempSwap"/>
    </cache>


</ehcache>

4、spring整合ehcache注解的使用demo

    @Cacheable(value="demoCache",key="#userName")
    public User findUserByUserName(String userName){
        System.out.println("--------- find user name -----------");
        return new User("ke","123654","cccp");
    }

    @CacheEvict(value = "demoCache",key = "#userName")
    public User removeName(String userName){
        System.out.println("--------- delete user name -----------");
        return new User("kexq","123654","2009");
    }

注解说明:
(1)@Cacheable(value=“demoCache”,key="#userName",condition="")
value:cacheName 不可为空;在ehcache.xml文件中必须存在的cacheNme
key:springEL表达式编写
condition:限制条件
(2)@CacheEvict
去除缓存
(3)@CachePut
不管对应缓存是否存在,都执行方法体,并且将返回的内容写入缓存
(4)@CacheConfig(value=“cacheName”)
类级别的注解,若类中使用缓存是同一个,可以配置在此注解中
5、demoTest

    @Resource
    private LoginService loginService;

    @RequestMapping("index.do")
    public String index(){
        //第一次调用时缓存,第二次调用直接取缓存
        loginService.findUserByUserName("ke");
        loginService.findUserByUserName("ke");

        loginService.removeName("ke");
        // 删除后再次调用时,再次缓存
        System.out.println(loginService.findUserByUserName("ke"));

        return "index";
    }

猜你喜欢

转载自blog.csdn.net/cccp_2009/article/details/82803051