1. 什么是 Redis
Redis 是一个基于内存的高性能 key-value 数据库。是完全开源免费的,用C语言编写的,遵守BSD协议。
2.Redis 特点:
Redis 是基于内存操作的,吞吐量非常高,可以在 1s内完成十万次读写操作
Redis 的读写模块是单线程,每个操作都具原子性
Redis 支持数据的持久化,可以将内存中的数据保存在磁盘中,重启可以再次加载,但可能会有极短时间内数据丢失
Redis 支持多种数据结构,String,list,set,zset,hash等
3.正文开始
1.导入所需要的依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
2.application.yml配置文件
server:
8080
spring:
redis:
host: 192.168.233.123 #redis服务器地址
port: 6379 #redis端口 默认6379
3.RedisConfig配置类
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author yujie.li
* @data 2023/7/18
* @desc redis配置类
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
// 获取到配置文件中链接redis的信息
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
3.Redis数据类型操作
1.String类型
小编这里使用的是自己本地数据库中查询出来的对象数据,大家自己可以手动实例化一个对象用来测试(这些不重要)
// 操作String类型中 此方法往Redis中写入数据,
// 第一个参数为Redis中设置的key,第二个参数为value
// 第三个参数为过期时间,第四个参数为过期单位
redisTemplate.opsForValue().set(preKey, flower, 3, TimeUnit.DAYS);
// 操作String类型中 此方法从Redis中获取数据
redisTemplate.opsForValue().get(preKey);
2.list类型
list类型类似于一个栈,先从左侧插入的数据会排在数据的末端
//从左侧开始插入数据
redisTemplate.opsForList().leftPush(preKey, flower);
//从右侧开始插入数据
redisTemplate.opsForList().rightPush(preKey, flower);
//从list中获取元素
//第一个参数为key 第二个参数为从第几个下标开始取
//第三个参数为取多少个值 -1代表全部取出
redisTemplate.opsForList().range(preKey, 0, -1)
3.set类型
set类型中不允许出现相同的数据
//添加缓存
//第一个参数为key 第二个参数为 value
//第三个参数为排序的优先级
redisTemplate.opsForSet().add(preKey, item, item.getId())
//取出缓存数据 按优先等级显示
redisTemplate.opsForSet().members(preKey)
4.sortedSet类型
sortedSet类型中不允许出现相同的数据,并是按顺序排列
//添加缓存
//第一个参数为key 第二个参数为 value
//第三个参数为排序的优先级
redisTemplate.opsForZSet().add(preKey, flowers.get(i), flowers.get(i))
//取出缓存数据 按优先等级显示
redisTemplate.opsForZSet().range(preKey,0,-1)
5.hash类型
hash类型中类似于是一个map中嵌套着一个map
//第一个参数为包裹map的key,第二个参数为里面内层map的key,第三个参数为value
redisTemplate.opsForHash().put(preKey,"hash"+preKey,flowers)
//通过第一个key找到map,在通过map的key去找到value
redisTemplate.opsForHash().get(preKey,"hash"+preKey)
给key设置过期时间
redisTemplate.expire(preKey, 3, TimeUnit.DAYS);
4.Redis数据操作测试类完整代码
import com.lyj.pojo.Flower;
import com.lyj.service.FlowerService;
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 java.util.List;
import java.util.concurrent.TimeUnit;
@SpringBootTest
public class test {
@Autowired
private FlowerService flowerService;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* String类型
*/
@Test
void stringTest() {
Flower flower = flowerService.findOneById(1);
String preKey = "SpringBootRedis" + flower.getId();
//判断key是否存在
Boolean hasKey = redisTemplate.hasKey(preKey);
if (hasKey) {
System.out.println("redis存在,未查询数据库");
Flower flowerRedis = (Flower) redisTemplate.opsForValue().get(preKey);
System.out.println(flowerRedis);
} else {
System.out.println("redis不存在,数据库查询");
//进行redis的添加
redisTemplate.opsForValue().set(preKey, flower, 3, TimeUnit.DAYS);
}
}
/**
* list数据类型
*/
@Test
void listTest() {
Flower flower = flowerService.findOneById(2);
String preKey = "SpringBootRedis" + flower.getId();
if (redisTemplate.hasKey(preKey)) {
System.out.println("redis存在,未查询数据库");
System.out.println(redisTemplate.opsForList().range(preKey, 0, -1));
} else {
System.out.println("redis不存在,数据库查询");
redisTemplate.opsForList().leftPush(preKey, flower);
}
}
/**
* set数据类型
*/
@Test
void setTest() {
List<Flower> flowers = flowerService.findAll();
String preKey = "SpringBootRedis" + flowers.get(2).getId();
System.out.println(preKey);
if (redisTemplate.hasKey(preKey)) {
System.out.println("redis存在,未查询数据库");
System.out.println(redisTemplate.opsForSet().members(preKey));
} else {
System.out.println("redis不存在,数据库查询");
flowers.forEach(item -> redisTemplate.opsForSet().add(preKey, item, item.getId()));
//给key设置过期时间
redisTemplate.expire(preKey, 3, TimeUnit.DAYS);
}
}
/**
* sortedSet数据类型
*/
@Test
void sortedSetTest() {
List<Flower> flowers = flowerService.findAll();
String preKey = "SpringBootRedis" + flowers.get(3).getId();
System.out.println(preKey);
if (redisTemplate.hasKey(preKey)) {
System.out.println("redis存在,未查询数据库");
System.out.println(redisTemplate.opsForZSet().range(preKey,0,-1));
} else {
System.out.println("redis不存在,数据库查询");
int index = flowers.size();
int i;
//设置优先级 值越小级别越高
int j = index;
for (i = 0; i < index; i++) {
redisTemplate.opsForZSet().add(preKey, flowers.get(i), j--);
}
redisTemplate.expire(preKey, 3, TimeUnit.DAYS);
}
}
/**
* hash数据类型
*/
@Test
void hashTest() {
List<Flower> flowers = flowerService.findAll();
String preKey = "SpringBootRedis" + flowers.get(4).getId();
System.out.println(preKey);
if (redisTemplate.hasKey(preKey)) {
System.out.println("redis存在,未查询数据库");
System.out.println(redisTemplate.opsForHash().get(preKey,"hash"+preKey));
} else {
System.out.println("redis不存在,数据库查询");
redisTemplate.opsForHash().put(preKey,"hash"+preKey,flowers);
redisTemplate.expire(preKey, 3, TimeUnit.DAYS);
}
}
/**
*
* 删除redis
*/
@Test
void delTest(){
String preKey = "SpringBootRedis" + 1;
if (redisTemplate.hasKey(preKey)) {
System.out.println("redis缓存Key存在,并删除");
System.out.println(redisTemplate.delete(preKey));
}else{
System.out.println("redis缓存Key不存在");
}
}
}
尾注:每一行代码 都是改变世界的能量 愿你每一次的运行 都是发自内心的快乐