SpringBoot学习笔记-9:第九章 Spring Boot 与缓存

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: SpringBoot学习笔记-9:第九章 Spring Boot 与缓存

第九章 Spring Boot 与缓存

主要内容

JSR-107
Spring 缓存抽象
整合 Redis

1、JSR-107

Java Caching 定义了 5 个核心接口

CachingProvider:管理多个CacheManager
CacheManager:管理多个Cache
Cache:类似Map数据结构
Entry:存储在Cache中的key-value对
Expiry: 有效期
Application
    - CachingProvider
        - CacheManager
            - Cache
                Entry <-> Expiry

Spring 缓存抽象简介

重要概念

Cache 缓存接口,实现 RedisCache、EhCacheCache、ConcurrentMapCache
CacheManager 缓存管理器
@Cacheable 根据方法请求参数,对结果进行缓存
@CacheEvict 清空缓存
@CachePut 保证方法被调用,又希望结果被缓存
@EnableCaching 开启基于注解的缓存
keyGenerator 缓存数据时 key 的生成策略
serialize 缓存数据时 value 序列化策略

1、搭建环境

引入 Spring 依赖

Cache
Web
MySQL
MyBatis
  • 导入数据库文件,创建出 employee 和 department 表
  • 创建 javabean 封装数据
  • 整合 MyBatis 操作数据库
  • 配置数据源信息
  • 使用注解版 MyBatis
  • @MapperScan 指定需要扫描的 Mapper 接口所在包

2、使用缓存

  • 开启基于注解的缓存 @EnableCaching
  • 标注缓存注解 @Cacheable

@Cacheable

SpEL

methodName 方法名
method 方法
target 目标对象
targetClass 目标对象类
args 参数列表
caches 缓存列表
argumentname 参数名
result 返回值

参数

value 缓存组件名
key 缓存使用的key, 默认使用方法参数值
keyGenerator 指定key的生成器,和key二选一
cacheManager 缓存管理器
cacheResolver 缓存解析器 和缓存管理器二选一
condition 缓存条件 eg: #id>0
unless 否定缓存 条件为true不缓存
sync 使用同步

缓存工作原理

自动配置类

CacheAutoConfiguration

GenericCacheConfiguration"
JCacheCacheConfiguration"
EhCacheCacheConfiguration"
HazelcastCacheConfiguration"
InfinispanCacheConfiguration"
CouchbaseCacheConfiguration"
RedisCacheConfiguration"
CaffeineCacheConfiguration"
SimpleCacheConfiguration"
NoOpCacheConfiguration"

默认使用 SimpleCacheConfiguration

cacheManager:ConcurrentMapCacheManager

运行流程:

  1. 执行方法之前,先按照 cacheNames 获取 Cache 缓存组件,暗
    第一次获取缓存如果没有 cache 组件就创建
  2. 按照某种策略生成 key,查抄内容
  1. 没有查找到缓存就调用目标方法
  2. 目标方法换回的结果放进缓存中

SimpleKeyGenerator 默认策略

  • 如果没有参数 key = new SimpleKey()
  • 如果有一个参数 key = 参数值
  • 如果有多个参数 key - new SimpleKey(params)

核心:

  • 获取 CacheManger(默认 ConcurrentMapCacheManager)
  • 使用 KeyGenerator 生成 key 默认 SimpleKeyGenerator

自定义 key 的生成方式

package com.example.demo.config;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Method;
import java.util.Arrays;
@Configuration
public class MyCacheConfig {
    @Bean("myKeyGenerator")
    public KeyGenerator keyGenerator(){
        return new KeyGenerator(){
            @Override
            public Object generate(Object target, Method method, Object... params) {
                System.out.println("generate");
                return method.getName() + "[" + Arrays.asList(params).toString()+ "]";
            }
        };
    }
}
// keyGenerator和key二选一
// key 支持SpEL表达式
@Cacheable(value = "department", key = "#id")
public Department getById(Integer id){
    return departmentMapper.getById(id);
}
// keyGenerator
@Cacheable(value = "department", keyGenerator = "myKeyGenerator")
public Department getById(Integer id){
    return departmentMapper.getById(id);
}

