Imrpvements to the cache

This commit is contained in:
Andrii Shvaika 2021-02-25 11:10:59 +02:00
parent 29fd4fb02c
commit ab76d81c6f
2 changed files with 10 additions and 6 deletions

View File

@ -20,10 +20,14 @@ import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import java.io.Serializable;
@EqualsAndHashCode @EqualsAndHashCode
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public class AttributeCacheKey { public class AttributeCacheKey implements Serializable {
private static final long serialVersionUID = 2013369077925351881L;
private final String scope; private final String scope;
private final EntityId entityId; private final EntityId entityId;
private final String key; private final String key;

View File

@ -37,10 +37,12 @@ import org.thingsboard.server.dao.service.Validator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.thingsboard.server.common.data.CacheConstants.ATTRIBUTES_CACHE; import static org.thingsboard.server.common.data.CacheConstants.ATTRIBUTES_CACHE;
@ -106,13 +108,11 @@ public class CachedAttributesService implements AttributesService {
return Futures.immediateFuture(cachedAttributes); return Futures.immediateFuture(cachedAttributes);
} }
ArrayList<String> notFoundAttributeKeys = new ArrayList<>(attributeKeys); Set<String> notFoundAttributeKeys = new HashSet<>(attributeKeys);
notFoundAttributeKeys.removeAll(wrappedCachedAttributes.keySet()); notFoundAttributeKeys.removeAll(wrappedCachedAttributes.keySet());
ListenableFuture<List<AttributeKvEntry>> result = attributesDao.find(tenantId, entityId, scope, notFoundAttributeKeys); ListenableFuture<List<AttributeKvEntry>> result = attributesDao.find(tenantId, entityId, scope, notFoundAttributeKeys);
return Futures.transform(result, foundInDbAttributes -> { return Futures.transform(result, foundInDbAttributes -> mergeDbAndCacheAttributes(entityId, scope, cachedAttributes, notFoundAttributeKeys, foundInDbAttributes), MoreExecutors.directExecutor());
return mergeDbAndCacheAttributes(entityId, scope, cachedAttributes, notFoundAttributeKeys, foundInDbAttributes);
}, MoreExecutors.directExecutor());
} }
@ -130,7 +130,7 @@ public class CachedAttributesService implements AttributesService {
return cachedAttributes; return cachedAttributes;
} }
private List<AttributeKvEntry> mergeDbAndCacheAttributes(EntityId entityId, String scope, List<AttributeKvEntry> cachedAttributes, ArrayList<String> notFoundAttributeKeys, List<AttributeKvEntry> foundInDbAttributes) { private List<AttributeKvEntry> mergeDbAndCacheAttributes(EntityId entityId, String scope, List<AttributeKvEntry> cachedAttributes, Set<String> notFoundAttributeKeys, List<AttributeKvEntry> foundInDbAttributes) {
for (AttributeKvEntry foundInDbAttribute : foundInDbAttributes) { for (AttributeKvEntry foundInDbAttribute : foundInDbAttributes) {
AttributeCacheKey attributeCacheKey = new AttributeCacheKey(scope, entityId, foundInDbAttribute.getKey()); AttributeCacheKey attributeCacheKey = new AttributeCacheKey(scope, entityId, foundInDbAttribute.getKey());
attributesCache.put(attributeCacheKey, foundInDbAttribute); attributesCache.put(attributeCacheKey, foundInDbAttribute);