springboot中的cache(三)
- 2020 年 1 月 14 日
- 笔记
https://blog.csdn.net/weixin_44510615/article/details/103656959 CSDN
今天 redis+cache
Redis 是一个高性能的 key-value 数据库。它支持存储的 value 类型很多,包括 String(字符串)、List(列表)、Set(集合)、Sorted-Set(有序集合) 和 Hash(哈希类型)。
数据类型 |
概述 |
---|---|
String(字符串类型) |
字符串是最基本的 Redis 值。Redis 字符串是二进制安全的,这意味着 Redis 字符串可以包含任何类型的数据,例如 JPEG 图像或 JSON 字符串。 |
List(列表类型) |
在 List 的头端或者尾端做百万次的插入和删除操作,也能保持稳定的很少的时间消耗。在 List 的两端访问元素是非常快的,但是如果要访问一个很大的 List 中的中间部分的元素就会比较慢了,时间复杂度是 O(N)。 |
Set(集合类型) |
Set 的一个重要特性是不允许重复元素。向集合中添加多次相同的元素,集合中只存在一个该元素。在实际应用中,这意味着在添加一个元素前不需要先检查元素是否存在。 |
Hash(哈希类型) |
Hash 保存 String 域和 String 值之间的映射,所以它们是用来表示对象的数据类型。其存储方式占用很小的空间,所以在一个小的 Redis 实例中就可以存储上百万的这种对象。 |
Sorted-Set(有序集合类型) |
Redis 有序集合类型与 Redis 的集合类型类似,是非重复的 String 元素的集合。不同之处在于,有序集合中的每个成员都关联一个 Score,Score 是在排序时候使用的,按照 Score 的值从小到大进行排序。集合中每个元素是唯一的,但 Score 有可能重复。 |
Pom.xml 中引用相关的依赖
<!-- 引入 redis 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
application.properties 中增加 Redis 配置
#Redis配置 # Redis数据库索引(默认为0) 我们这边修改为1 spring.redis.database=1 # Redis服务器地址 spring.redis.host=ip # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大连接数(使用负数表示没有限制) spring.redis.pool.max-active=1000 # 连接池最大阻塞等待时间(使用负数表示没有限制) spring.redis.pool.max-wait=-1 # 连接池的最大空闲连接 spring.redis.pool.max-idle=10 # 连接池的最小空闲连接 spring.redis.pool.min-idle=2 # 连接超时时间(毫秒) spring.redis.timeout=0
补充:在 Java 中操作 redis 是 StringRedisTemplate 与 RedisTemplate
StringRedisTemplate 与 RedisTemplate 区别点
两者的关系是 StringRedisTemplate
继承 RedisTemplate
。
两者的数据是不共通的;也就是说 StringRedisTemplate
只能管理 StringRedisTemplate
里面的数据, RedisTemplate
只能管理 RedisTemplate
中的数据。
其实他们两者之间的区别主要在于他们使用的序列化类:
RedisTemplate
使用的是 JdkSerializationRedisSerialize
r 存入数据会将数据先序列化成字节数组然后在存入 Redis 数据库。
StringRedisTemplate
使用的是 StringRedisSerializer
StringRedisTemplate
对应的方法
- stringRedisTemplate.opsForValue()[String(字符串)]
- stringRedisTemplate.opsForList()[List(列表)]
- stringRedisTemplate.opsForSet()[Set(集合)]
- stringRedisTemplate.opsForHash()[Hash(散列)]
- stringRedisTemplate.opsForZSet()[ZSet(有序集合)]
测试类(定义 stringRedisTemplate 和 redisTemplate

在测试定义的测试方法
@Test public void test01() { //给redis中保存数据 stringRedisTemplate.opsForValue().append("msg","hello"); String msg = stringRedisTemplate.opsForValue().get("msg"); System.out.println(msg); stringRedisTemplate.opsForList().leftPush("mylist","1"); stringRedisTemplate.opsForList().leftPush("mylist","2"); } //测试保存对象 @Test public void test02() { Employee empById = employeeMapper.getEmpById(1); //默认如果保存对象,使用jdk序列化机制,序列化后的数据保存到redis中 redisTemplate.opsForValue().set("emp-001",empById); //1、将数据以json的方式保存 //(1)自己将对象转为json //(2)redisTemplate默认的序列化规则;改变默认的序列化规则; empRedisTemplate.opsForValue().set("emp-01", empById); // emp-01使用的empRedisTemplate的json序列化,emp-001使用的jdk序列化机制 }
在 Redis Desktop 中可以看见 msg 已经读取到 redis 中的,

mylist 数组也成功读入

jdk 序列化
进行持久化操作和返回数据时都会使用到 javabean 来统一封装参数,方便操作,一般我们也都会实现 Serializable 接口进行 jdk 序列化(默认是看不懂的)


Json 序列化

@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; } }


对应的数据库
