Commit c811d67724e03b874b5f07e40d80e05c52b993c0

Authored by xp.Huang
1 parent 1615bcb9

fix: 修复在redis缓存下集合为空数据组的情况

1 1 package org.thingsboard.server.common.data.yunteng.core.cache;
  2 +
2 3 import org.springframework.cache.Cache;
3 4 import org.springframework.cache.CacheManager;
4 5 import org.springframework.stereotype.Component;
5 6 import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
  7 +
  8 +import java.util.Collection;
6 9 import java.util.Objects;
7 10 import java.util.Optional;
8 11
... ... @@ -49,17 +52,25 @@ public class CacheUtils {
49 52 }
50 53 }
51 54
52   - @SuppressWarnings("unchecked")
53 55 public <T> Optional<T> get(String key) {
54 56 return Optional.ofNullable(cacheManager.getCache(COMMON_STORE_AREA))
55 57 .map(cache -> Objects.requireNonNull(cache).get(key))
56   - .map(v -> (T) v.get());
  58 + .map(this::getValue);
57 59 }
58 60
59   - @SuppressWarnings("unchecked")
60 61 public <T> Optional<T> get(String cacheName, String key) {
61 62 return Optional.ofNullable(cacheManager.getCache(cacheName))
62 63 .map(cache -> Objects.requireNonNull(cache).get(key))
63   - .map(v -> (T) v.get());
  64 + .map(this::getValue);
  65 + }
  66 +
  67 + private <T> T getValue(Cache.ValueWrapper v) {
  68 + if (v.get() instanceof Collection) {
  69 + if (((Collection<?>) Objects.requireNonNull(v.get())).size()
  70 + == FastIotConstants.MagicNumber.ZERO) {
  71 + return null;
  72 + }
  73 + }
  74 + return (T) v.get();
64 75 }
65 76 }
... ...