官方文档: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. Java自学-数字与字符串 装箱和拆箱

    Java中基本类型的装箱和拆箱 步骤 1 : 封装类 所有的基本类型,都有对应的类类型 比如int对应的类是Integer 这种类就叫做封装类 package digit; public class ...

  2. Promise介绍及使用场景

    Promise 介绍 Promise 是一个构造函数,是异步编程的一种解决方案.所谓Promse,它本身就是一个容器,里面保存着异步操作的结果,对的,这和回调函数类似. Promise 容器本身不是异 ...

  3. RabbitMQ启动出现的问题与解决办法

    问题1 使用命令启动 service rabbitmq-server start 报错如下: Starting rabbitmq-server (via systemctl): Job for rab ...

  4. 为什么要将action实例设置为多例

    转载自 https://zhidao.baidu.com/question/622162406833405932.html struts2中action是多例的,即一个session产生一个actio ...

  5. 利用Metasploit攻击Android

    首先我在Kali下生成一个Android的应用程序,即apk格式的文件,用到的命令是: msfvenom -p android/meterpreter/reverse_tcp LHOST=本地ip L ...

  6. OneDrive,在云端

    应用场景 1.一份文档下班后还没编辑好,发送到自己的QQ/微信回家后继续编辑: 2.由于来回拷贝同一份文件,导致版本太多,忘记那个是最新版本了: 3.出门在外,客户突然需要一份重要文档,这份文件放在办 ...

  7. css overflow失效的原因

    声明 转载自https://my.oschina.net/xuqianwen/blog/540587 项目中常常有同学遇到这样的问题,现象是给元素设置了overflow:hidden,但超出容器的部分 ...

  8. 《团队名称》第八次团队作业:Alpha冲刺day2

    项目 内容 这个作业属于哪个课程 2016计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8-软件测试与ALPHA冲刺 团队名称 快活帮 作业学习目标 (1)掌握 ...

  9. 如何将 egg-validate 插件的错误提示,修改为中文?

    方案一(需要找到所有的提示并对应成中文,不是一个好方案:例子地址:https://github.com/cag2050/egg_mysql_demo/blob/master/app/controlle ...

  10. spring cloud 学习资料

    spring cloud 学习资料 网址 拜托!面试请不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA