肝完了,总结了SpringBoot与缓存的知识点,快速掌握

x33g5p2x  于2022-04-23 转载在 Spring  
字(5.0k)|赞(0)|评价(0)|浏览(203)

本章给大家带来的是SpringBoot和缓存的学习。同时已经录制了非常详细的视频,如果看文档较为吃力,可以结合视频进行学习,帮你快速掌握SringBoot与缓存。

目录

一、JSR107

二、Spring缓存抽象

三、几个重要概念&缓存注解

四、缓存使用

五、整合redis实现缓存

六、整合一个实例

一、JSR107

Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。

CachingProvider 定义了创建、配置、获取、管理和控制多个 CacheManager 。一个应用可以在运行期访问多个 CachingProvider 。
CacheManager 定义了创建、配置、获取、管理和控制多个唯一命名的 Cache ,这些 Cache 存在于 CacheManager 的上下文中。一个 CacheManager 仅被一个 CachingProvider 所拥有。
Cache 是一个类似 Map 的数据结构并临时存储以 Key 为索引的值。一个 Cache 仅被一个 CacheManager 所拥有。
Entry 是一个存储在 Cache 中的 key-value 对。
Expiry 每一个存储在 Cache 中的条目有一个定义的有效期。一旦超过这个时间,条目为过期的状态。一旦过期,条目将不可访问、更新和删除。缓存有效期可以通过 ExpiryPolicy 设置。
肝完了,总结了SpringBoot与缓存的知识点,快速掌握

二、Spring缓存抽象

Spring从3.1开始定义了
org.springframework.cache.Cache


org.springframework.cache.CacheManager接口来统一不同的缓存技术;

并支持使用JCache(JSR-107)注解简化我们的开发;

Cache 接口为缓存的组件规范定义,包含缓存的各种操作集合;
Cache 接口下 Spring 提供了各种 xxxCache 的实现;如 RedisCache , EhCacheCache , ConcurrentMapCache 等;
每次调用需要缓存功能的方法时, Spring 会检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。在此我向大家推荐一个架构学习交流圈。交流学习指导伪鑫:1253431195(里面有大量的面试题及答案)里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多
使用 Spring 缓存抽象时我们需要关注以下两点;
1、确定方法需要被缓存以及它们的缓存策略

2、从缓存中读取之前缓存存储的数据

肝完了,总结了SpringBoot与缓存的知识点,快速掌握

三、几个重要概念&缓存注解

肝完了,总结了SpringBoot与缓存的知识点,快速掌握

肝完了,总结了SpringBoot与缓存的知识点,快速掌握

SpEL语法的缓存格式

肝完了,总结了SpringBoot与缓存的知识点,快速掌握

四、缓存使用

• 1 、引入 spring-boot-starter-cache 模块

• 2 、 @ EnableCaching 开启缓存

• 3 、使用缓存注解

• 4 、切换为其他缓存

测试缓存对象:

五、整合redis实现缓存

\1. 引入 spring-boot-starter-data- redis 、 spring-data- redis

redis.clients

jedis

\2. 配置redis连接地址spring.redis.host=192.168.0.108

\3. 使用ReditTemplate操作redis

\1. redisTemplate.opsForValue ();// 操作字符串

\2. redisTemplate.opsForHash ();// 操作 hash

\3. redisTemplate.opsForList ();// 操作 list

\4. redisTemplate.opsForSet ();// 操作 set

\5. redisTemplate.opsForZSet ();// 操作有序 set

redisconfig实现:

@Configurationpublic class RedisConfig
{
//过期时间private Duration timeToLive = Duration.ofDays(1);
@Beanpublic RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory)
{
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(timeToLive).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer())).disableCachingNullValues();
RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory).cacheDefaults(configuration).transactionAware().build();
return redisCacheManager;
@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException
{
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(keySerializer());
template.setHashKeySerializer(keySerializer());
template.setValueSerializer(valueSerializer());
template.setHashValueSerializer(valueSerializer());
return template;
private RedisSerializer keySerializer()
{
return new StringRedisSerializer();
private RedisSerializer valueSerializer()
{
return new GenericJackson2JsonRedisSerializer();

application.properties的配置:

spring.datasource.username=springspring.datasource.password=spring
#开启驼峰命名匹配规则mybatis.configuration.map-underscore-to-camel-case=truelogging.level.com.dahaiwuliang.cache.mapper=debug
#redis连接spring.redis.host=192.168.0.108

六、整合一个实例

肝完了,总结了SpringBoot与缓存的知识点,快速掌握

controller层:

@RestControllerpublic class EmployeeController
{
@AutowiredEmployeeService employeeService;
@GetMapping(“/getEmployee”)
public Employee getEmployee(Integer id)
{
Employee employee = employeeService.getEmp(id);
return employee;
@GetMapping(“/updateEmployee”)
public Employee update(Employee employee)
{
Employee emp = employeeService.updateEmp(employee);
return emp;@GetMapping(“/deleteEmployee”)
public String deleteEmp(Integer id)
{
employeeService.deleteEmp(id);
return “success”;
@GetMapping(“/emp/{lastName}”)
public Employee getEmployeeByLastName(
@PathVariable(“lastName”) String lastName)
{
return employeeService.getEmployeeByLastName(lastName);

service层:

public Employee getEmp(Integer id)
{
System.out.println(“查询”+id+“员工”);
Employee employee = employeeMapper.getEmpById(id);
Cache cache = cacheManager.getCache(“emp”);
cache.put(“emp:”+id,employee);return employee;*
@CachePut:既调用方法,又更新缓存* 修改了数据库的数据,并同时更新缓存* 运行机制:* 1、先调用目标方法;* 2、将目标方法的结果缓存起来
@CachePut(/value = “emp”,/key=“#result.id”)
public Employee updateEmp(Employee employee)
{
System.out.println(“update:”+employee);
employeeMapper.updateEmp(employee);
return employee;*
@CacheEvict:清除缓存* allEntries=true清空这个缓存中的所有数据* beforeInvocation=true代表清空缓存操作是在方法执行前就执行了,无论方法是否出现异常,缓存都会被清除
@CacheEvict(/value = “emp”,/key = “#id”/,allEntries = true/,beforeInvocation = true)
public void deleteEmp(Integer id){System.out.println(“deletEmp:”+id);
//employeeMapper.deleteEmpById(id);
int a = 10/0;*
@Caching 定义复杂的缓存规则
@Caching(cacheable = {
@Cacheable(/value = “emp”,/key = “#lastName”)
},
put = {
@CachePut(/value = “emp”,/key = “#result.id”)
public Employee getEmployeeByLastName(String lastName)
{
System.out.println(“getEmployeeByLastName:”+lastName);
return employeeMapper.getEmpByLastName(lastName);

好了,讲解结束,小伙伴们点赞、收藏、评论,一键三连走起呀,下期见~

相关文章

微信公众号

最新文章

更多