瀏覽代碼

新增批量获取Redis值工具方法;新增数字转日期工具方法;

woody 2 年之前
父節點
當前提交
6133747fbc

+ 40 - 0
framework-base/src/main/java/com/chelvc/framework/base/util/DateUtils.java

@@ -598,6 +598,26 @@ public final class DateUtils {
         return datetime == null ? null : converter.apply(DATE_NUMBER_FORMATTER.format(datetime));
         return datetime == null ? null : converter.apply(DATE_NUMBER_FORMATTER.format(datetime));
     }
     }
 
 
+    /**
+     * 将字符串形式数字日期转换成日期对象
+     *
+     * @param number 字符串形式数字日期
+     * @return 日期对象
+     */
+    public static Date number2date(String number) {
+        return StringUtils.isEmpty(number) ? null : convert(LocalDate.parse(number, DATE_NUMBER_FORMATTER));
+    }
+
+    /**
+     * 将数字形式日期转换成日期对象
+     *
+     * @param number 数字形式日期
+     * @return 日期对象
+     */
+    public static Date number2date(Integer number) {
+        return number == null ? null : convert(LocalDate.parse(String.valueOf(number), DATE_NUMBER_FORMATTER));
+    }
+
     /**
     /**
      * 将日期时间转换成cron表达式
      * 将日期时间转换成cron表达式
      *
      *
@@ -686,6 +706,26 @@ public final class DateUtils {
         return datetime == null ? null : converter.apply(DATETIME_NUMBER_FORMATTER.format(datetime));
         return datetime == null ? null : converter.apply(DATETIME_NUMBER_FORMATTER.format(datetime));
     }
     }
 
 
+    /**
+     * 将字符串形式数字日期时间转换成日期对象
+     *
+     * @param number 字符串形式数字日期时间
+     * @return 日期对象
+     */
+    public static Date number2datetime(String number) {
+        return StringUtils.isEmpty(number) ? null : convert(LocalDate.parse(number, DATETIME_NUMBER_FORMATTER));
+    }
+
+    /**
+     * 将数字形式日期时间转换成日期对象
+     *
+     * @param number 数字形式日期时间
+     * @return 日期对象
+     */
+    public static Date number2datetime(Integer number) {
+        return number == null ? null : convert(LocalDate.parse(String.valueOf(number), DATETIME_NUMBER_FORMATTER));
+    }
+
     /**
     /**
      * 获取当前日期在当周的位置
      * 获取当前日期在当周的位置
      *
      *

+ 38 - 22
framework-redis/src/main/java/com/chelvc/framework/redis/util/RedisUtils.java

@@ -250,28 +250,7 @@ public class RedisUtils implements ApplicationContextAware {
      * @return 锁名称/标识信息映射表
      * @return 锁名称/标识信息映射表
      */
      */
     public static Map<String, String> locks(@NonNull List<String> names) {
     public static Map<String, String> locks(@NonNull List<String> names) {
-        if (names.isEmpty()) {
-            return Collections.emptyMap();
-        }
-        byte[][] keys = names.stream().map(name -> name.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
-        return execute(connection -> {
-            List<byte[]> values = connection.mGet(keys);
-            if (CollectionUtils.isEmpty(values)) {
-                return Collections.emptyMap();
-            }
-            Map<String, String> locks = Maps.newHashMapWithExpectedSize(keys.length);
-            for (int i = 0; i < keys.length; i++) {
-                byte[] bytes = values.get(i);
-                if (bytes == null || bytes.length == 0) {
-                    continue;
-                }
-                String value = new String(bytes, StandardCharsets.UTF_8);
-                if (StringUtils.nonEmpty(value)) {
-                    locks.put(names.get(i), value);
-                }
-            }
-            return locks;
-        }, e -> Collections.emptyMap());
+        return get(names, key -> key, value -> new String(value, StandardCharsets.UTF_8));
     }
     }
 
 
     /**
     /**
@@ -572,6 +551,43 @@ public class RedisUtils implements ApplicationContextAware {
         return object;
         return object;
     }
     }
 
 
+    /**
+     * 批量获取Redis值
+     *
+     * @param keys         键名称列表
+     * @param keyAdapter   键适配器
+     * @param valueAdapter 值适配器
+     * @param <V>          值类型
+     * @return 锁名称/标识信息映射表
+     */
+    public static <V> Map<String, V> get(List<String> keys, @NonNull Function<String, String> keyAdapter,
+                                         @NonNull Function<byte[], V> valueAdapter) {
+        if (CollectionUtils.isEmpty(keys)) {
+            return Collections.emptyMap();
+        }
+        return execute(connection -> {
+            List<byte[]> values = connection.mGet(
+                    keys.stream().map(name -> name.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new)
+            );
+            if (CollectionUtils.isEmpty(values)) {
+                return Collections.emptyMap();
+            }
+            Map<String, V> mapping = Maps.newHashMapWithExpectedSize(keys.size());
+            for (int i = 0; i < keys.size(); i++) {
+                String key = keys.get(i);
+                byte[] bytes = values.get(i);
+                if (StringUtils.isEmpty(key) || bytes == null || bytes.length == 0) {
+                    continue;
+                }
+                V value = valueAdapter.apply(bytes);
+                if (StringUtils.nonEmpty(value)) {
+                    mapping.put(keyAdapter.apply(key), value);
+                }
+            }
+            return mapping;
+        }, e -> Collections.emptyMap());
+    }
+
     /**
     /**
      * 获取键值,如果键不存在则初始化
      * 获取键值,如果键不存在则初始化
      *
      *