SpringBoot整合缓存 ~ 整合Redis缓存和序列化

推荐文章阅读

Click me to read SpringBoot与缓存 ~ 概念、注解、整合Redis、分布式锁

Click me to read SpringBoot整合缓存 ~ SpringBoot缓存工作原理以及@Cacheable运行流程

Click me to read SpringBoot整合缓存 ~ 整合Redis缓存和序列化

Click me to download

前言

授 人 以 鱼 不 如 授 人 以 渔 , 核 心 在 于 思 想 \color{red}授人以鱼不如授人以渔,核心在于思想
网 上 每 个 人 都 是 讲 整 合 步 骤 1 / 2 / 3 列 出 来 , 没 有 教 大 家 如 何 去 找 这 个 方 法 以 及 配 置 , \color{red}网上每个人都是讲整合步骤1/2/3列出来,没有教大家如何去找这个方法以及配置, 1/2/3
这 里 把 方 法 分 享 给 大 家 , 希 望 对 大 家 有 帮 助 \color{red}这里把方法分享给大家,希望对大家有帮助

整合redis缓存 ~ 版本:1.5.12

  1. 搭建redis服务器
    如 何 搭 建 r e d i s 不 在 这 里 展 开 , 不 是 重 点 \color{red}如何搭建redis不在这里展开,不是重点 redis
    在这里插入图片描述

2、引入pom依赖,如何找这个依赖呢?
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

https://docs.spring.io/spring-boot/docs/2.1.18.RELEASE/reference/html/
改为
https://docs.spring.io/spring-boot/docs/1.5.12.RELEASE/reference/html/

方法有点low,不过是真的找不到1.5.12的版本文档入口呀

找到:13.5. Starters
在这里插入图片描述
在这里插入图片描述

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

上 面 啰 嗦 了 那 么 多 , 就 是 引 入 了 该 依 赖 , 重 要 的 是 学 习 该 思 想 o k \color{#f6941d}上面啰嗦了那么多,就是引入了该依赖,重要的是学习该思想ok ok

  1. 使用@EnableCaching注解

@SpringBootApplication
@EnableCaching
@MapperScan("csdn.xiaozheng.springbootcachexiaozheng.mapper")
public class SpringBootCacheXiaozhengApplication {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(SpringBootCacheXiaozhengApplication.class, args);
	}

}

  1. 配置redis,redis支持哪些配置呢
Common application properties

或者搜搜

扫描二维码关注公众号,回复: 12717492 查看本文章
spring.redis.host

在这里插入图片描述

# REDIS (RedisProperties)
spring.redis.cluster.max-redirects= # Maximum number of redirects to follow when executing commands across the cluster.
spring.redis.cluster.nodes= # Comma-separated list of "host:port" pairs to bootstrap from.
spring.redis.database=0 # Database index used by the connection factory.
spring.redis.url= # Connection URL, will override host, port and password (user will be ignored), e.g. redis://user:password@example.com:6379
spring.redis.host=localhost # Redis server host.
spring.redis.password= # Login password of the redis server.
spring.redis.ssl=false # Enable SSL support.
spring.redis.pool.max-active=8 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
spring.redis.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.port=6379 # Redis server port.
spring.redis.sentinel.master= # Name of Redis server.
spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs.
spring.redis.timeout=0 # Connection timeout in milliseconds.

在这里插入图片描述
这样你就知道支持哪些配置了

spring.redis.host=127.0.0.1
整合完毕,测试
package csdn.xiaozheng.springbootcachexiaozheng;

import csdn.xiaozheng.springbootcachexiaozheng.domain.Department;
import csdn.xiaozheng.springbootcachexiaozheng.service.DepartmentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@EnableCaching
@RunWith(SpringRunner.class)
public class SpringBootCacheXiaozhengApplicationTests{
    
    
	@Autowired 
	private DepartmentService departmentService;

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Test
	public void test1(){
    
    
		Department department = departmentService.get(1);
		System.out.println(department.toString());
	}

