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

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

@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

相关文章
|
2月前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
2月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
2月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
446 2
|
3月前
|
人工智能 Java 机器人
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
Spring AI Alibaba集成Ollama,基于Java构建本地大模型应用,支持流式对话、knife4j接口可视化,实现高隐私、免API密钥的离线AI服务。
2876 1
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
存储 JSON Java
540 0
|
3月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
1411 0
|
4月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
820 3
|
8月前
|
前端开发 Java Maven
Spring 和 Spring Boot 之间的比较
本文对比了标准Spring框架与Spring Boot的区别,重点分析两者在模块使用(如MVC、Security)上的差异。Spring提供全面的Java开发基础设施支持,包含依赖注入和多种开箱即用的模块;而Spring Boot作为Spring的扩展,通过自动配置、嵌入式服务器等功能简化开发流程。文章还探讨了两者的Maven依赖、Mvc配置、模板引擎配置、启动方式及打包部署等方面的异同,展示了Spring Boot如何通过减少样板代码和配置提升开发效率。总结指出,Spring Boot是Spring的增强版,使应用开发、测试与部署更加便捷高效。
1206 11