Commit ab76d81c6fb6716148f6b40608c8918b9e76c36c

Authored by Andrii Shvaika
1 parent 29fd4fb0

Imrpvements to the cache

... ... @@ -20,10 +20,14 @@ import lombok.EqualsAndHashCode;
20 20 import lombok.Getter;
21 21 import org.thingsboard.server.common.data.id.EntityId;
22 22
  23 +import java.io.Serializable;
  24 +
23 25 @EqualsAndHashCode
24 26 @Getter
25 27 @AllArgsConstructor
26   -public class AttributeCacheKey {
  28 +public class AttributeCacheKey implements Serializable {
  29 + private static final long serialVersionUID = 2013369077925351881L;
  30 +
27 31 private final String scope;
28 32 private final EntityId entityId;
29 33 private final String key;
... ...
... ... @@ -37,10 +37,12 @@ import org.thingsboard.server.dao.service.Validator;
37 37 import java.util.ArrayList;
38 38 import java.util.Collection;
39 39 import java.util.HashMap;
  40 +import java.util.HashSet;
40 41 import java.util.List;
41 42 import java.util.Map;
42 43 import java.util.Objects;
43 44 import java.util.Optional;
  45 +import java.util.Set;
44 46 import java.util.stream.Collectors;
45 47
46 48 import static org.thingsboard.server.common.data.CacheConstants.ATTRIBUTES_CACHE;
... ... @@ -106,13 +108,11 @@ public class CachedAttributesService implements AttributesService {
106 108 return Futures.immediateFuture(cachedAttributes);
107 109 }
108 110
109   - ArrayList<String> notFoundAttributeKeys = new ArrayList<>(attributeKeys);
  111 + Set<String> notFoundAttributeKeys = new HashSet<>(attributeKeys);
110 112 notFoundAttributeKeys.removeAll(wrappedCachedAttributes.keySet());
111 113
112 114 ListenableFuture<List<AttributeKvEntry>> result = attributesDao.find(tenantId, entityId, scope, notFoundAttributeKeys);
113   - return Futures.transform(result, foundInDbAttributes -> {
114   - return mergeDbAndCacheAttributes(entityId, scope, cachedAttributes, notFoundAttributeKeys, foundInDbAttributes);
115   - }, MoreExecutors.directExecutor());
  115 + return Futures.transform(result, foundInDbAttributes -> mergeDbAndCacheAttributes(entityId, scope, cachedAttributes, notFoundAttributeKeys, foundInDbAttributes), MoreExecutors.directExecutor());
116 116
117 117 }
118 118
... ... @@ -130,7 +130,7 @@ public class CachedAttributesService implements AttributesService {
130 130 return cachedAttributes;
131 131 }
132 132
133   - private List<AttributeKvEntry> mergeDbAndCacheAttributes(EntityId entityId, String scope, List<AttributeKvEntry> cachedAttributes, ArrayList<String> notFoundAttributeKeys, List<AttributeKvEntry> foundInDbAttributes) {
  133 + private List<AttributeKvEntry> mergeDbAndCacheAttributes(EntityId entityId, String scope, List<AttributeKvEntry> cachedAttributes, Set<String> notFoundAttributeKeys, List<AttributeKvEntry> foundInDbAttributes) {
134 134 for (AttributeKvEntry foundInDbAttribute : foundInDbAttributes) {
135 135 AttributeCacheKey attributeCacheKey = new AttributeCacheKey(scope, entityId, foundInDbAttribute.getKey());
136 136 attributesCache.put(attributeCacheKey, foundInDbAttribute);
... ...