package com.lwy.it.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.QueryTimeoutException;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public final class RedisOperateUtil {
// 默认最长时效时间(秒)
private static final long EXPIRED_TIME = 604800L;
private void setDefaultExpiredTime(final String key) {
Long ttl = this.ttl(key, TimeUnit.SECONDS);
if (!Objects.isNull(ttl) && ttl.equals(-1L)) {
this.expire(key, EXPIRED_TIME, TimeUnit.SECONDS);
}
}
@Autowired
private RedisOperations redisTemplate;
/**
* DEL key
*
* @param key Redis Key
* @return 是否删除成功
*/
public Boolean del(final String key) {
Boolean result = null;
try {
result = redisTemplate.delete(key);
} catch (Exception exception) {
log.error(exception.getMessage(), exception);
}
return result;
}
/**
* DEL key key
*
* @param keys Redis Keys
* @return 删除的数量
*/
public Long del(final Set<String> keys) {
Long result = null;
try {
result = redisTemplate.delete(keys);
} catch (Exception exception) {
log.error(exception.getMessage(), exception);
}
return result;
}
/**
* EXISTS key
*
* @param key Redis Key
* @return 是否存在key
*/
public Boolean exists(final String key) {
Boolean result = null;
try {
result = redisTemplate.hasKey(key);
} catch (RedisConnectionFailureException exception) {
log.error(exception.getMessage(), exception);
} catch (QueryTimeoutException exception) {
log.error(exception.getMessage(), exception);
}
return result;
}
/**
* EXISTS key1 key2
*
* @param keys Redis Keys
* @return 指定为参数的键中存在的键数,多次提及和存在的键被多次计算。
*/
public Long exists(final Set<String> keys) {
Long result = null;
try {
result = redisTemplate.countExistingKeys(keys);
} catch (RedisConnectionFailureException exception) {
log.error(exception.getMessage(), exception);
} catch (QueryTimeoutException exception) {
log.error(exception.getMessage(), exception);
}
return result;
}
/**
* EXPIRE key seconds
*
* @param key Redis Key
* @param timeout 超时时间
* @param unit 时间粒度单位
* @return 在管道/事务中使用时为 null
*/
public Boolean expire(final String key, final long timeout, TimeUnit unit) {
Boolean result = null;
try {
result = redisTemplate.expire(key, timeout, unit);
} catch (Exception exception) {
log.error(exception.getMessage(), exception);
}
return result;
}
/**
* TTL key
*
* @param key Redis Key
* @param timeUnit 时间粒度单位
* @return 按照给定时间粒度单位,返回过期时间,时间粒度单位为空时默认为秒
*/
public Long ttl(final String key, TimeUnit timeUnit) {
if (Objects.isNull(timeUnit)) {
timeUnit = TimeUnit.SECONDS;
}
Long result = null;
try {
result = redisTemplate.getExpire(key, timeUnit);
} catch (RedisConnectionFailureException exception) {
log.error(exception.getMessage(), exception);
} catch (QueryTimeoutException exception) {
log.error(exception.getMessage(), exception);
}
return result;
}
/**
* TTL key
*
* @param key Redis Key
* @return 按照给定时间粒度单位,返回过期时间,时间粒度单位为空时默认为秒
*/
public Long ttl(final String key) {
return this.ttl(key, TimeUnit.SECONDS);
}
/**
* SET key value
*
* @param key Redis Key
* @param value 存储的value
* @param <V> value泛型类型
*/
public <V> void set(final String key, final V value) {
this.setex(key, value, EXPIRED_TIME, TimeUnit.SECONDS);
}
/**
* GET key
*
* @param key Redis Key
* @param <V> value泛型类型
* @return 返回存储的value
*/
public <V> V get(final String key) {
ValueOperations<String, V> operations = redisTemplate.opsForValue();
V result = null;
try {
result = operations.get(key);
} catch (Exception exception) {
log.error(exception.getMessage(), exception);
}
return result;
}
/**
* GETSET key value
*
* @param key Redis Key
* @param value 存储的value
* @param <V> value泛型类型
* @return 指定key的值,并返回key的旧值
*/
public <V> V getset(final String key, final V value) {
ValueOperations<String, V> operations = redisTemplate.opsForValue();
V result = null;
try {
result = operations.getAndSet(key, value);
this.setDefaultExpiredTime(key);
} catch (Exception exception) {
log.error(exception.getMessage(), exception);
}
return result;
}
/**
* MSET key value [key value ...]
* 缺少过期时间,不建议使用
*
* @param map 不能为空
* @param <V> value泛型类型
*/
@Deprecated
public <V> void mset(final Map<String, V> map) {
if (!CollectionUtils.isEmpty(map)) {
ValueOperations<String, V> operations = redisTemplate.opsForValue();
try {
operations.multiSet(map);
} catch (Exception exception) {
log.error(exception.getMessage(), exception);
}
} else {
log.warn("Parameters map is null or empty");
}
}
/**
* MGET key [key ...]
* 建议使用LinkedHashSet
*
* @param keys 不重复Redis Key集合,不能为空
* @param <V> value泛型类型
* @return 结果List集合
*/
public <V> List<V> mget(final Set<String> keys) {
List<V> result = Collections.emptyList();
if (!CollectionUtils.isEmpty(keys)) {
ValueOperations<String, V> operations = redisTemplate.opsForValue();
try {
result = operations.multiGet(keys);
} catch (RedisConnectionFailureException exception) {
log.error(exception.getMessage(), exception);
} catch (QueryTimeoutException exception) {
log.error(exception.getMessage(), exception);
}
}
return result;
}
/**
* SETEX key seconds value
*
* @param key Redis Key
* @param value Redis Value
* @param timeout 超时时间
* @param unit 单位
* @param <V> value泛型类型