@CachePut

@CachePut 既调用方法,又更新缓存数据

修改了数据库的某个数据,同时更新缓存

运行时机:先调用目标方法,再将方法的结果缓存

@CachePut(value = "department", key="#department.id")
public Department update(Department department){
    departmentMapper.update(department);
    return department;
}

@CacheEvict

@CacheEvict 缓存删除

参数

key 清除指定缓存
allEntries  清除所有缓存
beforeInvocation 清除缓存在方法运行之前执行 默认false=方法之后执行
// 清除单个缓存
@CacheEvict(value = "department", key="#id")
public String  delete(Integer id){
    return "success";
}
// 清除所有缓存
@CacheEvict(value = "department", allEntries = true)
    public String  deleteAllCache(){
        return "success";
    }

@Caching&@CacheConfig

public @interface Caching {
  Cacheable[] cacheable() default {};
  CachePut[] put() default {};
  CacheEvict[] evict() default {};
}

CacheConfig 抽取缓存公共配置

@CacheConfig(cacheNames={"department"})
@Service
public class DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    // 缓存数据
    @Cacheable(key = "#id")
    public Department getById(Integer id){
        return departmentMapper.getById(id);
    }
}

搭建 redis 环境&测试

本机安装

# 启动
$ redis-server
# 停止本机redis
$ redid-cli shutdown

可以使用 Docker 方式安装

https://hub.docker.com/

# 安装
docker pull redis
# 启动
docker run  -d -p 6379:6379  --name my-redis redis
# 查看运行状态
docker ps

Redis 命令

http://www.redis.cn/commands.html

RedisTemplate&序列化机制

引入依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

指定 redis 地址

spring:
  redis:
    host: localhost

redis 常见 5 中数据类型

String 字符串
List 列表
Set 集合
Hash 散列
ZSet 有序集合

测试使用

package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@SpringBootTest
public class RedisTest {
    // k-v字符串形式
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    // k-v对象形式
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void testRedis(){
        stringRedisTemplate.opsForValue().set("name", "Tom");
        String name = stringRedisTemplate.opsForValue().get("name");
        System.out.println(name);
    }
}

保存对象:序列化后再保存

1、手动转换数据为 json

2、自定义序列化规则

package com.example.demo.config;
import com.example.demo.pojo.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@Configuration
public class MyRedisConfig {
    @Bean
    public RedisTemplate<Object, Employee> employeeRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<Object, Employee> redisTemplate = new RedisTemplate<Object, Employee>();
        redisTemplate.setConnectionFactory(factory);
        // 设置序列化器
        Jackson2JsonRedisSerializer<Employee> serializer = new Jackson2JsonRedisSerializer<>(Employee.class);
        redisTemplate.setDefaultSerializer(serializer);
        return redisTemplate;
    }
}

测试

package com.example.demo;
import com.example.demo.pojo.Employee;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class RedisTest {
    @Autowired
    RedisTemplate<Object, Employee> employeeRedisTemplate;
    @Test
    public void testRedisSerializer(){
        Employee employee = new Employee();
        employee.setName("Tom");
        employee.setAge(23);
        employeeRedisTemplate.opsForValue().set("emp", employee);
    }
}

引入 redis-starter 之后,缓存组件使用 RedisCache

利用序列化保存数据,都是以 Object 形式保存,默认使用 jdk 序列化机制

