官方文档: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. 【转载】Jupyter Notebook 常用快捷键

    原文:http://blog.csdn.net/lawme/article/details/51034543 Jupyter Notebook 有两种键盘输入模式.编辑模式,允许你往单元中键入代码或文 ...

  2. [大数据学习研究] 错误排查,Hadoop集群部分DataNode不能启动

    错误现象 不知道什么原因,今天发现我的hadoop集群启动后datanode只有一台了,我的集群本来有三台的,怎么只剩一台了呢? 用jps命令检查一下,发现果然有两台机器的DataNode没有启动. ...

  3. 在win10、Ubuntu双系统下,卸载Ubuntu

    一.Win下确定ubuntu的磁盘分区 这个步骤是为了删除Ubuntu的系统分区,这种直接删除的方式来重新安装ubuntu的低版本比较省事. (1)右键计算机->管理->磁盘管理,打开磁盘 ...

  4. windows 下安装 node 以及安装 git 指令使用

    一.先百度搜索 node 找 Download | Node.js 二.下载 Node 根据你自己的操作系统下载 三.安装 node (注意:如果安装失败,使用管理员身份打开安装) 四.安装成功之后 ...

  5. 关于DataX

    1. 关于DataX 1.1. 前言 为什么写这篇文章,因为初出茅庐的时候,曾经遇到的一个面试官就是DataX的作者之一,而当时我还偏偏因为业务需求做了个数据库的同步工具,我当时不知道他做过这么专业的 ...

  6. 单词CAEMENT水泥CAEMENT英文

    caement Archaic spelling of cement. caement Alternative forms caement (archaic) c?ment (archaic) Hyp ...

  7. 你的MES今天升级了吗?

    你以为把MES装上了就完事了吗?NO NO NO!乔布斯先生曾讲过“你如果出色地完成了某件事,那你应该再做一些其他的精彩事儿.不要在前一件事上徘徊太久,想想接下来该做什么.” 目前大部分企业都已经完成 ...

  8. CDA数据分析【第一章:数据分析概述】

    一.数据分析行业发展 1.如何收集.保存.管理.分析.共享正在呈指数式增长的数据是我们必须要面对的一个重要挑战. 2.数据分析包括数据采集.数据存储.检查.清洗.分析.转换和建模等方法对数据进行处理的 ...

  9. Keras上实现简单线性回归模型

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/marsjhao/article/detai ...

  10. MySQL--mysqldmp命令参数set-gtid-purged

    在MySQL 5.7版本中使用mysqldump导出数据时,如果未显式指定set-gtid-purged参数,会报下面错误: Warning: A partial dump from a server ...