配置

1. applicationContext.xml

<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.xsdhttp://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"p:name="default"/><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"p:name="activityCache"/><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"p:name="awardsCache"/></set></property></bean>

Spring内部默认使用 ConcurrentHashMap 来存储, 配置中的 activityCache awardsCache 就是一个一个的 ConcurrentHashMap 对象的名字. 另外 spring还支持使用 EHCache 来存储缓存.

2. 在service的实现类上加上 @Cacheable

//@Service
//public class LotteryActivityServiceImpl implements LotteryActivityService@Cacheable(value = "activityCache", key = "#shortName")
public LotteryActivity findByShortName(String shortName) {log.info("query activity : {} from database.", shortName);return activityRepository.findByShortName(shortName);
}

@Cacheable参数

value   缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如:@Cacheable(value=”mycache”) 或者 
@Cacheable(value={”cache1”,”cache2”}
key   缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 例如:
@Cacheable(value=”testcache”,key=”#userName”)
condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 例如:
@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

在需要清除缓存的方法上加上@CacheEvict

@CacheEvict(value="activityCache", allEntries = true, beforeInvocation = true)
public void cleanActivityCache(String shortName) {log.info("cleaned cache activity : {}", shortName);
}

@CacheEvict 参数

value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如:
@CachEvict(value=”mycache”) 或者 
@CachEvict(value={”cache1”,”cache2”}
key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合 例如:
@CachEvict(value=”testcache”,key=”#userName”
condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才清空缓存 例如:
@CachEvict(value=”testcache”,
condition=”#userName.length()>2”)
allEntries 是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存 例如:
@CachEvict(value=”testcache”,allEntries=true)
beforeInvocation 是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存  例如:
@CachEvict(value=”testcache”,beforeInvocation=true)

当需要保证方法被调用,又希望结果被缓存, 可以使用@CachePut

@CachePut(value="accountCache",key="#account.getName()")// 更新 accountCache 缓存public Account updateAccount(Account account) { return updateDB(account); } 

@CachePut 参数

value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个  例如:
@Cacheable(value=”mycache”) 或者 
@Cacheable(value={”cache1”,”cache2”}
key 缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合  例如:
@Cacheable(value=”testcache”,key=”#userName”)
condition 缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存  例如:
@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

注解最好加在实现类而不是接口的方法上

Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Cache* annotation, as opposed to annotating interfaces. You certainly can place the @Cache* annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies (proxy-target-class="true") or the weaving-based aspect (mode="aspectj"), then the caching settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a caching proxy, which would be decidedly bad.

带有@Cache* 注解的方法不能被定义在调用该方法的类里, 比如 UserController要调用 findUserByName(String name), 且该方法有 @Cacheabele注解, 那么该方法就不能被定义在 UserController中

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual caching at runtime even if the invoked method is marked with @Cacheable - considering using the aspectj mode in this case.

@Cache*注解要加在 public 方法上

When using proxies, you should apply the @Cache* annotations only to methods with public visibility. If you do annotate protected, private or package-visible methods with these annotations, no error is raised, but the annotated method does not exhibit the configured caching settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods as it changes the bytecode itself.

转载于:https://www.cnblogs.com/ykt8465279130/p/3521557.html

Spring Cache 配置及一些问题的解决相关推荐

  1. springboot整合spring @Cache和Redis

    转载自  springboot整合spring @Cache和Redis spring基于注解的缓存 对于缓存声明,spring的缓存提供了一组java注解: @Cacheable:触发缓存写入. @ ...

  2. spring boot 整合 spring cache 简单使用

    spring boot 整合 spring cache 简单使用 spring cache简介 使用spring cache spring cache简介 Spring 3.1起,提供了基于注解的对C ...

  3. Spring Cache抽象-基于XML的配置声明(基于EhCache的配置)

    概述 完整示例 pomxml增加依赖 数据库表数据Oracle 实体类 服务层 ehcache的配置文件 Spring-EhCache配置文件 单元测试 日志输出 日志分析 示例源码 概述 首先请阅读 ...

  4. Spring Cache抽象-基于XML的配置声明(基于ConcurrentMap的配置)

    概述 示例 项目结构 数据库表数据Oracle 实体类 服务层 Spring配置文件 单元测试 日志输出 日志分析 示例源码 概述 Spring Cache基于注解的配置 如果不想使用注解或者由于其他 ...

  5. idea提示未配置 Spring Boot 配置注解处理器解决方法

    未配置 Spring Boot 配置注解处理器 解决方法: 在pom.xml里添加依赖 <dependency><groupId>org.springframework.boo ...

  6. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  7. Spring Cache抽象-缓存注解

    文章目录 概述 Spring缓存的基本原理 @Cacheable :主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 键生成器 带条件的缓存 @Cacheable 注解参数说明 示例-缓存管理 ...

  8. Spring Cache

    在WEB后端应用程序来说,耗时比较大的往往有两个地方:一个是查数据库,一个是调用其它服务的API(因为其它服务最终也要去做查数据库等耗时操作).重复查询也有两种.一种是我们在应用程序中代码写得不好,写 ...

  9. java 项目做多级缓存_【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)...

    一.缓存 当系统的并发量上来了,如果我们频繁地去访问数据库,那么会使数据库的压力不断增大,在高峰时甚至可以出现数据库崩溃的现象.所以一般我们会使用缓存来解决这个数据库并发访问问题,用户访问进来,会先从 ...

最新文章

  1. 如何创建一个定时管理的页面
  2. python入门作业编程题-Python编程:从入门到实践——【作业】——第三章(列表)...
  3. JVM内存模型知识点总结
  4. python-类方法和属性
  5. Java 200+ 面试题补充② Netty 模块
  6. perl亲身试验ini---使用perl读写配置文件
  7. Eclipse里编辑代码,进度条出现“Remote System Explorer Operation”解决方法
  8. 数据类型(简单数据类型、简单数据类型传参、复杂数据类型传参)
  9. 手机麦克风结构原理图_一文看懂咪头的工作原理及结构(驻极体话筒)
  10. TAOCP-Reading-计算机程序设计艺术阅读-1-2
  11. MAC将latex等大软件安装到移动硬盘
  12. 6.9 齐次线性方程组
  13. TCP/IP(三):ARP报文格式详解
  14. python中value的含义_python中value的意思
  15. mysql datesub interval_Mysql之INTERVAL与DATE_SUB与EXTRACT函数的使用
  16. 【操作系统】BIOS篇
  17. ORACLE-EBS常用表
  18. 如何在面试中回答 “你最大的缺点是什么?”
  19. Android学习网站推荐
  20. 如何临时修改ip地址,永久修改ip地址

热门文章

  1. LeetCode MySQL 571. 给定数字的频率查询中位数
  2. LeetCode 410. 分割数组的最大值(极小极大化 二分查找 / DP)
  3. 数据结构--红黑树 Red Black Tree
  4. mysql 停止同步_MYSQL从库数据冲突导致同步停止
  5. python中字符串注意事项
  6. python写名片管理系统_Python实现名片管理系统
  7. 史上最大多模态图文数据集发布!
  8. 别让数据坑了你!用置信学习找出错误标注(附开源实现)
  9. 史上最萌最认真的机器学习/深度学习/模式识别入门指导手册(二)
  10. Lego-美团接口自动化测试实践