SpringCache缓存处理详解
作者:小钟要学习!!!
SpringCache缓存处理
学习过redis的都知道redis缓存是一个很不错的nosql,对于上线项目正的有很大的帮助,平常在java中使用一般都是通过 jedis 或者 spring-boot-starter-data-redis 所提供的依赖进行开发,具体的api还需要我们手动的去调用实现,下面的SpringCache将会为我们使用注解的形式来快速使用redis
1、SpringCache介绍
SpringCache是一个框架,实现了基于注解缓存功能,只需要简单地加一个注解,就能实现缓存功能。 SpringCache提高了一层抽象,底层可以切换不同的cache实现,具体就是通过cacheManager接口来统一不同的缓存技术 cacheManager是spring提供的各种缓存技术抽象接口 针对不同的缓存技术需要实现不同的cacheManager接口
| CacheManager | 描述 |
| EhCacheCacheManager | 使用EhCache作为缓存技术 |
| GuavaCaceManager | 使用Google的GuavaCache作为缓存技术 |
| RedisCacheManager | 使用Redis作为缓存技术 |
2、Spring Cache常用注解
| 注解 | 说明 |
| @EnableCaching | 开启缓存注解功能 |
| @Cacheable | 在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中 |
| @CachePut | 将方法的返回值放到缓存中 |
| @CacheEvict | 将一个或多条数据从缓存中删除 |
在springboot项目中,使用缓存技术只需要在项目中导入相关的缓存技术包,并在启动类上使用@EnableCaching注解开启缓存技术支持即可 例如:使用redis作为缓存技术,只需要导入spring-data-redis的maven坐标即可
3、SpringCache快速入门
下面几步的案例都是基于SpringCache底层的Map存储,当服务器重启的时候就会清空,在下面第4段的时候会讲解如何存储到redis
3.1、在启动类上开redis注解缓存技术
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
@Slf4j
@SpringBootApplication
@ServletComponentScan
@EnableCaching
public class ReggieApplication {
public static void main(String[] args) {
SpringApplication.run(ReggieApplication.class);
}
}3.2、在控制器中注入CacheManager
/** * 注入注解缓存接口 */ @Autowired private CacheManager cacheManager;
3.3、在具体的方法中使用注解完成缓存添加
这里的控制器使用的是mybatisplus框架提供的接口业务更具id修改数据。 对于注解的 key 是一个重点,详细的官方介绍如下
/**
* CachePut:将方法返回的值放入缓存中
* value:缓存的名称,每一个缓存名称下面可以有多个key
* key:缓存的key,重点灵活配置
*/
@CachePut(value = "userCache",key = "#users.id")
@GetMapping()
public User get(User user){
User users= dishService.updateById(user);
return users;
}用于动态计算密钥的 Spring 表达式语言 (SpEL) 表达式。 默认为 {@code “”},这意味着所有方法参数都被视为一个键,除非已设置自定义 {@link keyGenerator}。 SpEL 表达式根据提供以下元数据的专用上下文进行评估: {@code result} 用于对方法调用结果的引用。对于 {@code Optional} 等受支持的包装器,{@code result} 指的是实际对象,而不是包装器 {@code root.method}、{@code root.target} 和 {@ code root.caches} 分别用于引用 {@link java.lang.reflect.Method 方法}、目标对象和受影响的缓存。 方法名称的快捷方式({@code root. methodName})和目标类({@code root.targetClass})也可用。 方法参数可以通过索引访问。例如,可以通过 {@code root.args[1]}、{@code p1} 或 {@code a1} 访问第二个参数。如果该信息可用,也可以按名称访问参数。
3.4、冲缓存中删除数据【删除和修改方法使用】
下面列举了三种获取key值的方法,选择哪一种都可以
/**
* CacheEvict:清理指定缓存
* value:缓存的名称,每一个缓存名称下面可以有多个key
* key:缓存的key,重点灵活配置
*/
// @CacheEvict(value = "userCache",key = "#p0")
// @CacheEvict(value = "userCache",key = "#root.args[0]")
@CacheEvict(value = "userCache",key = "#id")
@DeletMapping("/{id}")
public void get(@PathVariable Long id){
dishService.removeById(id);
}3.5、查看缓存数据,没有就添加
如果缓存中有数据就不执行方法直接放回,没有缓存数据就添加,同时添加了一个判断条件,当返回值不为空的时候再进行添加缓存
/*
* unless:满足条件不缓存
* condition:满足条件时才缓存
*
*/
// @CacheEvict(value = "userCache",key = "#p0")
// @CacheEvict(value = "userCache",key = "#root.args[0]")
@Cacheable(value = "userCache",key = "#users.id",unless= "#result == bull")
@GetMapping("/{id}")
public User get(@PathVariable Long id){
User users = userService.getById(id);
return users;
}4、Spring Cache缓存到Redis中
4.1、导入maven坐标
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--使用注解完成缓存技术--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
这里的操作redis是必须要有 spring-boot-starter-data-redis 依赖包的

4.2、在application.yml配置文件中配置Redis
spring:
# 配置redis链接学习
redis:
host: localhost
port: 6379
database: 0 # 操作的库(有12个)
jedis:
pool:
max-active: 8 # 最大链接数据
max-wait: 1ms # 连接池最大阻塞等待时间
max-idle: 4 # 连接线中最大的空闲链接
min-idle: 0 # 连接池中最小空闲链接
cache:
redis:
time-to-live: 1800000 # 缓存的过期时间到此这篇关于SpringCache缓存处理详解的文章就介绍到这了,更多相关SpringCache缓存内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
