mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-18 09:03:43 +08:00
fix:RedisUtils相关修改
This commit is contained in:
@@ -15,7 +15,8 @@ import java.util.Objects;
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class RedisConfig {
|
public class RedisConfig {
|
||||||
@Bean
|
|
||||||
|
@Bean("myRedisTemplate")
|
||||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
// 创建模板
|
// 创建模板
|
||||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||||
@@ -30,10 +31,11 @@ public class RedisConfig {
|
|||||||
// value和 hashValue采用 JSON序列化
|
// value和 hashValue采用 JSON序列化
|
||||||
redisTemplate.setValueSerializer(jsonRedisSerializer);
|
redisTemplate.setValueSerializer(jsonRedisSerializer);
|
||||||
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
|
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
|
||||||
|
redisTemplate.afterPropertiesSet();
|
||||||
return redisTemplate;
|
return redisTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MyRedisSerializerCustomized extends GenericJackson2JsonRedisSerializer {
|
private static class MyRedisSerializerCustomized extends GenericJackson2JsonRedisSerializer {
|
||||||
@Override
|
@Override
|
||||||
public byte[] serialize(Object source) throws SerializationException {
|
public byte[] serialize(Object source) throws SerializationException {
|
||||||
if (Objects.nonNull(source)) {
|
if (Objects.nonNull(source)) {
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public class CursorUtils {
|
|||||||
.map(String::valueOf)
|
.map(String::valueOf)
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
Boolean isLast = result.size() != cursorPageBaseReq.getPageSize();
|
Boolean isLast = result.size() != cursorPageBaseReq.getPageSize();
|
||||||
return new CursorPageBaseResp(cursor, isLast, result);
|
return new CursorPageBaseResp<>(cursor, isLast, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> CursorPageBaseResp<T> getCursorPageByMysql(IService<T> mapper, CursorPageBaseReq request, Consumer<LambdaQueryWrapper<T>> initWrapper, SFunction<T, ?> cursorColumn) {
|
public <T> CursorPageBaseResp<T> getCursorPageByMysql(IService<T> mapper, CursorPageBaseReq request, Consumer<LambdaQueryWrapper<T>> initWrapper, SFunction<T, ?> cursorColumn) {
|
||||||
@@ -60,7 +60,7 @@ public class CursorUtils {
|
|||||||
.map(String::valueOf)
|
.map(String::valueOf)
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
Boolean isLast = page.getRecords().size() != request.getPageSize();
|
Boolean isLast = page.getRecords().size() != request.getPageSize();
|
||||||
return new CursorPageBaseResp(cursor, isLast, page.getRecords());
|
return new CursorPageBaseResp<>(cursor, isLast, page.getRecords());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,24 +8,20 @@ import org.springframework.data.redis.core.*;
|
|||||||
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
|
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
|
||||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||||
import org.springframework.data.redis.core.script.RedisScript;
|
import org.springframework.data.redis.core.script.RedisScript;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
|
||||||
public class RedisUtils {
|
public class RedisUtils {
|
||||||
public RedisTemplate redisTemplate;
|
|
||||||
|
|
||||||
private static StringRedisTemplate stringRedisTemplate;
|
private static StringRedisTemplate stringRedisTemplate;
|
||||||
|
private static RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
@PostConstruct
|
static {
|
||||||
public void init() {
|
RedisUtils.stringRedisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
|
||||||
this.stringRedisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
|
RedisUtils.redisTemplate = SpringUtil.getBean("myRedisTemplate");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String LUA_INCR_EXPIRE =
|
private static final String LUA_INCR_EXPIRE =
|
||||||
@@ -49,7 +45,7 @@ public class RedisUtils {
|
|||||||
* @param key 键
|
* @param key 键
|
||||||
* @param time 时间(秒)
|
* @param time 时间(秒)
|
||||||
*/
|
*/
|
||||||
public static boolean expire(String key, long time) {
|
public static Boolean expire(String key, long time) {
|
||||||
try {
|
try {
|
||||||
if (time > 0) {
|
if (time > 0) {
|
||||||
stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
|
stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
|
||||||
@@ -68,7 +64,7 @@ public class RedisUtils {
|
|||||||
* @param time 时间(秒)
|
* @param time 时间(秒)
|
||||||
* @param timeUnit 单位
|
* @param timeUnit 单位
|
||||||
*/
|
*/
|
||||||
public boolean expire(String key, long time, TimeUnit timeUnit) {
|
public static Boolean expire(String key, long time, TimeUnit timeUnit) {
|
||||||
try {
|
try {
|
||||||
if (time > 0) {
|
if (time > 0) {
|
||||||
redisTemplate.expire(key, time, timeUnit);
|
redisTemplate.expire(key, time, timeUnit);
|
||||||
@@ -86,7 +82,7 @@ public class RedisUtils {
|
|||||||
* @param key 键 不能为null
|
* @param key 键 不能为null
|
||||||
* @return 时间(秒) 返回0代表为永久有效
|
* @return 时间(秒) 返回0代表为永久有效
|
||||||
*/
|
*/
|
||||||
public static long getExpire(String key) {
|
public static Long getExpire(String key) {
|
||||||
return stringRedisTemplate.getExpire(key, TimeUnit.SECONDS);
|
return stringRedisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +92,7 @@ public class RedisUtils {
|
|||||||
* @param key 键 不能为null
|
* @param key 键 不能为null
|
||||||
* @return 时间(秒) 返回0代表为永久有效
|
* @return 时间(秒) 返回0代表为永久有效
|
||||||
*/
|
*/
|
||||||
public static long getExpire(String key, TimeUnit timeUnit) {
|
public static Long getExpire(String key, TimeUnit timeUnit) {
|
||||||
return stringRedisTemplate.getExpire(key, timeUnit);
|
return stringRedisTemplate.getExpire(key, timeUnit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +102,7 @@ public class RedisUtils {
|
|||||||
* @param pattern key
|
* @param pattern key
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public List<String> scan(String pattern) {
|
public static List<String> scan(String pattern) {
|
||||||
ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
|
ScanOptions options = ScanOptions.scanOptions().match(pattern).build();
|
||||||
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
|
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
|
||||||
RedisConnection rc = Objects.requireNonNull(factory).getConnection();
|
RedisConnection rc = Objects.requireNonNull(factory).getConnection();
|
||||||
@@ -131,7 +127,7 @@ public class RedisUtils {
|
|||||||
* @param size 每页数目
|
* @param size 每页数目
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public List<String> findKeysForPage(String patternKey, int page, int size) {
|
public static List<String> findKeysForPage(String patternKey, int page, int size) {
|
||||||
ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
|
ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
|
||||||
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
|
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
|
||||||
RedisConnection rc = Objects.requireNonNull(factory).getConnection();
|
RedisConnection rc = Objects.requireNonNull(factory).getConnection();
|
||||||
@@ -167,7 +163,7 @@ public class RedisUtils {
|
|||||||
* @param key 键
|
* @param key 键
|
||||||
* @return true 存在 false不存在
|
* @return true 存在 false不存在
|
||||||
*/
|
*/
|
||||||
public boolean hasKey(String key) {
|
public static Boolean hasKey(String key) {
|
||||||
try {
|
try {
|
||||||
return redisTemplate.hasKey(key);
|
return redisTemplate.hasKey(key);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -185,21 +181,23 @@ public class RedisUtils {
|
|||||||
public static void del(String... keys) {
|
public static void del(String... keys) {
|
||||||
if (keys != null && keys.length > 0) {
|
if (keys != null && keys.length > 0) {
|
||||||
if (keys.length == 1) {
|
if (keys.length == 1) {
|
||||||
boolean result = stringRedisTemplate.delete(keys[0]);
|
Boolean result = stringRedisTemplate.delete(keys[0]);
|
||||||
log.debug("--------------------------------------------");
|
|
||||||
log.debug(new StringBuilder("删除缓存:").append(keys[0]).append(",结果:").append(result).toString());
|
|
||||||
log.debug("--------------------------------------------");
|
log.debug("--------------------------------------------");
|
||||||
|
log.debug("删除缓存:" + keys[0] + ",结果:" + result);
|
||||||
} else {
|
} else {
|
||||||
Set<String> keySet = new HashSet<>();
|
Set<String> keySet = new HashSet<>();
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
keySet.addAll(stringRedisTemplate.keys(key));
|
Set<String> stringSet = stringRedisTemplate.keys(key);
|
||||||
|
if(Objects.nonNull(stringSet) && !stringSet.isEmpty()){
|
||||||
|
keySet.addAll(stringSet);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
long count = stringRedisTemplate.delete(keySet);
|
Long count = stringRedisTemplate.delete(keySet);
|
||||||
log.debug("--------------------------------------------");
|
log.debug("--------------------------------------------");
|
||||||
log.debug("成功删除缓存:" + keySet.toString());
|
log.debug("成功删除缓存:" + keySet);
|
||||||
log.debug("缓存删除数量:" + count + "个");
|
log.debug("缓存删除数量:" + count + "个");
|
||||||
log.debug("--------------------------------------------");
|
|
||||||
}
|
}
|
||||||
|
log.debug("--------------------------------------------");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,7 +220,7 @@ public class RedisUtils {
|
|||||||
* @param value 值
|
* @param value 值
|
||||||
* @return true成功 false失败
|
* @return true成功 false失败
|
||||||
*/
|
*/
|
||||||
public static boolean set(String key, Object value) {
|
public static Boolean set(String key, Object value) {
|
||||||
try {
|
try {
|
||||||
stringRedisTemplate.opsForValue().set(key, objToStr(value));
|
stringRedisTemplate.opsForValue().set(key, objToStr(value));
|
||||||
return true;
|
return true;
|
||||||
@@ -243,7 +241,10 @@ public class RedisUtils {
|
|||||||
|
|
||||||
public static <T> List<T> mget(Collection<String> keys, Class<T> tClass) {
|
public static <T> List<T> mget(Collection<String> keys, Class<T> tClass) {
|
||||||
List<String> list = stringRedisTemplate.opsForValue().multiGet(keys);
|
List<String> list = stringRedisTemplate.opsForValue().multiGet(keys);
|
||||||
return (List<T>) list.stream().map(o -> toBeanOrNull(o, tClass)).collect(Collectors.toList());
|
if (Objects.isNull(list)) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
return list.stream().map(o -> toBeanOrNull(o, tClass)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T> T toBeanOrNull(String json, Class<T> tClass) {
|
static <T> T toBeanOrNull(String json, Class<T> tClass) {
|
||||||
@@ -271,7 +272,7 @@ public class RedisUtils {
|
|||||||
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
|
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
|
||||||
* @return true成功 false 失败
|
* @return true成功 false 失败
|
||||||
*/
|
*/
|
||||||
public boolean set(String key, Object value, long time) {
|
public Boolean set(String key, Object value, long time) {
|
||||||
try {
|
try {
|
||||||
if (time > 0) {
|
if (time > 0) {
|
||||||
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
||||||
@@ -294,7 +295,7 @@ public class RedisUtils {
|
|||||||
* @param timeUnit 类型
|
* @param timeUnit 类型
|
||||||
* @return true成功 false 失败
|
* @return true成功 false 失败
|
||||||
*/
|
*/
|
||||||
public static boolean set(String key, Object value, long time, TimeUnit timeUnit) {
|
public static Boolean set(String key, Object value, long time, TimeUnit timeUnit) {
|
||||||
try {
|
try {
|
||||||
if (time > 0) {
|
if (time > 0) {
|
||||||
stringRedisTemplate.opsForValue().set(key, objToStr(value), time, timeUnit);
|
stringRedisTemplate.opsForValue().set(key, objToStr(value), time, timeUnit);
|
||||||
@@ -317,7 +318,7 @@ public class RedisUtils {
|
|||||||
* @param item 项 不能为null
|
* @param item 项 不能为null
|
||||||
* @return 值
|
* @return 值
|
||||||
*/
|
*/
|
||||||
public Object hget(String key, String item) {
|
public static Object hget(String key, String item) {
|
||||||
return redisTemplate.opsForHash().get(key, item);
|
return redisTemplate.opsForHash().get(key, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,7 +328,7 @@ public class RedisUtils {
|
|||||||
* @param key 键
|
* @param key 键
|
||||||
* @return 对应的多个键值
|
* @return 对应的多个键值
|
||||||
*/
|
*/
|
||||||
public Map<Object, Object> hmget(String key) {
|
public static Map<Object, Object> hmget(String key) {
|
||||||
return redisTemplate.opsForHash().entries(key);
|
return redisTemplate.opsForHash().entries(key);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -339,7 +340,7 @@ public class RedisUtils {
|
|||||||
* @param map 对应多个键值
|
* @param map 对应多个键值
|
||||||
* @return true 成功 false 失败
|
* @return true 成功 false 失败
|
||||||
*/
|
*/
|
||||||
public boolean hmset(String key, Map<String, Object> map) {
|
public static Boolean hmset(String key, Map<String, Object> map) {
|
||||||
try {
|
try {
|
||||||
redisTemplate.opsForHash().putAll(key, map);
|
redisTemplate.opsForHash().putAll(key, map);
|
||||||
return true;
|
return true;
|
||||||
@@ -357,7 +358,7 @@ public class RedisUtils {
|
|||||||
* @param time 时间(秒)
|
* @param time 时间(秒)
|
||||||
* @return true成功 false失败
|
* @return true成功 false失败
|
||||||
*/
|
*/
|
||||||
public boolean hmset(String key, Map<String, Object> map, long time) {
|
public static Boolean hmset(String key, Map<String, Object> map, long time) {
|
||||||
try {
|
try {
|
||||||
redisTemplate.opsForHash().putAll(key, map);
|
redisTemplate.opsForHash().putAll(key, map);
|
||||||
if (time > 0) {
|
if (time > 0) {
|
||||||
@@ -378,7 +379,7 @@ public class RedisUtils {
|
|||||||
* @param value 值
|
* @param value 值
|
||||||
* @return true 成功 false失败
|
* @return true 成功 false失败
|
||||||
*/
|
*/
|
||||||
public boolean hset(String key, String item, Object value) {
|
public static Boolean hset(String key, String item, Object value) {
|
||||||
try {
|
try {
|
||||||
redisTemplate.opsForHash().put(key, item, value);
|
redisTemplate.opsForHash().put(key, item, value);
|
||||||
return true;
|
return true;
|
||||||
@@ -397,7 +398,7 @@ public class RedisUtils {
|
|||||||
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
|
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
|
||||||
* @return true 成功 false失败
|
* @return true 成功 false失败
|
||||||
*/
|
*/
|
||||||
public boolean hset(String key, String item, Object value, long time) {
|
public static Boolean hset(String key, String item, Object value, long time) {
|
||||||
try {
|
try {
|
||||||
redisTemplate.opsForHash().put(key, item, value);
|
redisTemplate.opsForHash().put(key, item, value);
|
||||||
if (time > 0) {
|
if (time > 0) {
|
||||||
@@ -416,7 +417,7 @@ public class RedisUtils {
|
|||||||
* @param key 键 不能为null
|
* @param key 键 不能为null
|
||||||
* @param item 项 可以使多个 不能为null
|
* @param item 项 可以使多个 不能为null
|
||||||
*/
|
*/
|
||||||
public void hdel(String key, Object... item) {
|
public static void hdel(String key, Object... item) {
|
||||||
redisTemplate.opsForHash().delete(key, item);
|
redisTemplate.opsForHash().delete(key, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -427,7 +428,7 @@ public class RedisUtils {
|
|||||||
* @param item 项 不能为null
|
* @param item 项 不能为null
|
||||||
* @return true 存在 false不存在
|
* @return true 存在 false不存在
|
||||||
*/
|
*/
|
||||||
public boolean hHasKey(String key, String item) {
|
public static Boolean hHasKey(String key, String item) {
|
||||||
return redisTemplate.opsForHash().hasKey(key, item);
|
return redisTemplate.opsForHash().hasKey(key, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -439,7 +440,7 @@ public class RedisUtils {
|
|||||||
* @param by 要增加几(大于0)
|
* @param by 要增加几(大于0)
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public double hincr(String key, String item, double by) {
|
public static Double hincr(String key, String item, double by) {
|
||||||
return redisTemplate.opsForHash().increment(key, item, by);
|
return redisTemplate.opsForHash().increment(key, item, by);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -451,7 +452,7 @@ public class RedisUtils {
|
|||||||
* @param by 要减少记(小于0)
|
* @param by 要减少记(小于0)
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public double hdecr(String key, String item, double by) {
|
public static Double hdecr(String key, String item, double by) {
|
||||||
return redisTemplate.opsForHash().increment(key, item, -by);
|
return redisTemplate.opsForHash().increment(key, item, -by);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,7 +464,7 @@ public class RedisUtils {
|
|||||||
* @param key 键
|
* @param key 键
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<Object> sGet(String key) {
|
public static Set<Object> sGet(String key) {
|
||||||
try {
|
try {
|
||||||
return redisTemplate.opsForSet().members(key);
|
return redisTemplate.opsForSet().members(key);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -479,7 +480,7 @@ public class RedisUtils {
|
|||||||
* @param value 值
|
* @param value 值
|
||||||
* @return true 存在 false不存在
|
* @return true 存在 false不存在
|
||||||
*/
|
*/
|
||||||
public boolean sHasKey(String key, Object value) {
|
public static Boolean sHasKey(String key, Object value) {
|
||||||
try {
|
try {
|
||||||
return redisTemplate.opsForSet().isMember(key, value);
|
return redisTemplate.opsForSet().isMember(key, value);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -495,12 +496,12 @@ public class RedisUtils {
|
|||||||
* @param values 值 可以是多个
|
* @param values 值 可以是多个
|
||||||
* @return 成功个数
|
* @return 成功个数
|
||||||
*/
|
*/
|
||||||
public long sSet(String key, Object... values) {
|
public static Long sSet(String key, Object... values) {
|
||||||
try {
|
try {
|
||||||
return redisTemplate.opsForSet().add(key, values);
|
return redisTemplate.opsForSet().add(key, values);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return 0;
|
return 0L;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -512,7 +513,7 @@ public class RedisUtils {
|
|||||||
* @param values 值 可以是多个
|
* @param values 值 可以是多个
|
||||||
* @return 成功个数
|
* @return 成功个数
|
||||||
*/
|
*/
|
||||||
public long sSetAndTime(String key, long time, Object... values) {
|
public static Long sSetAndTime(String key, long time, Object... values) {
|
||||||
try {
|
try {
|
||||||
Long count = redisTemplate.opsForSet().add(key, values);
|
Long count = redisTemplate.opsForSet().add(key, values);
|
||||||
if (time > 0) {
|
if (time > 0) {
|
||||||
@@ -521,7 +522,7 @@ public class RedisUtils {
|
|||||||
return count;
|
return count;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return 0;
|
return 0L;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -531,12 +532,12 @@ public class RedisUtils {
|
|||||||
* @param key 键
|
* @param key 键
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public long sGetSetSize(String key) {
|
public static Long sGetSetSize(String key) {
|
||||||
try {
|
try {
|
||||||
return redisTemplate.opsForSet().size(key);
|
return redisTemplate.opsForSet().size(key);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return 0;
|
return 0L;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -547,13 +548,12 @@ public class RedisUtils {
|
|||||||
* @param values 值 可以是多个
|
* @param values 值 可以是多个
|
||||||
* @return 移除的个数
|
* @return 移除的个数
|
||||||
*/
|
*/
|
||||||
public long setRemove(String key, Object... values) {
|
public static Long setRemove(String key, Object... values) {
|
||||||
try {
|
try {
|
||||||
Long count = redisTemplate.opsForSet().remove(key, values);
|
return redisTemplate.opsForSet().remove(key, values);
|
||||||
return count;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return 0;
|
return 0L;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -567,7 +567,7 @@ public class RedisUtils {
|
|||||||
* @param end 结束 0 到 -1代表所有值
|
* @param end 结束 0 到 -1代表所有值
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public List<Object> lGet(String key, long start, long end) {
|
public static List<Object> lGet(String key, long start, long end) {
|
||||||
try {
|
try {
|
||||||
return redisTemplate.opsForList().range(key, start, end);
|
return redisTemplate.opsForList().range(key, start, end);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -582,12 +582,12 @@ public class RedisUtils {
|
|||||||
* @param key 键
|
* @param key 键
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public long lGetListSize(String key) {
|
public static Long lGetListSize(String key) {
|
||||||
try {
|
try {
|
||||||
return redisTemplate.opsForList().size(key);
|
return redisTemplate.opsForList().size(key);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return 0;
|
return 0L;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -598,7 +598,7 @@ public class RedisUtils {
|
|||||||
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
|
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Object lGetIndex(String key, long index) {
|
public static Object lGetIndex(String key, long index) {
|
||||||
try {
|
try {
|
||||||
return redisTemplate.opsForList().index(key, index);
|
return redisTemplate.opsForList().index(key, index);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -614,7 +614,7 @@ public class RedisUtils {
|
|||||||
* @param value 值
|
* @param value 值
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean lSet(String key, Object value) {
|
public static Boolean lSet(String key, Object value) {
|
||||||
try {
|
try {
|
||||||
redisTemplate.opsForList().rightPush(key, value);
|
redisTemplate.opsForList().rightPush(key, value);
|
||||||
return true;
|
return true;
|
||||||
@@ -632,7 +632,7 @@ public class RedisUtils {
|
|||||||
* @param time 时间(秒)
|
* @param time 时间(秒)
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean lSet(String key, Object value, long time) {
|
public static Boolean lSet(String key, Object value, long time) {
|
||||||
try {
|
try {
|
||||||
redisTemplate.opsForList().rightPush(key, value);
|
redisTemplate.opsForList().rightPush(key, value);
|
||||||
if (time > 0) {
|
if (time > 0) {
|
||||||
@@ -652,7 +652,7 @@ public class RedisUtils {
|
|||||||
* @param value 值
|
* @param value 值
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean lSet(String key, List<Object> value) {
|
public static Boolean lSet(String key, List<Object> value) {
|
||||||
try {
|
try {
|
||||||
redisTemplate.opsForList().rightPushAll(key, value);
|
redisTemplate.opsForList().rightPushAll(key, value);
|
||||||
return true;
|
return true;
|
||||||
@@ -670,7 +670,7 @@ public class RedisUtils {
|
|||||||
* @param time 时间(秒)
|
* @param time 时间(秒)
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean lSet(String key, List<Object> value, long time) {
|
public static Boolean lSet(String key, List<Object> value, long time) {
|
||||||
try {
|
try {
|
||||||
redisTemplate.opsForList().rightPushAll(key, value);
|
redisTemplate.opsForList().rightPushAll(key, value);
|
||||||
if (time > 0) {
|
if (time > 0) {
|
||||||
@@ -691,7 +691,7 @@ public class RedisUtils {
|
|||||||
* @param value 值
|
* @param value 值
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public boolean lUpdateIndex(String key, long index, Object value) {
|
public static Boolean lUpdateIndex(String key, long index, Object value) {
|
||||||
try {
|
try {
|
||||||
redisTemplate.opsForList().set(key, index, value);
|
redisTemplate.opsForList().set(key, index, value);
|
||||||
return true;
|
return true;
|
||||||
@@ -709,12 +709,12 @@ public class RedisUtils {
|
|||||||
* @param value 值
|
* @param value 值
|
||||||
* @return 移除的个数
|
* @return 移除的个数
|
||||||
*/
|
*/
|
||||||
public long lRemove(String key, long count, Object value) {
|
public static Long lRemove(String key, long count, Object value) {
|
||||||
try {
|
try {
|
||||||
return redisTemplate.opsForList().remove(key, count, value);
|
return redisTemplate.opsForList().remove(key, count, value);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return 0;
|
return 0L;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -723,11 +723,14 @@ public class RedisUtils {
|
|||||||
* @param ids id
|
* @param ids id
|
||||||
*/
|
*/
|
||||||
public void delByKeys(String prefix, Set<Long> ids) {
|
public void delByKeys(String prefix, Set<Long> ids) {
|
||||||
Set<Object> keys = new HashSet<>();
|
Set<String> keys = new HashSet<>();
|
||||||
for (Long id : ids) {
|
for (Long id : ids) {
|
||||||
keys.addAll(redisTemplate.keys(new StringBuffer(prefix).append(id).toString()));
|
Set<String> stringSet = redisTemplate.keys(prefix + id);
|
||||||
|
if (Objects.nonNull(stringSet) && !stringSet.isEmpty()) {
|
||||||
|
keys.addAll(stringSet);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
long count = redisTemplate.delete(keys);
|
Long count = redisTemplate.delete(keys);
|
||||||
// 此处提示可自行删除
|
// 此处提示可自行删除
|
||||||
log.debug("--------------------------------------------");
|
log.debug("--------------------------------------------");
|
||||||
log.debug("成功删除缓存:" + keys.toString());
|
log.debug("成功删除缓存:" + keys.toString());
|
||||||
@@ -761,7 +764,7 @@ public class RedisUtils {
|
|||||||
* @param values
|
* @param values
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zAdd(String key, Set<TypedTuple<String>> values) {
|
public static Long zAdd(String key, Set<TypedTuple<Object>> values) {
|
||||||
return redisTemplate.opsForZSet().add(key, values);
|
return redisTemplate.opsForZSet().add(key, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -770,7 +773,7 @@ public class RedisUtils {
|
|||||||
* @param values
|
* @param values
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zRemove(String key, Object... values) {
|
public static Long zRemove(String key, Object... values) {
|
||||||
return redisTemplate.opsForZSet().remove(key, values);
|
return redisTemplate.opsForZSet().remove(key, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -790,7 +793,7 @@ public class RedisUtils {
|
|||||||
* @param delta
|
* @param delta
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Double zIncrementScore(String key, String value, double delta) {
|
public static Double zIncrementScore(String key, String value, double delta) {
|
||||||
return redisTemplate.opsForZSet().incrementScore(key, value, delta);
|
return redisTemplate.opsForZSet().incrementScore(key, value, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -801,7 +804,7 @@ public class RedisUtils {
|
|||||||
* @param value
|
* @param value
|
||||||
* @return 0表示第一位
|
* @return 0表示第一位
|
||||||
*/
|
*/
|
||||||
public Long zRank(String key, Object value) {
|
public static Long zRank(String key, Object value) {
|
||||||
return redisTemplate.opsForZSet().rank(key, value);
|
return redisTemplate.opsForZSet().rank(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -812,7 +815,7 @@ public class RedisUtils {
|
|||||||
* @param value
|
* @param value
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zReverseRank(String key, Object value) {
|
public static Long zReverseRank(String key, Object value) {
|
||||||
return redisTemplate.opsForZSet().reverseRank(key, value);
|
return redisTemplate.opsForZSet().reverseRank(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -824,7 +827,7 @@ public class RedisUtils {
|
|||||||
* @param end 结束位置, -1查询所有
|
* @param end 结束位置, -1查询所有
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<String> zRange(String key, long start, long end) {
|
public static Set<Object> zRange(String key, long start, long end) {
|
||||||
return redisTemplate.opsForZSet().range(key, start, end);
|
return redisTemplate.opsForZSet().range(key, start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -836,7 +839,7 @@ public class RedisUtils {
|
|||||||
* @param end
|
* @param end
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<ZSetOperations.TypedTuple<String>> zRangeWithScores(String key, long start,
|
public static Set<ZSetOperations.TypedTuple<Object>> zRangeWithScores(String key, long start,
|
||||||
long end) {
|
long end) {
|
||||||
return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
|
return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
|
||||||
}
|
}
|
||||||
@@ -849,7 +852,7 @@ public class RedisUtils {
|
|||||||
* @param max 最大值
|
* @param max 最大值
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<String> zRangeByScore(String key, double min, double max) {
|
public static Set<Object> zRangeByScore(String key, double min, double max) {
|
||||||
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
|
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -861,7 +864,7 @@ public class RedisUtils {
|
|||||||
* @param max 最大值
|
* @param max 最大值
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,
|
public Set<TypedTuple<Object>> zRangeByScoreWithScores(String key,
|
||||||
double min, double max) {
|
double min, double max) {
|
||||||
return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
|
return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max);
|
||||||
}
|
}
|
||||||
@@ -874,7 +877,7 @@ public class RedisUtils {
|
|||||||
* @param end
|
* @param end
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<TypedTuple<String>> zRangeByScoreWithScores(String key,
|
public static Set<TypedTuple<Object>> zRangeByScoreWithScores(String key,
|
||||||
double min, double max, long start, long end) {
|
double min, double max, long start, long end) {
|
||||||
return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max,
|
return redisTemplate.opsForZSet().rangeByScoreWithScores(key, min, max,
|
||||||
start, end);
|
start, end);
|
||||||
@@ -888,7 +891,7 @@ public class RedisUtils {
|
|||||||
* @param end
|
* @param end
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<String> zReverseRange(String key, long start, long end) {
|
public static Set<Object> zReverseRange(String key, long start, long end) {
|
||||||
return redisTemplate.opsForZSet().reverseRange(key, start, end);
|
return redisTemplate.opsForZSet().reverseRange(key, start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -967,7 +970,7 @@ public class RedisUtils {
|
|||||||
* @param max
|
* @param max
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zCount(String key, double min, double max) {
|
public static Long zCount(String key, double min, double max) {
|
||||||
return redisTemplate.opsForZSet().count(key, min, max);
|
return redisTemplate.opsForZSet().count(key, min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -977,7 +980,7 @@ public class RedisUtils {
|
|||||||
* @param key
|
* @param key
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zSize(String key) {
|
public static Long zSize(String key) {
|
||||||
return redisTemplate.opsForZSet().size(key);
|
return redisTemplate.opsForZSet().size(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -998,7 +1001,7 @@ public class RedisUtils {
|
|||||||
* @param value
|
* @param value
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Double zScore(String key, Object value) {
|
public static Double zScore(String key, Object value) {
|
||||||
return redisTemplate.opsForZSet().score(key, value);
|
return redisTemplate.opsForZSet().score(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1010,7 +1013,7 @@ public class RedisUtils {
|
|||||||
* @param end
|
* @param end
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zRemoveRange(String key, long start, long end) {
|
public static Long zRemoveRange(String key, long start, long end) {
|
||||||
return redisTemplate.opsForZSet().removeRange(key, start, end);
|
return redisTemplate.opsForZSet().removeRange(key, start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1022,7 +1025,7 @@ public class RedisUtils {
|
|||||||
* @param max
|
* @param max
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zRemoveRangeByScore(String key, double min, double max) {
|
public static Long zRemoveRangeByScore(String key, double min, double max) {
|
||||||
return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
|
return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1034,7 +1037,7 @@ public class RedisUtils {
|
|||||||
* @param destKey
|
* @param destKey
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zUnionAndStore(String key, String otherKey, String destKey) {
|
public static Long zUnionAndStore(String key, String otherKey, String destKey) {
|
||||||
return redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
|
return redisTemplate.opsForZSet().unionAndStore(key, otherKey, destKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1044,7 +1047,7 @@ public class RedisUtils {
|
|||||||
* @param destKey
|
* @param destKey
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zUnionAndStore(String key, Collection<String> otherKeys,
|
public static Long zUnionAndStore(String key, Collection<String> otherKeys,
|
||||||
String destKey) {
|
String destKey) {
|
||||||
return redisTemplate.opsForZSet()
|
return redisTemplate.opsForZSet()
|
||||||
.unionAndStore(key, otherKeys, destKey);
|
.unionAndStore(key, otherKeys, destKey);
|
||||||
@@ -1058,7 +1061,7 @@ public class RedisUtils {
|
|||||||
* @param destKey
|
* @param destKey
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zIntersectAndStore(String key, String otherKey,
|
public static Long zIntersectAndStore(String key, String otherKey,
|
||||||
String destKey) {
|
String destKey) {
|
||||||
return redisTemplate.opsForZSet().intersectAndStore(key, otherKey,
|
return redisTemplate.opsForZSet().intersectAndStore(key, otherKey,
|
||||||
destKey);
|
destKey);
|
||||||
@@ -1072,7 +1075,7 @@ public class RedisUtils {
|
|||||||
* @param destKey
|
* @param destKey
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Long zIntersectAndStore(String key, Collection<String> otherKeys,
|
public static Long zIntersectAndStore(String key, Collection<String> otherKeys,
|
||||||
String destKey) {
|
String destKey) {
|
||||||
return redisTemplate.opsForZSet().intersectAndStore(key, otherKeys,
|
return redisTemplate.opsForZSet().intersectAndStore(key, otherKeys,
|
||||||
destKey);
|
destKey);
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ public class LoginServiceImpl implements LoginService {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private JwtUtils jwtUtils;
|
private JwtUtils jwtUtils;
|
||||||
@Autowired
|
|
||||||
private RedisUtils redisUtils;
|
|
||||||
//token过期时间
|
//token过期时间
|
||||||
private static final Integer TOKEN_EXPIRE_DAYS = 5;
|
private static final Integer TOKEN_EXPIRE_DAYS = 5;
|
||||||
//token续期时间
|
//token续期时间
|
||||||
@@ -37,6 +35,7 @@ public class LoginServiceImpl implements LoginService {
|
|||||||
* @param token
|
* @param token
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean verify(String token) {
|
public boolean verify(String token) {
|
||||||
Long uid = jwtUtils.getUidOrNull(token);
|
Long uid = jwtUtils.getUidOrNull(token);
|
||||||
if (Objects.isNull(uid)) {
|
if (Objects.isNull(uid)) {
|
||||||
@@ -48,25 +47,26 @@ public class LoginServiceImpl implements LoginService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
|
@Override
|
||||||
public void renewalTokenIfNecessary(String token) {
|
public void renewalTokenIfNecessary(String token) {
|
||||||
Long uid = jwtUtils.getUidOrNull(token);
|
Long uid = jwtUtils.getUidOrNull(token);
|
||||||
if (Objects.isNull(uid)) {
|
if (Objects.isNull(uid)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String key = RedisKey.getKey(RedisKey.USER_TOKEN_STRING, uid);
|
String key = RedisKey.getKey(RedisKey.USER_TOKEN_STRING, uid);
|
||||||
long expireDays = redisUtils.getExpire(key, TimeUnit.DAYS);
|
long expireDays = RedisUtils.getExpire(key, TimeUnit.DAYS);
|
||||||
if (expireDays == -2) {//不存在的key
|
if (expireDays == -2) {//不存在的key
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (expireDays < TOKEN_RENEWAL_DAYS) {//小于一天的token帮忙续期
|
if (expireDays < TOKEN_RENEWAL_DAYS) {//小于一天的token帮忙续期
|
||||||
redisUtils.expire(key, TOKEN_EXPIRE_DAYS, TimeUnit.DAYS);
|
RedisUtils.expire(key, TOKEN_EXPIRE_DAYS, TimeUnit.DAYS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String login(Long uid) {
|
public String login(Long uid) {
|
||||||
String key = RedisKey.getKey(RedisKey.USER_TOKEN_STRING, uid);
|
String key = RedisKey.getKey(RedisKey.USER_TOKEN_STRING, uid);
|
||||||
String token = redisUtils.getStr(key);
|
String token = RedisUtils.getStr(key);
|
||||||
if (StrUtil.isNotBlank(token)) {
|
if (StrUtil.isNotBlank(token)) {
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user