目录
2、Spring-data-redis 针对 Jedis 提供的功能
二、SpringBoot整合Redis加入Redis依赖及序列化依赖
三、增加Redis配置类,为key、value配置序列化方式
四、配置Redis工具类,将RedisTemplate的API统一管理
五、RedisTemplate针对Redis五种数据类型的操作
一、Spring Data Redis
1、Spring-data-redis简介
Spring-data-redis提供了在Spring应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,封装了 RedisTemplate 对象来进行对Redis的各种操作、异常处理及序列化,支持发布订阅,并对Spring 3.1 cache进行了实现,它支持所有的Redis原生的 API。
2、Spring-data-redis 针对 Jedis 提供的功能
1、连接池自动管理,提供了一个高度封装的“RedisTemplate”类;
2、针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
- ValueOperations:简单的K-V操作
- SetOperations:set类型数据操作
- ZSetOperations:zset类型数据操作
- HashOperations:针对map类型的数据操作
- ListOperations:针对list类型的数据操作
3、提供了对key的“bound”(绑定)便捷化操作API,可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key,即BoundKeyOperations:
- BoundValueOperations
- BoundSetOperations
- BoundListOperations
- BoundSetOperations
- BoundHashOperations
4、将事务操作封装,有容器控制。
5、针对数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)
- JdkSerializationRedisSerializer:POJO对象的存取场景,使用JDK本身序列化机制,将pojo类通过ObjectInputStream/ObjectOutputStream进行序列化操作,最终redis-server中将存储字节序列。是目前最常用的序列化策略。
- StringRedisSerializer:Key或者value为字符串的场景,根据指定的charset对数据的字节序列编码成string,是“new String(bytes, charset)”和“string.getBytes(charset)”的直接封装。是最轻量级和高效的策略。
- JacksonJsonRedisSerializer:jackson-json工具提供了javabean与json之间的转换能力,可以将pojo实例序列化成json格式存储在redis中,也可以将json格式的数据转换成pojo实例。
在项目中,我们经常会用到RedisTemplate来操作Redis,本文详细罗列了RedisTemplate操作Redis的方法。
二、SpringBoot整合Redis加入Redis依赖及序列化依赖
<!-- redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
三、增加Redis配置类,为key、value配置序列化方式
package com.test.redis.redis;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @ClassName RedisConfig
* @Description Redis配置类
* @Author chengjunyu
* @Date 2022/2/11
* @Version 1.0
*/
@Configuration
public class RedisConfig {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
// fastjson序列化
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
// key的序列化采用StringRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// value的序列化采用FastJsonRedisSerializer
redisTemplate.setValueSerializer(fastJsonRedisSerializer);
redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(connectionFactory);
return stringRedisTemplate;
}
}
四、配置Redis工具类,将RedisTemplate的API统一管理
/**
* @ClassName RedisUtil
* @Description Redis工具类
* @Author chengjunyu
* @Date 2022/2/11
* @Version 1.0
*/
public class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
}
五、RedisTemplate针对Redis五种数据类型的操作
1、RedisTemplate操作string类型数据
/**
* @description string设置 key和 value的值
* @author chengjunyu
* @date 2022/2/11
* @param key
* @param value
* @return void
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* @description string设置 key和 value的值并设置过期时间和时间单位
* @author chengjunyu
* @date 2022/2/12
* @param key
* @param value
* @param seconds
* @param timeUnit
* @return void
*/
public void setWithExpire(String key, Object value, Long seconds, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, seconds, timeUnit);
}
/**
* @description string获取 key对应的 value值
* @author chengjunyu
* @date 2022/2/11
* @param * @param key
* @return java.lang.Object
*/
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* @description 判断在 redis中是不是存在对应的 key值,有的话就返回 true,没有就返回 false
* @author chengjunyu
* @date 2022/2/11
* @param * @param key
* @return boolean
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* @description 删除redis中对应的key值
* @author chengjunyu
* @date 2022/2/11
* @param key
* @return boolean
*/
public Boolean del(String key) {
return redisTemplate.delete(key);
}
/**
* @description 批量删除 redis中对应的 key值,其中 keys是数组 keys:Collection<K> keys
* @author chengjunyu
* @date 2022/2/11
* @param keys
* @return long
*/
public Long batchDel(Collection<String> keys) {
return redisTemplate.delete(keys);
}
/**
* @description 把 key值序列化成 byte[]类型
* @author chengjunyu
* @date 2022/2/11
* @param key
* @return byte[]
*/
public byte[] dump(String key) {
return redisTemplate.dump(key);
}
/**
* @description 对传入的 key值设置过期时间
* @author chengjunyu
* @date 2022/2/11
* @param * @param key
* @param seconds
* @return void
*/
public Boolean expire(String key, long seconds) {
return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
}
/**
* @description 对传入的 key值设置过期日期
* @author chengjunyu
* @date 2022/2/11
* @param * @param key
* @param date
* @return boolean
*/
public Boolean expireAt(String key, Date date) {
return redisTemplate.expireAt(key, date);
}
/**
* @description 模糊查询,返回一个没有重复的 Set类型
* @author chengjunyu
* @date 2022/2/11
* @param * @param key
* @return java.util.Set<java.lang.String>
*/
public Set<String> getStringKeys(String key) {
return redisTemplate.keys(key);
}