关注公众号:CoderBuff,回复“redis”获取《Redis5.x入门教程》完整版PDF。html
有关本章节源码:https://github.com/yu-linfeng/redis5.x_tutorial/tree/master/code/spring-data-redisjava
Java客户端(上)章节中咱们使用了redis的Java客户端的第三方开源框架——Jedis,但目前Java应用已经被Spring(Spring Boot)统治了大半江山,就连一些数据链接操做的封装Spring也不放过,这其中也不乏有redis的封装——Spring Data Redis
。关于Spring Data Redis
的官方介绍:https://spring.io/projects/spring-data-redis。git
使用Spring Data Redis
后,你会发现一切变得如此简单,只须要配置文件便可作到开箱即用。程序员
咱们经过IDEA中的Spring Initializer
建立Spring Boot工程,并选择Spring Data Redis
,主要步骤入下图所示:github
第一步,建立工程,选择Spring Initializr
。redis
第二步,选择SpringBoot的依赖NoSQL -> Spring Data Redis
。spring
建立好后,咱们经过ymal
格式的配置文件application.yml
配置相关配置项。api
spring: redis: host: 127.0.0.1 port: 6379
Spring Data Redis
中操做redis的最关键的类是RedisTemplate
,了解过Spring Data
的朋友应该很熟悉~Template
后缀,咱们在配置好application.yml
后直接写一个测试类体验一下,什么是开箱即用:缓存
package com.coderbuff.springdataredis; 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 public class SpringDataRedisApplicationTests { @Autowired private RedisTemplate<String, String> redisTemplate; @Test void testDefaultRestTemplate() { redisTemplate.opsForValue().set("default_redis_template", "1"); } }
什么是开箱即用,这就是开箱即用。咱们没有再写任何的类,只须要两行配置便可使用。springboot
一般状况下,咱们喜欢“封装”,喜欢把redisTemplate.opsForValue().set
这样的操做封装成工具类redisUtil.set
,因此咱们封装一下RedisTemplate
,顺带熟悉它的API。
package com.coderbuff.springdataredis.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** * redis操做工具类 * @author okevin * @date 2020/2/18 14:34 */ @Component public class RedisUtil { @Autowired private RedisTemplate<Object, Object> redisTemplate; /** * 字符串类型写入操做 * @param key key值 * @param value value值 */ public void set(String key, String value) { this.redisTemplate.opsForValue().set(key, value); } /** * 可设置过时时间字符串类型写入操做 * @param key key值 * @param value value值 * @param expire 过时时间 * @param timeUnit 过时时间单位 */ public void set(String key, String value, Long expire, TimeUnit timeUnit) { this.redisTemplate.opsForValue().set(key, value, expire, timeUnit); } /** * 字符串类型读取操做 * @param key key值 * @return value值 */ public String get(String key) { return (String) this.redisTemplate.opsForValue().get(key); } }
编写测试类:
package com.coderbuff.springdataredis.util; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; /** * @author okevin * @date 2020/2/18 23:14 */ @SpringBootTest public class RedisUtilTests { @Autowired private RedisUtil redisUtil; @Test public void testSet() { redisUtil.set("redis_util", "1"); Assertions.assertEquals("1", redisUtil.get("redis_util")); } }
实际上,真正要把Spring Data Redis
用好,还能够作如下工做:
注意本文使用的是SpringBoot2.x与SpringBoot1.x有必定的区别。
package com.coderbuff.springdataredis.config; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; /** * redis配置 * @author okevin * @date 2020/2/18 14:20 */ @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { /** * 使用redis做为spring的缓存管理工具 * 注意:springboot2.x与springboot1.x此处的区别较大 * 在springboot1.x中,要使用redis的缓存管理工具为如下代码: * * public CacheManager cacheManager(RedisTemplate redisTemplate) { * RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); * return redisCacheManager; * } * * @param redisConnectionFactory redis链接工厂 * @return redis缓存管理 */ @Bean public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheManager redisCacheManager = RedisCacheManager.create(redisConnectionFactory); return redisCacheManager; } }
RedisTemplate
,但咱们可能仍是须要定制化注入RedisTemplate
。咱们能够定制RedisTemplate
,例如序列化的方式等,固然这些都不是必须的:
package com.coderbuff.springdataredis.config; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; /** * redis配置 * @author okevin * @date 2020/2/18 14:20 */ @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { //省略上面的cacheManager注入 @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); //配置链接工厂 /*使用Jackson序列化和反序列化key、value值,默认使用JDK的序列化方式 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); //指定要序列化的域,ALL表示全部字段、以及set/get方法,ANY是都有包括修饰符private和public om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); //指定序列化输入的类型,NON_FINAL表示必须是非final修饰的类型 jacksonSeial.setObjectMapper(om); //如下数据类型经过jackson序列化 redisTemplate.setValueSerializer(jacksonSeial); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(jacksonSeial); */ redisTemplate.afterPropertiesSet(); return redisTemplate; } }
想要学习更多Spring Data Redis
,就请打开官网(https://spring.io/projects/spring-data-redis)尽情探索吧。
关注公众号:CoderBuff,回复“redis”获取《Redis5.x入门教程》完整版PDF。