package org.sxkj.common.redis;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.ValueOperations;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
import java.util.Set;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* @author sean
|
* @version 1.0
|
* @date 2022/4/19
|
*/
|
@Slf4j
|
public class RedisOpsUtils {
|
|
private static RedisTemplate<String, Object> redisTemplate;
|
|
public static void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
|
RedisOpsUtils.redisTemplate = redisTemplate;
|
}
|
|
/**
|
* HSET
|
*
|
* @param key
|
* @param field
|
* @param value
|
*/
|
public static void hashSet(String key, String field, Object value) {
|
redisTemplate.opsForHash().put(key, field, value);
|
}
|
|
/**
|
* HGET
|
*
|
* @param key
|
* @param field
|
* @return
|
*/
|
public static Object hashGet(String key, String field) {
|
return redisTemplate.opsForHash().get(key, field);
|
}
|
|
/**
|
* HKEYS
|
*
|
* @param key
|
* @return
|
*/
|
public static Set<Object> hashKeys(String key) {
|
return redisTemplate.opsForHash().keys(key);
|
}
|
|
/**
|
* HEXISTS
|
*
|
* @param key
|
* @param field
|
* @return
|
*/
|
public static boolean hashCheck(String key, String field) {
|
return redisTemplate.opsForHash().hasKey(key, field);
|
}
|
|
/**
|
* HDEL
|
*
|
* @param key
|
* @param fields
|
* @return
|
*/
|
public static boolean hashDel(String key, Object[] fields) {
|
return redisTemplate.opsForHash().delete(key, fields) > 0;
|
}
|
|
/**
|
* HLEN
|
*
|
* @param key
|
* @return
|
*/
|
public static long hashLen(String key) {
|
return redisTemplate.opsForHash().size(key);
|
}
|
|
/**
|
* EXPIRE
|
*
|
* @param key
|
* @param timeout
|
* @return
|
*/
|
public static boolean expireKey(String key, long timeout) {
|
return redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
}
|
|
/**
|
* SET
|
*
|
* @param key
|
* @param value
|
*/
|
public static void set(String key, Object value) {
|
redisTemplate.opsForValue().set(key, value);
|
}
|
|
/**
|
* GET
|
*
|
* @param key
|
* @return
|
*/
|
public static Object get(String key) {
|
return redisTemplate.opsForValue().get(key);
|
}
|
|
/**
|
* SETEX
|
*
|
* @param key
|
* @param value
|
* @param expire
|
*/
|
public static void setWithExpire(String key, Object value, long expire) {
|
redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
|
}
|
|
|
public static Long increment(String key, Long num) {
|
ValueOperations<String, Object> valueOps = redisTemplate.opsForValue();
|
Long result = valueOps.increment(key, num);
|
return result;
|
}
|
|
|
/**
|
* TTL
|
*
|
* @param key
|
* @return
|
*/
|
public static long getExpire(String key) {
|
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
}
|
|
/**
|
* EXISTS
|
*
|
* @param key
|
* @return
|
*/
|
public static boolean checkExist(String key) {
|
return redisTemplate.hasKey(key);
|
}
|
|
/**
|
* DEL
|
*
|
* @param key
|
* @return
|
*/
|
public static boolean del(String key) {
|
return RedisOpsUtils.checkExist(key) && redisTemplate.delete(key);
|
}
|
|
/**
|
* KEYS
|
*
|
* @param pattern
|
* @return
|
*/
|
public static Set<String> getAllKeys(String pattern) {
|
return redisTemplate.keys(pattern);
|
}
|
|
/**
|
* RPUSH
|
*
|
* @param key
|
* @param value
|
*/
|
public static void listRPush(String key, Object... value) {
|
if (value.length == 0) {
|
return;
|
}
|
for (Object val : value) {
|
redisTemplate.opsForList().rightPush(key, val);
|
}
|
}
|
|
/**
|
* LRANGE
|
*
|
* @param key
|
* @param start
|
* @param end
|
* @return
|
*/
|
public static List<Object> listGet(String key, long start, long end) {
|
return redisTemplate.opsForList().range(key, start, end);
|
}
|
|
/**
|
* LRANGE
|
*
|
* @param key
|
* @return
|
*/
|
public static List<Object> listGetAll(String key) {
|
return redisTemplate.opsForList().range(key, 0, -1);
|
}
|
|
/**
|
* LLen
|
*
|
* @param key
|
* @return
|
*/
|
public static Long listLen(String key) {
|
return redisTemplate.opsForList().size(key);
|
}
|
|
/**
|
* ZADD
|
*
|
* @param key
|
* @param value
|
* @param score
|
*/
|
public static Boolean zAdd(String key, Object value, double score) {
|
return redisTemplate.opsForZSet().add(key, value, score);
|
}
|
|
/**
|
* ZREM
|
*
|
* @param key
|
* @param value
|
*/
|
public static Boolean zRemove(String key, Object... value) {
|
return redisTemplate.opsForZSet().remove(key, value) > 0;
|
}
|
|
/**
|
* ZRANGE
|
*
|
* @param key
|
* @param start
|
* @param end
|
* @return
|
*/
|
public static Set<Object> zRange(String key, long start, long end) {
|
return redisTemplate.opsForZSet().range(key, start, end);
|
}
|
|
/**
|
* ZRANGE
|
*
|
* @param key
|
* @return
|
*/
|
public static Object zGetMin(String key) {
|
Set<Object> objects = zRange(key, 0, 0);
|
if (CollectionUtils.isEmpty(objects)) {
|
return null;
|
}
|
return objects.iterator().next();
|
}
|
|
/**
|
* ZSCORE
|
*
|
* @param key
|
* @param value
|
* @return
|
*/
|
public static Double zScore(String key, Object value) {
|
return redisTemplate.opsForZSet().score(key, value);
|
}
|
|
/**
|
* ZINCRBY
|
*
|
* @param key
|
* @param value
|
* @param delta
|
*/
|
public static Double zIncrement(String key, Object value, double delta) {
|
return redisTemplate.opsForZSet().incrementScore(key, value, delta);
|
}
|
|
/**
|
* 获取锁
|
**/
|
public static boolean getLock(String key, String value) {
|
try {
|
if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
|
return false;
|
}
|
return redisTemplate.opsForValue().setIfAbsent(key, value);
|
} catch (Exception e) {
|
log.error("Failed to acquire lock for key: {}", key, e);
|
return false;
|
}
|
}
|
|
/**
|
* 获取锁
|
*
|
* @param key
|
* @param value
|
* @param expireTime
|
* @param timeUnit
|
* @return
|
*/
|
public static boolean getLock(String key, String value, long expireTime, TimeUnit timeUnit) {
|
try {
|
if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
|
return false;
|
}
|
return redisTemplate.opsForValue().setIfAbsent(key, value, expireTime, timeUnit);
|
} catch (Exception e) {
|
log.error("Failed to acquire lock for key: {}", key, e);
|
return false;
|
}
|
}
|
|
/**
|
* 解锁
|
*/
|
public static void releaseLock(String key, String value) {
|
if (StringUtils.isBlank(key)) {
|
return;
|
}
|
if (Objects.equals(value, redisTemplate.opsForValue().get(key))) {
|
redisTemplate.delete(key);
|
}
|
}
|
/**
|
* SETNX with EXPIRE (原子操作)
|
* 如果 key 不存在,则设置 key 的值为 value 并设置过期时间,返回 true.
|
* 如果 key 已存在,则不做任何操作,返回 false.
|
*
|
* @param key 键
|
* @param value 值
|
* @param expire 过期时间(秒)
|
* @return boolean 设置成功返回 true,否则返回 false
|
*/
|
public static boolean setIfAbsent(String key, Object value, long expire) {
|
// setIfAbsent(key, value, duration) 是一个原子操作,等同于 SET key value EX seconds NX
|
return redisTemplate.opsForValue().setIfAbsent(key, value, expire, TimeUnit.SECONDS);
|
}
|
|
/**
|
* 向通道发布消息
|
*/
|
public static boolean convertAndSend(String channel, DeviceUnregisterMessage message) {
|
if (!StringUtils.isNotBlank(channel)) {
|
return false;
|
}
|
try {
|
redisTemplate.convertAndSend(channel, message);
|
log.info("发送消息成功,channel:{},message:{}", channel, message);
|
return true;
|
} catch (Exception e) {
|
log.info("发送消息失败,channel:{},message:{}", channel, message);
|
e.printStackTrace();
|
}
|
return false;
|
}
|
|
public static Map<String, String> leftPop(String cacheKey) {
|
return (Map<String, String>) redisTemplate.opsForList().leftPop(cacheKey);
|
}
|
}
|