背景
软件开发是一门实践性科学,对大多数人来说,学习一种新技术不是一开始就去深究其原理,而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经历中,每次学习新技术总是要花费或多或少的时间、检索不止一篇资料才能得出一个可工作的DEMO,这占用了我大量的时间精力。因此本文旨在通过一篇文章即能还原出可工作的、甚至可用于生产的DEMO,期望初学者能尽快地迈过0到1的这一步骤,并在此基础上不断深化对相关知识的理解。
为达以上目的,本文会将开发环境、工程目录结构、开发步骤及源码尽量全面地展现出来,文字描述能简则简,能用代码注释的绝不在正文中再啰嗦一遍,正文仅对必要且关键的信息做重点描述。
介绍
本文介绍开发Spring Boot应用时借助Spring Data Redis实现对Redis五种基本数据(字符串string、哈希hash、列表list、集合set、有序集合zset)类型的操作。
开发环境
分类 | 名称 | 版本 |
---|---|---|
操作系统 | Windows | Windows 11 |
JDK | Oracle JDK | 21.0.1 |
IDE | IntelliJ IDEA | 2023.2.4 |
构建工具 | Apache Maven | 3.9.3 |
缓存 | Redis | 7.2 |
开发步骤及源码
1> 创建Maven工程,添加依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jiyongliang</groupId>
<artifactId>springboot3-redis</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>springboot3-redis-data</artifactId>
<properties>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>3.2.2</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2> 添加应用配置(src/main/resources/application.yml
)。
spring:
data:
redis:
# 连接地址
host: 127.0.0.1
# 端口
port: 6379
# Redis数据库索引,默认为 0
database: 0
# 用户名(可选)
# username:
# 密码(可选)
# password:
3> 定义SpringBoot应用启动类。
package com.jiyongliang.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBoot3RedisDataApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot3RedisDataApplication.class, args);
}
}
4> 创建Redis配置类。
package com.jiyongliang.springboot.config;
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;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
5> 字符串(string)
package com.jiyongliang.springboot;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
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
class RedisStringTests {
@Autowired
RedisTemplate<String, Object> redisTemplate;
String key = "string-key";
@AfterEach
void afterEach() {
redisTemplate.delete(key);
}
@Test
void testString() {
// 添加字符串缓存数据
redisTemplate.opsForValue().set(key, "string data");
// 获取字符串缓存数据
String cachedString = (String) redisTemplate.opsForValue().get(key);
Assertions.assertThat(cachedString)
.isNotNull()
.isEqualTo("string data");
}
}
6> 哈希(hash)
package com.jiyongliang.springboot;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
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.HashMap;
import java.util.Map;
@SpringBootTest
class RedisHashTests {
@Autowired
RedisTemplate<String, Object> redisTemplate;
String key = "hash-single-key";
@BeforeEach
void beforeEach() {
redisTemplate.delete(key);
}
@Test
void testHash() {
// 添加单个Hash缓存数据
redisTemplate.opsForHash().put(key, "hash key", "hash value");
// 获取单个Hash缓存数据
String cachedHash = (String) redisTemplate.opsForHash().get(key, "hash key");
Assertions.assertThat(cachedHash).isEqualTo("hash value");
redisTemplate.delete(key);
// 添加map缓存数据
Map<String, String> map = new HashMap<>();
map.put("map-key-1", "map value 1");
map.put("map-key-2", "map value 2");
map.put("map-key-3", "map value 3");
redisTemplate.opsForHash().putAll(key, map);
// 获取map缓存数据
Map<Object, Object> cachedHashMap = redisTemplate.opsForHash().entries(key);
Assertions.assertThat(cachedHashMap)
.isNotNull()
.containsEntry("map-key-1", "map value 1")
.containsEntry("map-key-2", "map value 2")
.containsEntry("map-key-3", "map value 3");
}
}
7> 列表(list)
package com.jiyongliang.springboot;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
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;
@SpringBootTest
class RedisListTests {
@Autowired
RedisTemplate<String, Object> redisTemplate;
String key = "list-key";
@BeforeEach
void beforeEach() {
redisTemplate.delete(key);
}
@Test
void testList() {
// 添加List缓存数据
redisTemplate.opsForList().leftPush(key, "list value 3");
redisTemplate.opsForList().leftPush(key, "list value 2");
redisTemplate.opsForList().leftPush(key, "list value 1");
redisTemplate.opsForList().rightPush(key, "list value 4");
redisTemplate.opsForList().rightPush(key, "list value 5");
// 获取全部List缓存数据
List<Object> cachedList = redisTemplate.opsForList().range(key, 0, -1);
Assertions.assertThat(cachedList)
.isNotNull()
.isNotEmpty()
.hasToString("[list value 1, list value 2, list value 3, list value 4, list value 5]");
// 获取指定下标数据
String listValue = (String) redisTemplate.opsForList().index(key, 2);
Assertions.assertThat(listValue).isEqualTo("list value 3");
listValue = (String) redisTemplate.opsForList().index(key, 5);
Assertions.assertThat(listValue).isNull();
// 从左边开始取数据
listValue = (String) redisTemplate.opsForList().leftPop(key);
Assertions.assertThat(listValue).isEqualTo("list value 1");
// 从右边开始取数据
listValue = (String) redisTemplate.opsForList().rightPop(key);
Assertions.assertThat(listValue).isEqualTo("list value 5");
// 获取list长度
Long length = redisTemplate.opsForList().size(key);
Assertions.assertThat(length).isNotNull().isEqualTo(3);
Assertions.assertThat(redisTemplate.opsForList().range(key, 0, -1))
.isNotNull()
.isNotEmpty()
.hasToString("[list value 2, list value 3, list value 4]");
}
}
8> 集合(set)
package com.jiyongliang.springboot;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
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.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
@SpringBootTest
class RedisSetTests {
@Autowired
RedisTemplate<String, Object> redisTemplate;
String key1 = "set-key-1";
String key2 = "set-key-2";
@BeforeEach
void beforeEach() {
redisTemplate.delete(key1);
redisTemplate.delete(key2);
}
@Test
void testSet() {
// 添加Set缓存数据
String[] setData = new String[]{"A", "B", "C", "D", "D", "C", "D", "E", "F", "F", "G"};
Long addCount = redisTemplate.opsForSet().add(key1, setData);
Set<String> expected = Arrays.stream(setData).collect(Collectors.toSet());
Assertions.assertThat(addCount).isNotNull().isEqualTo(expected.size());
// 获取Set缓存数据
Set<Object> cachedSet = redisTemplate.opsForSet().members(key1);
Assertions.assertThat(cachedSet)
.isNotNull()
.hasSameSizeAs(expected)
.containsAll(expected);
// 判断是否在Set缓存数据中
Assertions.assertThat(redisTemplate.opsForSet().isMember(key1, "F")).isTrue();
Assertions.assertThat(redisTemplate.opsForSet().isMember(key1, "X")).isFalse();
// 返回缓存Set的并集
String[] setTempData = new String[]{"A", "X", "E", "Y", "G", "Z"};
redisTemplate.opsForSet().add(key2, setTempData);
Set<Object> unionSet = redisTemplate.opsForSet().union(key1, key2);
Assertions.assertThat(unionSet)
.isNotNull()
.hasSize(expected.size() + 3)
.containsAll(expected)
.containsAll(Set.of("X", "Y", "Z"));
// 返回缓存Set的交集
Set<Object> intersectSet = redisTemplate.opsForSet().intersect(key1, key2);
Assertions.assertThat(intersectSet)
.isNotNull()
.hasSize(3)
.containsAll(Set.of("A", "E", "G"));
// 返回缓存的set-key-1中存在但set-key-2中不存在的数据
Set<Object> differenceSet = redisTemplate.opsForSet().difference(key1, key2);
Assertions.assertThat(differenceSet)
.isNotNull()
.hasSize(expected.size() - 3)
.containsAll(Set.of("B", "C", "D", "F"));
}
}
9> 有序集合(zset)
package com.jiyongliang.springboot;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
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.Set;
@SpringBootTest
class RedisZSetTests {
@Autowired
RedisTemplate<String, Object> redisTemplate;
String key = "zset-key";
@BeforeEach
void beforeEach() {
redisTemplate.delete(key);
}
@Test
void testZSet() {
// 添加ZSet缓存数据
redisTemplate.opsForZSet().add(key, "X", 1);
redisTemplate.opsForZSet().add(key, "Y", 2);
redisTemplate.opsForZSet().add(key, "Z", 3);
// 值相同的情况下,权重会被覆盖
redisTemplate.opsForZSet().add(key, "X", 1);
redisTemplate.opsForZSet().add(key, "Y", 3);
redisTemplate.opsForZSet().add(key, "Z", 5);
// 获取ZSet缓存数据
Set<Object> cachedZSet = redisTemplate.opsForZSet().range(key, 0, -1);
Assertions.assertThat(cachedZSet)
.isNotNull()
.hasSize(3)
.containsAll(Set.of("X", "Y", "Z"));
// 获取值对应的权重
Double score = redisTemplate.opsForZSet().score(key, "Y");
Assertions.assertThat(score).isNotNull().isEqualTo(3);
// 获取值对应的排名(从0开始)
Long rank = redisTemplate.opsForZSet().rank(key, "Y");
Assertions.assertThat(rank).isNotNull().isEqualTo(1);
// 根据score范围获取值
Set<Object> rangedZSet = redisTemplate.opsForZSet().rangeByScore(key, 1, 4);
Assertions.assertThat(rangedZSet)
.isNotNull()
.hasSize(2)
.containsAll(Set.of("X", "Y"));
}
}
10> 数据过期
package com.jiyongliang.springboot;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
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.concurrent.TimeUnit;
@SpringBootTest
class RedisExpireTests {
@Autowired
RedisTemplate<String, Object> redisTemplate;
String key = "expire-key";
@BeforeEach
void beforeEach() {
redisTemplate.delete(key);
}
@Test
void testExpire() throws InterruptedException {
redisTemplate.opsForValue().set(key, "expire data");
Assertions.assertThat(redisTemplate.opsForValue().get(key))
.isNotNull()
.isEqualTo("expire data");
redisTemplate.opsForValue().getOperations().expire(key, 3, TimeUnit.SECONDS);
TimeUnit.SECONDS.sleep(3);
Assertions.assertThat(redisTemplate.opsForValue().get(key)).isNull();
}
}
11> 单元测试结果
文章来源:https://www.toymoban.com/news/detail-807493.html
工程目录结构
文章来源地址https://www.toymoban.com/news/detail-807493.html
到了这里,关于【Spring Boot 3】【Redis】基本数据类型操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!