--- title: Spring 访问 Redis date: 2023-01-31 20:54:42 order: 21 categories: - Java - 框架 - Spring - Spring数据 tags: - Java - 框架 - Spring - SpringBoot - Redis permalink: /pages/6bb64355/ --- # Spring 访问 Redis ## 简介 [Redis](https://redis.io/) 是一个被数百万开发人员用作数据库、缓存、流引擎和消息代理的开源内存数据库。 在 Spring 中,[spring-data-redis](https://github.com/spring-projects/spring-data-redis) 项目对访问 [Redis](https://redis.io/) 进行了 API 封装,提供了便捷的访问方式。 [spring-data-redis](https://github.com/spring-projects/spring-data-redis) [spring-boot](https://github.com/spring-projects/spring-boot) 项目中的子模块 [spring-boot-starter-data-redis](https://github.com/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot-starters/spring-boot-starter-data-redis) 基于 [spring-data-redis](https://github.com/spring-projects/spring-data-redis) 项目,做了二次封装,大大简化了 Redis 的相关配置。 ## Spring Boot 快速入门 ### 引入依赖 在 pom.xml 中引入依赖: ```xml org.springframework.boot spring-boot-starter-data-redis ``` ### 数据源配置 ```properties spring.redis.database = 0 spring.redis.host = localhost spring.redis.port = 6379 spring.redis.password = ``` ### 定义实体 ```java import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import java.io.Serializable; @Data @ToString @NoArgsConstructor @AllArgsConstructor public class User implements Serializable { private static final long serialVersionUID = 4142994984277644695L; private Long id; private String name; private Integer age; private String address; private String email; } ``` ### 定义 CRUD 接口 ```java import java.util.Map; public interface UserService { void batchSetUsers(Map users); long count(); User getUser(Long id); void setUser(User user); } ``` ### 创建 CRUD 接口实现 ```java import cn.hutool.core.bean.BeanUtil; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.Map; @Service public class UserServiceImpl implements UserService { public static final String DEFAULT_KEY = "spring:tutorial:user"; private final RedisTemplate redisTemplate; public UserServiceImpl(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } @Override public void batchSetUsers(Map users) { redisTemplate.opsForHash().putAll(DEFAULT_KEY, users); } @Override public long count() { return redisTemplate.opsForHash().size(DEFAULT_KEY); } @Override public User getUser(Long id) { Object obj = redisTemplate.opsForHash().get(DEFAULT_KEY, id.toString()); return BeanUtil.toBean(obj, User.class); } @Override public void setUser(User user) { redisTemplate.opsForHash().put(DEFAULT_KEY, user.getId().toString(), user); } } ``` ### 创建 Application 创建 Application,实例化一个 `RedisTemplate` 对象。 ```java import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Slf4j @SpringBootApplication public class RedisQuickstartApplication { @Autowired private ObjectMapper objectMapper; @Bean @Primary public RedisTemplate redisTemplate(RedisConnectionFactory factory) { // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 // objectMapper.activateDefaultTyping(new DefaultBaseTypeLimitingValidator(), // ObjectMapper.DefaultTyping.NON_FINAL); // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Object.class); serializer.setObjectMapper(objectMapper); RedisTemplate template = new RedisTemplate<>(); // 配置连接工厂 template.setConnectionFactory(factory); // 值采用json序列化 template.setValueSerializer(serializer); // 使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 设置hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(serializer); template.afterPropertiesSet(); return template; } public static void main(String[] args) { SpringApplication.run(RedisQuickstartApplication.class, args); } } ``` ### 测试 ```java @Slf4j @SpringBootTest(classes = { RedisQuickstartApplication.class }) public class RedisQuickstartTests { @Autowired private UserService userService; @Test public void test() { final long SIZE = 1000L; Map map = new HashMap<>(); for (long i = 0; i < SIZE; i++) { User user = new User(i, RandomUtil.randomChineseName(), RandomUtil.randomInt(1, 100), RandomUtil.randomEnum(Location.class).name(), RandomUtil.randomEmail()); map.put(String.valueOf(i), user); } userService.batchSetUsers(map); long count = userService.count(); Assertions.assertThat(count).isEqualTo(SIZE); for (int i = 0; i < 100; i++) { long id = RandomUtil.randomLong(0, 1000); User user = userService.getUser(id); log.info("user-{}: {}", id, user.toString()); } } } ``` ## 示例源码 更多 Spring 访问 Redis 示例请参考:[Redis 示例源码](https://github.com/dunwu/spring-tutorial/tree/master/codes/data/nosql/redis) ## 参考资料 - [Redis 官网](https://redis.io/) - [Redis Github](https://github.com/redis/redis) - [spring-data-redis Github](https://github.com/spring-projects/spring-data-redis) - [Spring Data Redis 官方文档](https://docs.spring.io/spring-data/redis/docs/current/reference/html/) - [Spring Data 官方示例](https://github.com/spring-projects/spring-data-examples/)