	@Test
	public void test001(){
    
    
		redisTemplate.opsForValue().set("name", "xiaozheng");
		Object name = redisTemplate.opsForValue().get("name");
		System.out.println(name.toString());
	}

}

在这里插入图片描述
工具查看:
在这里插入图片描述
整 个 过 程 是 不 是 很 简 单 , 个 人 建 议 最 重 要 的 是 掌 握 学 习 方 法 , \color{red}整个过程是不是很简单,个人建议最重要的是掌握学习方法,
例 如 整 合 d o c k e t 需 要 引 入 哪 些 依 赖 , 有 哪 些 配 置 , 全 部 都 可 以 在 A P I 文 档 中 看 出 , 上 面 把 所 有 的 截 图 都 给 大 家 了 \color{red}例如整合docket需要引入哪些依赖,有哪些配置,全部都可以在API文档中看出,上面把所有的截图都给大家了 docketAPI

存在的序列化问题

从上图我们可以看出,存入redis的内容xiaozheng变成了\xAC\xED\x00\x05t\x00\x09xiaozheng

@Test
	public void test002(){
    
    
		Employee employee = employeeService.get(1);
		redisTemplate.opsForValue().set("emp-01", employee);

	}

在这里插入图片描述

在默认情况下呢,它使用的是jdk的序列化机机制

自定义序列化机制

package csdn.xiaozheng.springbootcachexiaozheng.config;


import csdn.xiaozheng.springbootcachexiaozheng.domain.Department;
import csdn.xiaozheng.springbootcachexiaozheng.domain.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class MyRedisConfig {
    
    

    @Bean
    public RedisTemplate<Object, Employee> empRedisTemplate(
            RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
    
    
        RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Employee> ser = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
        template.setDefaultSerializer(ser);
        return template;
    }
    @Bean
    public RedisTemplate<Object, Department> deptRedisTemplate(
            RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
    
    
        RedisTemplate<Object, Department> template = new RedisTemplate<Object, Department>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Department> ser = new Jackson2JsonRedisSerializer<Department>(Department.class);
        template.setDefaultSerializer(ser);
        return template;
    }



}

package csdn.xiaozheng.springbootcachexiaozheng;

import csdn.xiaozheng.springbootcachexiaozheng.domain.Department;
import csdn.xiaozheng.springbootcachexiaozheng.domain.Employee;
import csdn.xiaozheng.springbootcachexiaozheng.service.DepartmentService;
import csdn.xiaozheng.springbootcachexiaozheng.service.EmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@EnableCaching
@RunWith(SpringRunner.class)
public class SpringBootCacheXiaozhengApplicationTests{
    
    
	@Autowired 
	private DepartmentService departmentService;
	@Autowired
	private EmployeeService employeeService;

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private RedisTemplate<Object, Employee> empRedisTemplate;
	
	@Test
	public void test1(){
    
    
		Department department = departmentService.get(1);
		System.out.println(department.toString());
	}

	@Test
	public void test001(){
    
    
		redisTemplate.opsForValue().set("name", "xiaozheng");
		Object name = redisTemplate.opsForValue().get("name");
		System.out.println(name.toString());
	}
	
	@Test
	public void test002(){
    
    
		Employee employee = employeeService.get(1);
		redisTemplate.opsForValue().set("emp-01", employee);

	}

	@Test
	public void test004(){
    
    
		Employee employee = employeeService.get(1);
		empRedisTemplate.opsForValue().set("emp-004", employee);

	}

}

在这里插入图片描述

总结

这里并不展开讲redisTemplate如何使用,重点在于整合
你 知 道 的 越 多 , 不 知 道 的 越 多 , 希 望 对 大 家 有 帮 助 , 谢 谢 ! \color{red}你知道的越多,不知道的越多,希望对大家有帮助,谢谢!

猜你喜欢

转载自blog.csdn.net/xiaozhegaa/article/details/110089669
今日推荐