From be5a9c56369e37de9be1f71e0bb83de93e85926e Mon Sep 17 00:00:00 2001 From: pnoker Date: Tue, 23 Nov 2021 22:19:41 +0800 Subject: [PATCH 1/4] fix: redis mset --- .../auth/service/impl/TokenServiceImpl.java | 2 +- .../service/impl/PointValueServiceImpl.java | 12 +- .../java/com/dc3/common/utils/RedisUtil.java | 166 ++++++++++++------ dc3/dependencies/maven/settings.xml | 3 +- 4 files changed, 121 insertions(+), 62 deletions(-) diff --git a/dc3-center/dc3-auth/src/main/java/com/dc3/center/auth/service/impl/TokenServiceImpl.java b/dc3-center/dc3-auth/src/main/java/com/dc3/center/auth/service/impl/TokenServiceImpl.java index 307d44628..cdc8ebdcb 100644 --- a/dc3-center/dc3-auth/src/main/java/com/dc3/center/auth/service/impl/TokenServiceImpl.java +++ b/dc3-center/dc3-auth/src/main/java/com/dc3/center/auth/service/impl/TokenServiceImpl.java @@ -106,7 +106,7 @@ public class TokenServiceImpl implements TokenService { @Override public boolean cancelToken(String username) { - redisUtil.removeKey(Common.Cache.USER + Common.Cache.TOKEN + Common.Cache.SEPARATOR + username); + redisUtil.deleteKey(Common.Cache.USER + Common.Cache.TOKEN + Common.Cache.SEPARATOR + username); return true; } diff --git a/dc3-center/dc3-data/src/main/java/com/dc3/center/data/service/impl/PointValueServiceImpl.java b/dc3-center/dc3-data/src/main/java/com/dc3/center/data/service/impl/PointValueServiceImpl.java index 5ae3ca8ac..5a482d481 100644 --- a/dc3-center/dc3-data/src/main/java/com/dc3/center/data/service/impl/PointValueServiceImpl.java +++ b/dc3-center/dc3-data/src/main/java/com/dc3/center/data/service/impl/PointValueServiceImpl.java @@ -134,7 +134,7 @@ public class PointValueServiceImpl implements PointValueService { String prefix = Common.Cache.REAL_TIME_VALUE_KEY_PREFIX + deviceId + "_"; List keys = listR.getData().stream().map(point -> prefix + point.getId()).collect(Collectors.toList()); if (keys.size() > 0) { - List pointValues = redisUtil.getKeys(keys, PointValue.class); + List pointValues = redisUtil.getKey(keys, PointValue.class); pointValues = pointValues.stream().filter(Objects::nonNull).map(pointValue -> pointValue.setTimeOut(null).setTimeUnit(null)).collect(Collectors.toList()); if (pointValues.size() > 0) { return pointValues; @@ -318,7 +318,15 @@ public class PointValueServiceImpl implements PointValueService { * @param pointValues Point Value Array */ private void savePointValuesToRedis(final List pointValues) { - pointValues.forEach(this::savePointValueToRedis); + Map valueMap = new HashMap<>(16); + Map expireMap = new HashMap<>(16); + for (PointValue pointValue : pointValues) { + String pointIdKey = pointValue.getPointId() != null ? String.valueOf(pointValue.getPointId()) : Common.Cache.ASTERISK; + String key = Common.Cache.REAL_TIME_VALUE_KEY_PREFIX + pointValue.getDeviceId() + Common.Cache.DOT + pointIdKey; + valueMap.put(key, pointValue); + expireMap.put(key, pointValue.getTimeUnit().toMillis(pointValue.getTimeOut())); + } + redisUtil.setKey(valueMap, expireMap); } private PointValue latestPointValue(Long deviceId, Long pointId) { diff --git a/dc3-common/dc3-core/src/main/java/com/dc3/common/utils/RedisUtil.java b/dc3-common/dc3-core/src/main/java/com/dc3/common/utils/RedisUtil.java index fb2139ebf..10b46d8c9 100644 --- a/dc3-common/dc3-core/src/main/java/com/dc3/common/utils/RedisUtil.java +++ b/dc3-common/dc3-core/src/main/java/com/dc3/common/utils/RedisUtil.java @@ -15,12 +15,15 @@ package com.dc3.common.utils; import cn.hutool.core.convert.Convert; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import java.util.Arrays; +import java.util.Date; import java.util.List; +import java.util.Map; import java.util.concurrent.TimeUnit; /** @@ -33,22 +36,6 @@ public class RedisUtil { @Resource private RedisTemplate redisTemplate; - /** - * 判断 Key 是否存在 - * - * @param key String key - * @return boolean - */ - public boolean hasKey(String key) { - try { - Boolean hasKey = redisTemplate.hasKey(key); - return null != hasKey ? hasKey : false; - } catch (Exception e) { - log.error(e.getMessage(), e); - } - return false; - } - /** * 添加 Key 缓存 * @@ -63,36 +50,6 @@ public class RedisUtil { } } - /** - * 批量添加 Key 缓存 - * - * @param key String key - * @param value Object - */ - public void setKeys(String key, Object value) { - try { - redisTemplate.opsForValue().set(key, value); - } catch (Exception e) { - log.error(e.getMessage(), e); - } - } - - /** - * 批量添加 Key 缓存,并设置失效时间 - * - * @param key String key - * @param value Object - * @param time Time - * @param unit TimeUnit - */ - public void setKeys(String key, Object value, long time, TimeUnit unit) { - try { - redisTemplate.opsForValue().set(key, value, time, unit); - } catch (Exception e) { - log.error(e.getMessage(), e); - } - } - /** * 添加 Key 缓存,并设置失效时间 * @@ -109,6 +66,34 @@ public class RedisUtil { } } + /** + * 批量添加 Key 缓存 + * + * @param valuesMap Map + */ + public void setKey(Map valuesMap) { + try { + redisTemplate.opsForValue().multiSet(valuesMap); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + + /** + * 批量添加 Key 缓存,并设置失效时间 + * + * @param valueMap Map + * @param expireMillis Map + */ + public void setKey(Map valueMap, Map expireMillis) { + try { + redisTemplate.opsForValue().multiSet(valueMap); + setExpire(expireMillis); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + /** * 获取 Key 缓存 * @@ -127,13 +112,13 @@ public class RedisUtil { } /** - * 获取 Keys 缓存 + * 批量获取 Key 缓存 * * @param keys String key array * @param T * @return T Array */ - public List getKeys(List keys, Class type) { + public List getKey(List keys, Class type) { try { List objects = redisTemplate.opsForValue().multiGet(keys); return Convert.toList(type, objects); @@ -144,24 +129,49 @@ public class RedisUtil { } /** - * 删除 Keys 缓存 + * 删除 Key 缓存 + * + * @param key Key + */ + public void deleteKey(String key) { + try { + redisTemplate.delete(key); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + + /** + * 批量删除 Key 缓存 * * @param keys Key Array */ - public void removeKey(String... keys) { - if (null != keys && keys.length > 0) { + public void deleteKey(List keys) { + if (null != keys && keys.size() > 0) { try { - if (keys.length == 1) { - redisTemplate.delete(keys[0]); - } else { - redisTemplate.delete(Arrays.asList(keys)); - } + redisTemplate.delete(keys); } catch (Exception e) { log.error(e.getMessage(), e); } } } + /** + * 判断 Key 是否存在 + * + * @param key String key + * @return boolean + */ + public boolean hasKey(String key) { + try { + Boolean hasKey = redisTemplate.hasKey(key); + return null != hasKey ? hasKey : false; + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return false; + } + /** * 指定键值失效时间 * @@ -179,6 +189,47 @@ public class RedisUtil { } } + /** + * 批量指定键值失效时间 + * + * @param expireMillis Map + */ + public void setExpire(Map expireMillis) { + try { + if (null != expireMillis && expireMillis.size() > 0) { + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); + redisTemplate.execute((RedisCallback) connection -> { + expireMillis.forEach((key, expire) -> { + byte[] serialize = stringRedisSerializer.serialize(key); + if (null != serialize) { + connection.pExpire(serialize, expire); + } + }); + return null; + }); + } + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + + /** + * 指定键值在指定时间失效 + * + * @param key String key + * @param date Date + */ + public void setExpireAt(String key, Date date) { + try { + Date current = new Date(); + if (date.getTime() >= current.getTime()) { + redisTemplate.expireAt(key, date); + } + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + /** * 获取 Key 失效时间 * @@ -195,4 +246,5 @@ public class RedisUtil { } return 0L; } + } diff --git a/dc3/dependencies/maven/settings.xml b/dc3/dependencies/maven/settings.xml index 65dda95e7..de3ba918e 100644 --- a/dc3/dependencies/maven/settings.xml +++ b/dc3/dependencies/maven/settings.xml @@ -14,8 +14,7 @@ --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">