官方文档: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. Vue搭建脚手架2

    Vue2.0搭建Vue脚手架(vue-cli) 在网上找了很多的搭建脚手架教程,但都不求甚解.终于找到2个比较好的教程,读者可对比阅读1和2,在这里分享给大家,希望对初学者有所帮助.ps:高手请绕道. ...

  2. 给任意view设置圆角outline.setRoundRect()方法实现

    效果图,给banner设置圆角,1没有直接修改imageView而是使用自带方法进行设置 具体方法如下 //圆角方法 @TargetApi(Build.VERSION_CODES.LOLLIPOP) ...

  3. Oracle数据表字段小写转大写

    BEGIN FOR c IN ( SELECT COLUMN_NAME cn FROM all_tab_columns WHERE table_name = '表名' ) loop BEGIN exe ...

  4. java操作excel-----poi

    一.所需依赖包 1.使用maven会自动导入相关依赖,所以只需要导入2007版的的包,其他包自动导入,包括2003所需jar包. <dependency> <groupId>o ...

  5. go中如何更好的迭代

    三种迭代方式 3 ways to iterate in Go 有如下三种迭代的写法: 回调函数方式迭代 通过Next()方法迭代.参照python 迭代器的概念,自定义Next()方法来迭代 通过ch ...

  6. mysql数据库SQL执行分析,优化前必备分析

    概述 一般我们在对mysql数据库做优化,肯定需要对慢sql去做分析才能开始优化,那么有什么分析的方法呢?下面通过对sql执行时间和执行情况来做分析. 一.SQL 执行时间分析 通过找到执行时间长的 ...

  7. ELK快速入门(一)基本部署

    ELK快速入门一-基本部署 ELK简介 什么是ELK?通俗来讲,ELK是由Elasticsearch.Logstash.Kibana 三个开源软件组成的一个组合体,这三个软件当中,每个软件用于完成不同 ...

  8. 个人作业第五次——Alpha项目测试

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/2019autumnsystemanalysisanddesign/ 这个作业的要求在哪里 https:/ ...

  9. 多态典型用例之virtual

    多态典型用例之virtual 参考:https://www.cnblogs.com/dormant/p/5223215.html 1.虚函数(virtual) (1)在某基类中声明为 virtual ...

  10. npm install 和 npm ci 的主要区别

    npm install 和 npm ci 的主要区别: 该项目必须有一个 package-lock.json 或 npm-shrinkwrap.json. 如果 package-lock.json 中 ...