依赖jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置信息
###################
##### redis集群配置配置####
###################
spring.redis.host=xxx.xxx.xxx.xxx
spring.redis.database=0
spring.redis.port=30001
spring.redis.timeout=5000
spring.redis.password=ypk222
spring.redis.lettuce.pool.min-idle=8
spring.redis.lettuce.pool.max-idle=500
spring.redis.lettuce.pool.max-active=2000
spring.redis.lettuce.pool.max-wait=10000
## 集群配置
spring.redis.cluster.max-redirects=3
spring.redis.cluster.nodes=${spring.redis.host}:7000,${spring.redis.host}:7001,${spring.redis.host}:7002
配置类
package cn.qgos.core.common.config;
import cn.qgos.core.common.utils.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
/**
* @author :yepk
* @version :1.0
* @apiNote :redis配置类
* @date :2020-05-26-16:25
*/
@Component
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
此配置类是将redis的各个分类单独注入spring,并将value类型通过fastjson序列化,因此value类型的数据反序列化也要用fastjson。
工具类
package cn.qgos.core.common.utils;
import cn.hutool.core.util.StrUtil;
import cn.qgos.core.base.constant.BaseCodeConstant;
import cn.qgos.core.base.pojo.system.QgosResult;
import cn.qgos.core.common.consts.RedisKeys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author :yepk
* @version :1.0
* @apiNote :redis工具类
* @date :2020-05-26-11:16
*/
@Component
@Slf4j
public class RedisUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Resource(name = "valueOperations")
private ValueOperations<String, Object> valueOperations;
@Resource(name = "hashOperations")
private HashOperations<String, String, Object> hashOperations;
@Resource(name = "listOperations")
private ListOperations<String, Object> listOperations;
@Resource(name = "setOperations")
private SetOperations<String, Object> setOperations;
@Resource(name = "zSetOperations")
private ZSetOperations<String, Object> zSetOperations;
/**
* 默认过期时长,单位:秒
*/
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
/**
* 不设置过期时长
*/
public final static long NOT_EXPIRE = -1;
/***
* @apiNote 设置过期时间
* @author yepk
* @date 2020/5/26 13:50
* @param key key
* @param time 时间
* @return {@link Boolean }
*/
public Boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
log.error("设置过期时间报错:{}", e);
return false;
}
}
/***
* @apiNote 根据key获取过期时间
* @author yepk
* @date 2020/5/26 13:49
* @param key key
* @return {@link Long }
*/
public Long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/***
* @apiNote 判断是否存在
* @author yepk
* @date 2020/5/26 13:49
* @param key key
* @return {@link Boolean }
*/
public Boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
log.error("判断键值报错:{}", e);
return false;
}
}
/***
* @apiNote 删除数据
* @author yepk
* @date 2020/5/26 13:49
* @param key key数组
*/
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(Arrays.asList(key));
}
}
}
/***
* @apiNote 获取值
* @author yepk
* @date 2020/5/26 13:49
* @param key key
* @return {@link Object }
*/
public Object get(String key) {
try {
Object val = valueOperations.get(key);
if (val == null) {
return "";
}
return val;
} catch (Exception e) {
log.error("获取数据错误:{}", e);
return null;
}
}
/***
* @apiNote 设置值
* @author yepk
* @date 2020/5/26 13:48
* @param key key
* @param value 值
* @return {@link Boolean }
*/
public Boolean set(String key, Object value) {
try {
valueOperations.set(key, value);
return true;
} catch (Exception e) {
log.error("设置值报错:{}", e);
return false;
}
}
/***
* @apiNote 设置值
* @author yepk
* @date 2020/5/26 13:48
* @param key key
* @param value 值
* @param time 时间 秒
* @return {@link boolean }
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
valueOperations.set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
log.error("设置值报错:{}", e);
e.printStackTrace();
return false;
}
}
/***
* @apiNote 递增
* @author yepk
* @date 2020/5/26 13:48
* @param key key
* @param delta 递增值
* @return {@link Long }
*/
public Long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return valueOperations.increment(key, delta);
}
/***
* @apiNote 递减
* @author yepk
* @date 2020/5/26 13:47
* @param key key
* @param delta 递减值
* @return {@link Long }
*/
public Long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return valueOperations.increment(key, -delta);
}
/**
* @Description 发送广播信息
* @Author llz
* @Date 2020/7/24 上午10:28
* @param topic 广播topic
* @param message 信息
* @return boolean
**/
public boolean convertAndSend(String topic, Object message) {
try {
redisTemplate.convertAndSend(topic, message);
return true;
} catch (Exception e) {
log.error("发送广播信息失败" + topic + "{}", e);
}
return false;
}
public List<Object> lrange(String key, Long start, Long end) {
return listOperations.range(key, start, end);
}
}
按照自己的需去书写工具类
应用
@Resource
private RedisUtil redisUtil;
依赖下来直接用调用