- SpringBoot整合Redis
- 1)自定义RedisTemplate
- 2)RedisUtils工具类
SpringBoot 操作数据:spring-data
SpringData 也是和 SpringBoot 齐名的项目
整合测试
- 创建一个springboot项目
说明:再SpringBoot2.x之后,原来使用的jedis被替换成了lettuce
jedis:采用的是直连,多个线程操作的话,是不安全的。想要避免不安全的,使用 jedis pool 连接池!更像 BIO
lettuce:采用netty,实例可以在多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据,更像 NIO
源码分析:
在jar包中找到
@ConditionalOnSingleCandidate
表示当指定Bean在容器中只有一个,或者虽然有多个但是指定首选Bean,这个组合了@Conditional元注解,只是使用了不同的条件(Conditional)
@Bean
@ConditionalOnMissingBean( //我们可以自己定义一个redisTemplate来替换这个默认的
name = {"redisTemplate"}
)
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public RedisTemplate
- 导入依赖
org.springframework.boot spring-boot-starter-data-redis
- 配置连接
# SpringBoot 所有的配置类,都有一个自动配置类 RedisAutoConfiguration # 自动配置类都会绑定一个properties 配置文件 RedisProperties # redis服务运行的ip地址 spring.redis.host=127.0.0.1 # redis服务运行的端口号 spring.redis.port=6379
- 编写实体类User.class
@AllArgsConstructor
@NoArgsConstructor
@Data
@Accessors(chain = true)
@Component
//在企业中,所有的 pojo 都需要序列化
public class User implements Serializable{
private String name;
private int age;
}
- 测试
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
RedisTemplate redisTemplate;
@Test
void contextLoads() {
//除了基本的操作,我们常用的方法都可以直接通过 redisTemplate 来操作,比如事务和基本的CURD
redisTemplate.opsForValue().set("mykey","我就是太阳");
System.out.println(redisTemplate.opsForValue().get("mykey"));
}
}
1)自定义RedisTemplate
当我们使用Java代码实现Redis时,可能会出现以下情况
测试代码
@Test
public void test() throws JsonProcessingException {
// 真实的开发一般都是用JSON来传递对象
User user = new User("张三", 12);
String jsonUser = new ObjectMapper().writeValueAsString(user);
redisTemplate.opsForValue().set("user",user);
User user1 = (User) redisTemplate.opsForValue().get("user");
System.out.println(user1);
}
从redis-cli.exe客户端获取
但是在控制台字符是正常的!
源码分析:
关于对象的保存,大多数都是通过Json格式来保存的
RedisConfiguration.class配置类
package com.vinjcent.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfiguration {
//编写自定义的 redisTemplate
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//为了开发方便,一般直接使用
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
//配置具体的序列化方式
// JSON解析任意对象
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//String的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用String的序列化
template.setKeySerializer(stringRedisSerializer);
//hash的key也采用String的序列化
template.setHashKeySerializer(stringRedisSerializer);
//value的序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
//hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
//设置所有配置
template.afterPropertiesSet();
return template;
}
}
Jackson ObjectMapper 中的 enableDefaultTyping 方法由于安全原因,从 2.10.0 开始标记为过期,建议用 activateDefaultTyping 方法代替,所以如果继续使用 enableDefaultTyping 会有警告出现,我们现在要消除这个警告
再次测试,并替换原来使用的RedisTemplate
package com.vinjcent.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@SuppressWarnings("all")
@Component
public final class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
//=============================common===================================
public boolean expire(String key, long time){
try {
if(time > 0){
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long getExpire(String key){ return redisTemplate.getExpire(key,TimeUnit.SECONDS);}
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void del(String... key) {
if(key != null && key.length > 0){
if (key.length == 1){
redisTemplate.delete(key[0]);
}else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
//=============================String===================================
public Object get(String key){ return key == null ? null : redisTemplate.opsForValue().get(key);}
public boolean set(String key, Object value){
try {
redisTemplate.opsForValue().set(key,value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean set(String key, Object value, long time){
try {
if(time > 0){
redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);
}else {
set(key,value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long incr(String key, long delta){
if(delta < 0){
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
public long decr(String key, long delta){
if(delta < 0){
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
//=============================Map===================================
public Object hget(String key, String item){ return redisTemplate.opsForHash().get(key,item);}
public Map hmget(String key) { return redisTemplate.opsForHash().entries(key);}
public boolean hmset(String key, Map map) {
try {
redisTemplate.opsForHash().putAll(key,map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean hmset(String key, Map map, long time){
try {
redisTemplate.opsForHash().putAll(key,map);
if (time > 0){
expire(key,time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean hset(String key, String item, Object value){
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean hset(String key, String item, Object value, long time){
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0){
expire(key,time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void hdel(String key, Object... item){
redisTemplate.opsForHash().delete(key,item);
}
public boolean hHasKey(String key, String item){
return redisTemplate.opsForHash().hasKey(key, item);
}
public double hincr(String key, String item, double by){
return redisTemplate.opsForHash().increment(key, item, by);
}
public double hdecr(String key, String item, double by){
return redisTemplate.opsForHash().increment(key, item, -by);
}
//=============================Set===================================
public Set sGet(String key){
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean sHasKey(String key, Object value){
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long sSet(String key, Object... values){
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public long sSetAndTime(String key, long time, Object... values){
try {
Long count = redisTemplate.opsForSet().add(key, values);
if(time > 0){
expire(key, time);
}
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public long sGetSetSize(String key){
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public long setRemove(String key, Object... values){
try {
return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
//=============================List===================================
public long lGetListSize(String key){
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public Object lgetIndex(String key, long index){
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean lSet(String key, Object value){
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean lSet(String key, Object value, long time){
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0){
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean lSet(String key, List values){
try {
redisTemplate.opsForList().rightPushAll(key, values);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean lSet(String key, List values, long time){
try {
redisTemplate.opsForList().rightPushAll(key, values);
if (time > 0){
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean lUpdateIndex(String key, Object value, long index){
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long lRemove(String key, Object value, long number){
try {
Long count = redisTemplate.opsForList().remove(key, number, value);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}



