十二:SpringBoot-基于Cache注解模式,管理Redis缓存
SpringBoot-基于Cache注解模式,管理Redis缓存
1、Cache缓存简介
- 从Spring3开始定义Cache和CacheManager接口来统一不同的缓存技术;
- Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
- Cache接口下Spring提供了各种缓存的实现;
- 如RedisCache,EhCacheCache ,ConcurrentMapCache等;
2、核心API说明
Cache缓存接口
定义缓存操作。实现有:RedisCache、EhCacheCache、ConcurrentMapCache等CacheManager
缓存管理器,管理各种缓存(cache)组件@Cacheable
主要针对方法配置,能够根据方法的请求参数对其进行缓存
Cacheable 执行流程
1)方法运行之前,按照cacheNames指定的名字先去查询Cache 缓存组件
2)第一次获取缓存如果没有Cache组件会自动创建
3)Cache中查找缓存的内容,使用一个key,默认就是方法的参数
4)key是按照某种策略生成的;默认是使用keyGenerator生成的,这里使用自定义配置
5)没有查到缓存就调用目标方法;
6)将目标方法返回的结果,放进缓存中
Cacheable 注解属性
cacheNames/value:指定方法返回结果使用的缓存组件的名字,可以指定多个缓存
key:缓存数据使用的key
key/keyGenerator:key的生成器,可以自定义
cacheManager:指定缓存管理器
cacheResolver:指定缓存解析器
condition:指定符合条件的数据才缓存
unless:否定缓存;当unless指定的条件为true,方法的返回值就不会被缓存
sync:是否使用异步模式
- @CacheEvict
清除缓存
CacheEvict:缓存清除
key:指定要清除的数据
allEntries = true:指定清除这个缓存中所有的数据
beforeInvocation = false:方法之前执行清除缓存,出现异常不执行
beforeInvocation = true:代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除
- @CachePut
保证方法被调用,又希望结果被缓存。
与@Cacheable区别在于是否每次都调用方法,常用于更新,写入
CachePut:执行方法且缓存方法执行的结果
修改了数据库的某个数据,同时更新缓存;
执行流程
1)先调用目标方法
2)然后将目标方法的结果缓存起来
@EnableCaching
开启基于注解的缓存keyGenerator
缓存数据时key生成策略@CacheConfig
统一配置本类的缓存注解的属性
3、SpringBoot整合Cache
3.1 核心依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3.2 Cache缓存配置
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Method;
@Configuration
public class CacheConfig {
/**
* 自定义 Cache 的 key 生成器
*/
@Bean("oneKeyGenerator")
public KeyGenerator getKeyGenerator (){
return new KeyGenerator() {
@Override
public Object generate(Object obj, Method method, Object... objects) {
return "KeyGenerator:"+method.getName();
}
} ;
}
}
3.3 启动类注解开启Cache
@EnableCaching // 开启Cache 缓存注解
@SpringBootApplication
public class CacheApplication {
public static void main(String[] args) {
SpringApplication.run(CacheApplication.class,args) ;
}
}
3.4 Cache注解使用代码
1)封装增删改查接口
import com.boot.cache.entity.User;
public interface UserService {
// 增、改、查、删
User addUser (User user) ;
User updateUser (Integer id) ;
User selectUser (Integer id) ;
void deleteUser (Integer id);
}
2)Cache注解使用案例
import com.boot.cache.entity.User;
import com.boot.cache.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
// 使用自定义的key生成策略
// 缓存结果key:addUser::KeyGenerator:addUser
@CachePut(value = "addUser",keyGenerator="oneKeyGenerator")
@Override
public User addUser(User user) {
return user ;
}
// 缓存结果key:updateUser::2
@CachePut(value = "updateUser",key = "#result.id")
@Override
public User updateUser(Integer id) {
User user = new User() ;
user.setId(id);
user.setName("smile");
return user;
}
// 缓存结果key: selectUser::3
@Cacheable(cacheNames = "selectUser",key = "#id")
@Override
public User selectUser(Integer id) {
User user = new User() ;
user.setId(id);
user.setName("cicadaSmile");
return user;
}
// 删除指定key: selectUser::3
@CacheEvict(value = "selectUser",key = "#id",beforeInvocation = true)
@Override
public void deleteUser(Integer id) {
}
}
3.5 测试代码块
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CacheApplication.class)
public class CacheTest {
@Resource
private UserService userService ;
// 分别测试:增、改、查、删,四个方法
@Test
public void testAdd (){
User user = new User() ;
user.setId(1);
user.setName("cicada");
userService.addUser(user) ;
}
@Test
public void testUpdate (){
userService.updateUser(2) ;
}
@Test
public void testSelect (){
userService.selectUser(3) ;
}
@Test
public void testDelete (){
userService.deleteUser(3) ;
}
}
十二:SpringBoot-基于Cache注解模式,管理Redis缓存的更多相关文章
- SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...
- 10.spring-boot基于角色的权限管理页面实现
10.spring-boot基于角色的权限管理页面实现
- spring-boot的spring-cache中的扩展redis缓存的ttl和key名
原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...
- Python 基于python+mysql浅谈redis缓存设计与数据库关联数据处理
基于python+mysql浅谈redis缓存设计与数据库关联数据处理 by:授客 QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3 ...
- springboot 基于@Scheduled注解 实现定时任务
前言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- spring与hibernate整合配置基于Annotation注解方式管理实务
1.配置数据源 数据库连接基本信息存放到properties文件中,因此先加载properties文件 <!-- jdbc连接信息 --> <context:property-pla ...
- MongoDB十二种最有效的模式设计【转】
持续关注MongoDB博客(https://www.mongodb.com/blog)的同学一定会留意到,技术大牛Daniel Coupal 和 Ken W. Alger ,从 今年 2月17 号开始 ...
- springboot系列总结(二)---springboot的常用注解
上一篇文章我们简单讲了一下@SpringBootApplication这个注解,申明让spring boot自动给程序进行必要的配置,他是一个组合注解,包含了@ComponentScan.@Confi ...
- ruby -- 进阶学习(十二)fragment cache
基于rails4.0环境 Rails 页面缓存的方法很多,最近弱弱地尝试了fragment cache,用法还算简单~@_@|| 首先,查看config/environment/production. ...
随机推荐
- 基于Opencv的简单图像处理
实验环境 本实验均在笔记本电脑完成,电脑的配置如表1所示: 系统 Windows 10 家庭版 处理器 英特尔 Core i5-6200 @ 2.30GHz 双核 主板 宏碁 Zoro_SL 内存 1 ...
- 看了CopyOnWriteArrayList后自己实现了一个CopyOnWriteHashMap
引言 面试官: 小伙子你有点眼熟啊,是不是去年来这面试过啊. 二胖: 啊,没有啊我这是第一次来这. 面试官: 行,那我们开始今天的面试吧,刚开始我们先来点简单的吧,java里面的容器你知道哪些啊,跟我 ...
- java函数方法学习
1.函数(方法)定义 类中特定功能小程序 2.函数定义格式 修饰符 返回值类型 函数名 (参数类型 形式参数) { 执行语句; return 返回值 } 函数功能实现的2个明确 1.这个功能的结果是什 ...
- Java学习日报8.5
package student;import java.util.*;public class student { Scanner sc=new Scanner(System.in); private ...
- golang unsafe.Pointer与uintptr
原文地址:https://blog.fanscore.cn/p/33/ 先说结论 uintptr 是一个地址数值,它不是指针,与地址上的对象没有引用关系,垃圾回收器不会因为有一个uintptr类型的值 ...
- 阿里云对象存储OSS及CDN加速配置
目录 十大云存储服务商 1. 登陆阿里云官网,开通对象存储服务 OSS 2. 创建存储空间 3. 绑定自定义域名 4. 配置阿里云CDN加速 5. 购买阿里云免费SSL证书 6. 阿里云CDN配置HT ...
- win7-win10 禁用IPV6临时地址
IPV6临时地址本意是保护设备隐私,但有时候需要暂时禁用的情景下指令 netsh interface ipv6 set privacy state=disable 启用则修改最后的状态值为enable ...
- 分享一个的c++写的,模仿awk的框架类CAwkDoc
这是我好多年前,模仿awk写的. awk大家都比较熟悉,使用awk处理文件,读取文件,分割字段这些工作awk自己帮你实现了. 程序员只要编写业务逻辑代码,并且awk还提供了很多常用的字符串操作函数,可 ...
- Mac上“您没有权限来打开应用程序”(Big Sur)
最近电脑更新了Macos的最新11版大苏尔 Big Sur.很快问题就出现了:安装某个软件的时候Key Gen打不开,提示您没有权限来打开应用程序,类似这样:https://zhuanlan.zhih ...
- tf.argmax(vector,axis)函数的使用
1.返回值 vector为向量,返回行或列的最大值的索引号: vector为矩阵,返回值是向量,返回每行或每列的最大值的索引号. 2.参数 vector为向量或者矩阵 axis = 0 或1 0:返回 ...