官方文档: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。

SpringCache的更多相关文章

  1. SpringCache缓存初探

    body,table tr { background-color: #fff } table tr td,table tr th { border: 1px solid #ccc; text-alig ...

  2. SpringCache与redis集成,优雅的缓存解决方案

    缓存可以说是加速服务响应速度的一种非常有效并且简单的方式.在缓存领域,有很多知名的框架,如EhCache .Guava.HazelCast等.Redis作为key-value型数据库,由于他的这一特性 ...

  3. SpringCache @Cacheable 在同一个类中调用方法,导致缓存不生效的问题及解决办法

    由于项目需要使用SpringCache来做一点缓存,但自己之前没有使用过(其实是没有听过)SpringCache,于是,必须先学习之. 在网上找到一篇文章,比较好,就先学习了,地址是: https:/ ...

  4. SpringCache学习之操作redis

    一.redis快速入门 1.redis简介 在java领域,常见的四大缓存分别是ehcache,memcached,redis,guava-cache,其中redis与其他类型缓存相比,有着得天独厚的 ...

  5. SpringBoot基础系列-SpringCache使用

    原创文章,转载请标注出处:<SpringBoot基础系列-SpringCache使用> 一.概述 SpringCache本身是一个缓存体系的抽象实现,并没有具体的缓存能力,要使用Sprin ...

  6. spring-boot的spring-cache中的扩展redis缓存的ttl和key名

    原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...

  7. SpringCache实战遇坑

    1. SpringCache实战遇坑 1.1. pom 主要是以下两个 <dependency> <groupId>org.springframework.boot</g ...

  8. SpringCache学习实践

    1. SpringCache学习实践 1.1. 引用 <dependency> <groupId>org.springframework.boot</groupId> ...

  9. SpringBoot2.X + SpringCache + redis解决乱码问题

    环境:SpringBoot2.X + SpringCache + Redis Spring boot默认使用的是SimpleCacheConfiguration,使用ConcurrentMapCach ...

  10. AOP方法增强自身内部方法调用无效 SpringCache 例子

    开启注解@EnableCaChing,配置CacheManager,结合注解@Cacheable,@CacheEvit,@CachePut对数据进行缓存操作 缺点:内部调用,非Public方法上使用注 ...

随机推荐

  1. SpringBoot中执行定时任务

    一:在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. @SpringBootApplication @EnableSchedu ...

  2. 小tips:TCP的三次握手、长连接、 短连接、 SPDY 协议

    当网络通信时采用TCP协议时,在真正的读写操作之前,server与client之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时它们可以释放这个连接,连接的建立是需要三次握手的,而释放则需 ...

  3. Java 之 线程池

    一.线程池思想概述 如果使用线程的时候就去创建一个线程,这样实现起来非常简便,但是会出现一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低 ...

  4. docker + gitlab + jenkins 搭建 CI/CD 系统

    gitlab+jenkins+docker 计算机网络大全

  5. Golang: 解析JSON数据之一

    JSON 作为目前最流行的数据传输格式, 相信每个程序员都跟它打过交道吧.使用 Go 语言时,也不可避免的要操作 JSON 数据,令人惊喜的是,Go 内置了序列化和反序列化 JSON 的功能,今天就来 ...

  6. JavaScript基础内容中的函数详解

    函数 函数:即方法 函数就是一段预先设置的功能代码块,可以反复调用,根据输入参数的不同,返回不同的值. 为什么使用函数: 1.方便调用 2.代码重用,利于维护 3.便于修改,便于重构 4.简化逻辑,利 ...

  7. Centos6.5配置防火墙

    1.查看防火墙状态 [root@instance-xfl1djr7 ~]# /etc/init.d/iptables status 2.启动/关闭防火墙 开启防火墙 [root@instance-xf ...

  8. ZYNQ7000性能分析

    提到自动驾驶,机器人视觉,高清摄像机,都要想到摄像头这个单元,先前本侠也讲过一些FPGA应用在高清摄像头和机器视觉中的深度摄像头以及双目摄像头等,FPGA在里面的作用主要是对采集的图像进行处理,对图像 ...

  9. vimplus基本操作

    1. YouCompleteMe按tab键,自动补全 2. vim-commentary添加注释,以及取消注释gcc 注释当前行(普通模式)gc 可视模式下,注释当前选中的部分gcu 撤销上一次注释的 ...

  10. php单例型(singleton pattern)

    搞定,吃饭 <?php /* The purpose of singleton pattern is to restrict instantiation of class to a single ...