可以保存为 json

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
4月前
|
缓存 NoSQL Java
什么是缓存?如何在 Spring Boot 中使用缓存框架
什么是缓存?如何在 Spring Boot 中使用缓存框架
163 0
|
7天前
|
前端开发 Java Nacos
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
本文介绍了如何使用Spring Cloud Alibaba 2023.0.0.0技术栈构建微服务网关,以应对微服务架构中流量治理与安全管控的复杂性。通过一个包含鉴权服务、文件服务和主服务的项目,详细讲解了网关的整合与功能开发。首先,通过统一路由配置,将所有请求集中到网关进行管理;其次,实现了限流防刷功能,防止恶意刷接口;最后,添加了登录鉴权机制,确保用户身份验证。整个过程结合Nacos注册中心,确保服务注册与配置管理的高效性。通过这些实践,帮助开发者更好地理解和应用微服务网关。
33 0
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
|
2月前
|
监控 Java 应用服务中间件
SpringBoot是如何简化Spring开发的,以及SpringBoot的特性以及源码分析
Spring Boot 通过简化配置、自动配置和嵌入式服务器等特性,大大简化了 Spring 应用的开发过程。它通过提供一系列 `starter` 依赖和开箱即用的默认配置,使开发者能够更专注于业务逻辑而非繁琐的配置。Spring Boot 的自动配置机制和强大的 Actuator 功能进一步提升了开发效率和应用的可维护性。通过对其源码的分析,可以更深入地理解其内部工作机制,从而更好地利用其特性进行开发。
50 6
|
2月前
|
缓存 安全 Java
Spring Boot 3 集成 Spring Security + JWT
本文详细介绍了如何使用Spring Boot 3和Spring Security集成JWT,实现前后端分离的安全认证概述了从入门到引入数据库,再到使用JWT的完整流程。列举了项目中用到的关键依赖,如MyBatis-Plus、Hutool等。简要提及了系统配置表、部门表、字典表等表结构。使用Hutool-jwt工具类进行JWT校验。配置忽略路径、禁用CSRF、添加JWT校验过滤器等。实现登录接口,返回token等信息。
523 12
|
2月前
|
存储 安全 Java
Spring Boot 3 集成Spring AOP实现系统日志记录
本文介绍了如何在Spring Boot 3中集成Spring AOP实现系统日志记录功能。通过定义`SysLog`注解和配置相应的AOP切面,可以在方法执行前后自动记录日志信息,包括操作的开始时间、结束时间、请求参数、返回结果、异常信息等,并将这些信息保存到数据库中。此外,还使用了`ThreadLocal`变量来存储每个线程独立的日志数据,确保线程安全。文中还展示了项目实战中的部分代码片段,以及基于Spring Boot 3 + Vue 3构建的快速开发框架的简介与内置功能列表。此框架结合了当前主流技术栈,提供了用户管理、权限控制、接口文档自动生成等多项实用特性。
93 8
|
3月前
|
负载均衡 Java 开发者
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
219 5
|
3月前
|
缓存 NoSQL Java
Spring Boot中的分布式缓存方案
Spring Boot提供了简便的方式来集成和使用分布式缓存。通过Redis和Memcached等缓存方案,可以显著提升应用的性能和扩展性。合理配置和优化缓存策略,可以有效避免常见的缓存问题,保证系统的稳定性和高效运行。
83 3
|
3月前
|
缓存 Java 数据库连接
深入探讨:Spring与MyBatis中的连接池与缓存机制
Spring 与 MyBatis 提供了强大的连接池和缓存机制,通过合理配置和使用这些机制,可以显著提升应用的性能和可扩展性。连接池通过复用数据库连接减少了连接创建和销毁的开销,而 MyBatis 的一级缓存和二级缓存则通过缓存查询结果减少了数据库访问次数。在实际应用中,结合具体的业务需求和系统架构,优化连接池和缓存的配置,是提升系统性能的重要手段。
168 4
|
4月前
|
前端开发 Java 开发者
Spring生态学习路径与源码深度探讨
【11月更文挑战第13天】Spring框架作为Java企业级开发中的核心框架,其丰富的生态系统和强大的功能吸引了无数开发者的关注。学习Spring生态不仅仅是掌握Spring Framework本身,更需要深入理解其周边组件和工具,以及源码的底层实现逻辑。本文将从Spring生态的学习路径入手,详细探讨如何系统地学习Spring,并深入解析各个重点的底层实现逻辑。
99 9
|
4月前
|
存储 缓存 Java
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
657 2

热门文章

最新文章