SpringCache

官方文档:https://docs.spring.io/spring/docs/4.3.13.RELEASE/spring-framework-reference/htmlsingle/#cache

对应的XML配置文件:

<?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:cache="http://www.springframework.org/schema/cache"
       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">

    <cache:annotation-driven/>
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                    <property name="name" value="default"/>
                </bean>
            </set>
        </property>
    </bean>
    <bean id="cacheManager2" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
    </bean>

</beans>

 对应的实现:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by wangzhilei3 on 2017/12/27.
 */
@Service
@CacheConfig(cacheNames = "default", cacheManager = "cacheManager2")
public class CacheableService {
    private static final Logger log = LoggerFactory.getLogger(CacheableService.class);

    /**
     * 启用缓存
     *
     * @param l
     * @return
     */
    @Cacheable()
    public Date getDate(Long l) {
        log.info("缓存未生效");
        return new Date();
    }

    /**
     * 更新缓存
     *
     * @param l
     * @return
     */
    @CachePut()
    public Date putDate(Long l) {
        log.info("更新");
        return new Date();
    }

    /**
     * 删除缓存
     *
     * @param l
     */
    @CacheEvict()
    public void deleteDate(Long l) {
        log.info("删除");
    }

}

  增加 ”cacheNames“

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


/**
 * Created by wangzhilei3 on 2017/12/27.
 */
@Service
@CacheConfig(cacheNames = "default,sting",cacheManager = "cacheManager2")
public class CacheableServiceOfString {
    private static Logger logger = LoggerFactory.getLogger(CacheableServiceOfString.class);
    @Cacheable("sting")
    public String getDate(Long l){
        logger.info("缓存未生效");
        return "hello";
    }
}

  需要注意的是不同返回类型的方法共享同一个缓存(@Cacheable("sting")注解相同),当存在键冲突时,会产生类型转换异常。

当@CacheConfig(cacheNames = "default,sting",cacheManager = "cacheManager2")中的cacheNames 为多个,而方法注解@Cacheable()中的参数缺省时,两个缓存队列会同时生效,所以不同的返回类型要尽量避免采用相同的cacheName。

posted @ 2017-12-27 22:09  heapStark  阅读(213)  评论(0编辑  收藏  举报