From 29396864f25d80cab793a1b63569c20e51cfcf53 Mon Sep 17 00:00:00 2001 From: Andrii Shvaika Date: Tue, 10 May 2022 11:47:09 +0300 Subject: [PATCH] Edge cache --- .../dao/asset/AssetCacheEvictEvent.java | 3 -- .../server/dao/asset/AssetRedisCache.java | 16 +------ .../server/dao/asset/BaseAssetService.java | 1 - .../dao/attributes/AttributeRedisCache.java | 16 +------ .../server/dao/cache/TbRedisSerializer.java | 35 ++++++++++++++ .../server/dao/device/DeviceRedisCache.java | 16 +------ .../server/dao/edge/EdgeCacheEvictEvent.java | 30 ++++++++++++ .../server/dao/edge/EdgeCacheKey.java | 41 +++++++++++++++++ .../server/dao/edge/EdgeCaffeineCache.java | 35 ++++++++++++++ .../server/dao/edge/EdgeRedisCache.java | 39 ++++++++++++++++ .../server/dao/edge/EdgeServiceImpl.java | 46 +++++++++++++------ .../dao/entityview/EntityViewRedisCache.java | 16 +------ .../dao/relation/RelationRedisCache.java | 16 +------ .../dao/tenant/TenantProfileRedisCache.java | 16 +------ 14 files changed, 224 insertions(+), 102 deletions(-) create mode 100644 dao/src/main/java/org/thingsboard/server/dao/cache/TbRedisSerializer.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCacheEvictEvent.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCacheKey.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCaffeineCache.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/edge/EdgeRedisCache.java diff --git a/dao/src/main/java/org/thingsboard/server/dao/asset/AssetCacheEvictEvent.java b/dao/src/main/java/org/thingsboard/server/dao/asset/AssetCacheEvictEvent.java index faaf3945a0..ce81e047aa 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/asset/AssetCacheEvictEvent.java +++ b/dao/src/main/java/org/thingsboard/server/dao/asset/AssetCacheEvictEvent.java @@ -27,7 +27,4 @@ class AssetCacheEvictEvent { private final String newName; private final String oldName; - public AssetCacheEvictEvent(TenantId tenantId, String newName) { - this(tenantId, newName, null); - } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/asset/AssetRedisCache.java b/dao/src/main/java/org/thingsboard/server/dao/asset/AssetRedisCache.java index 864d5cf1e5..996756c7eb 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/asset/AssetRedisCache.java +++ b/dao/src/main/java/org/thingsboard/server/dao/asset/AssetRedisCache.java @@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.CacheConstants; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.dao.cache.RedisTbTransactionalCache; +import org.thingsboard.server.dao.cache.TbRedisSerializer; import org.thingsboard.server.dao.device.DeviceCacheKey; @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") @@ -33,19 +34,6 @@ import org.thingsboard.server.dao.device.DeviceCacheKey; public class AssetRedisCache extends RedisTbTransactionalCache { public AssetRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { - super(CacheConstants.ASSET_CACHE, cacheSpecsMap, connectionFactory, configuration, new RedisSerializer<>() { - - private final RedisSerializer java = RedisSerializer.java(); - - @Override - public byte[] serialize(Asset attributeKvEntry) throws SerializationException { - return java.serialize(attributeKvEntry); - } - - @Override - public Asset deserialize(byte[] bytes) throws SerializationException { - return (Asset) java.deserialize(bytes); - } - }); + super(CacheConstants.ASSET_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbRedisSerializer<>()); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java b/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java index a7b408b47e..25d4029a0c 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/asset/BaseAssetService.java @@ -116,7 +116,6 @@ public class BaseAssetService extends AbstractCachedEntityService assetDao.findAssetsByTenantIdAndName(tenantId.getId(), name) .orElse(null), true); - } @Transactional(propagation = Propagation.SUPPORTS) diff --git a/dao/src/main/java/org/thingsboard/server/dao/attributes/AttributeRedisCache.java b/dao/src/main/java/org/thingsboard/server/dao/attributes/AttributeRedisCache.java index 8d2b8deb1f..3fc4a98075 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/attributes/AttributeRedisCache.java +++ b/dao/src/main/java/org/thingsboard/server/dao/attributes/AttributeRedisCache.java @@ -27,25 +27,13 @@ import org.thingsboard.server.cache.TBRedisCacheConfiguration; import org.thingsboard.server.common.data.CacheConstants; import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.dao.cache.RedisTbTransactionalCache; +import org.thingsboard.server.dao.cache.TbRedisSerializer; @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") @Service("AttributeCache") public class AttributeRedisCache extends RedisTbTransactionalCache { public AttributeRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { - super(CacheConstants.ATTRIBUTES_CACHE, cacheSpecsMap, connectionFactory, configuration, new RedisSerializer<>() { - - private final RedisSerializer java = RedisSerializer.java(); - - @Override - public byte[] serialize(AttributeKvEntry attributeKvEntry) throws SerializationException { - return java.serialize(attributeKvEntry); - } - - @Override - public AttributeKvEntry deserialize(byte[] bytes) throws SerializationException { - return (AttributeKvEntry) java.deserialize(bytes); - } - }); + super(CacheConstants.ATTRIBUTES_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbRedisSerializer<>()); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/cache/TbRedisSerializer.java b/dao/src/main/java/org/thingsboard/server/dao/cache/TbRedisSerializer.java new file mode 100644 index 0000000000..cf0fac914a --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/cache/TbRedisSerializer.java @@ -0,0 +1,35 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.cache; + +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; +import org.thingsboard.server.common.data.edge.Edge; + +public class TbRedisSerializer implements RedisSerializer { + + private final RedisSerializer java = RedisSerializer.java(); + + @Override + public byte[] serialize(T t) throws SerializationException { + return java.serialize(t); + } + + @Override + public T deserialize(byte[] bytes) throws SerializationException { + return (T) java.deserialize(bytes); + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceRedisCache.java b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceRedisCache.java index a3c63b49f9..72a7273ed5 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/device/DeviceRedisCache.java +++ b/dao/src/main/java/org/thingsboard/server/dao/device/DeviceRedisCache.java @@ -25,25 +25,13 @@ import org.thingsboard.server.cache.TBRedisCacheConfiguration; import org.thingsboard.server.common.data.CacheConstants; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.dao.cache.RedisTbTransactionalCache; +import org.thingsboard.server.dao.cache.TbRedisSerializer; @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") @Service("DeviceCache") public class DeviceRedisCache extends RedisTbTransactionalCache { public DeviceRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { - super(CacheConstants.DEVICE_CACHE, cacheSpecsMap, connectionFactory, configuration, new RedisSerializer<>() { - - private final RedisSerializer java = RedisSerializer.java(); - - @Override - public byte[] serialize(Device attributeKvEntry) throws SerializationException { - return java.serialize(attributeKvEntry); - } - - @Override - public Device deserialize(byte[] bytes) throws SerializationException { - return (Device) java.deserialize(bytes); - } - }); + super(CacheConstants.DEVICE_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbRedisSerializer<>()); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCacheEvictEvent.java b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCacheEvictEvent.java new file mode 100644 index 0000000000..44f4cdb25d --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCacheEvictEvent.java @@ -0,0 +1,30 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.edge; + +import lombok.Data; +import lombok.RequiredArgsConstructor; +import org.thingsboard.server.common.data.id.TenantId; + +@Data +@RequiredArgsConstructor +class EdgeCacheEvictEvent { + + private final TenantId tenantId; + private final String newName; + private final String oldName; + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCacheKey.java b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCacheKey.java new file mode 100644 index 0000000000..152528f936 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCacheKey.java @@ -0,0 +1,41 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.edge; + +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.thingsboard.server.cache.CacheKeyUtil; +import org.thingsboard.server.common.data.id.TenantId; + +import java.io.Serializable; + +@Getter +@EqualsAndHashCode +@RequiredArgsConstructor +@Builder +public class EdgeCacheKey implements Serializable { + + private final TenantId tenantId; + private final String name; + + @Override + public String toString() { + return CacheKeyUtil.toString(tenantId, name); + } + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCaffeineCache.java b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCaffeineCache.java new file mode 100644 index 0000000000..c4cff90b7f --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeCaffeineCache.java @@ -0,0 +1,35 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.edge; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cache.CacheManager; +import org.springframework.stereotype.Service; +import org.thingsboard.server.common.data.CacheConstants; +import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.edge.Edge; +import org.thingsboard.server.dao.asset.AssetCacheKey; +import org.thingsboard.server.dao.cache.CaffeineTbTransactionalCache; + +@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true) +@Service("EdgeCache") +public class EdgeCaffeineCache extends CaffeineTbTransactionalCache { + + public EdgeCaffeineCache(CacheManager cacheManager) { + super(cacheManager, CacheConstants.EDGE_CACHE); + } + +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeRedisCache.java b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeRedisCache.java new file mode 100644 index 0000000000..9a18f9d855 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeRedisCache.java @@ -0,0 +1,39 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.edge; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; +import org.springframework.stereotype.Service; +import org.thingsboard.server.cache.CacheSpecsMap; +import org.thingsboard.server.cache.TBRedisCacheConfiguration; +import org.thingsboard.server.common.data.CacheConstants; +import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.edge.Edge; +import org.thingsboard.server.dao.asset.AssetCacheKey; +import org.thingsboard.server.dao.cache.RedisTbTransactionalCache; +import org.thingsboard.server.dao.cache.TbRedisSerializer; + +@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") +@Service("EdgeCache") +public class EdgeRedisCache extends RedisTbTransactionalCache { + + public EdgeRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { + super(CacheConstants.EDGE_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbRedisSerializer<>()); + } +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java index 80d3a1bb03..2e386c4c31 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/edge/EdgeServiceImpl.java @@ -25,11 +25,14 @@ import com.google.common.util.concurrent.MoreExecutors; import lombok.extern.slf4j.Slf4j; import org.hibernate.exception.ConstraintViolationException; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.event.TransactionalEventListener; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeInfo; @@ -47,8 +50,7 @@ import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.EntitySearchDirection; import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.common.data.rule.RuleChainConnectionInfo; -import org.thingsboard.server.dao.cache.EntitiesCacheManager; -import org.thingsboard.server.dao.entity.AbstractEntityService; +import org.thingsboard.server.dao.entity.AbstractCachedEntityService; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.relation.RelationService; import org.thingsboard.server.dao.rule.RuleChainService; @@ -74,7 +76,7 @@ import static org.thingsboard.server.dao.service.Validator.validateString; @Service @Slf4j -public class EdgeServiceImpl extends AbstractEntityService implements EdgeService { +public class EdgeServiceImpl extends AbstractCachedEntityService implements EdgeService { public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; @@ -90,9 +92,6 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic @Autowired private UserService userService; - @Autowired - private EntitiesCacheManager cacheManager; - @Autowired private RuleChainService ruleChainService; @@ -102,6 +101,17 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic @Autowired private DataValidator edgeValidator; + @TransactionalEventListener(classes = EdgeCacheEvictEvent.class) + @Override + public void handleEvictEvent(EdgeCacheEvictEvent event) { + List keys = new ArrayList<>(2); + keys.add(new EdgeCacheKey(event.getTenantId(), event.getNewName())); + if (StringUtils.isNotEmpty(event.getOldName()) && !event.getOldName().equals(event.getNewName())) { + keys.add(new EdgeCacheKey(event.getTenantId(), event.getOldName())); + } + cache.evict(keys); + } + @Override public Edge findEdgeById(TenantId tenantId, EdgeId edgeId) { log.trace("Executing findEdgeById [{}]", edgeId); @@ -128,8 +138,9 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic public Edge findEdgeByTenantIdAndName(TenantId tenantId, String name) { log.trace("Executing findEdgeByTenantIdAndName [{}][{}]", tenantId, name); validateId(tenantId, INCORRECT_TENANT_ID + tenantId); - Optional edgeOpt = edgeDao.findEdgeByTenantIdAndName(tenantId.getId(), name); - return edgeOpt.orElse(null); + return cache.getAndPutInTransaction(new EdgeCacheKey(tenantId, name), + () -> edgeDao.findEdgeByTenantIdAndName(tenantId.getId(), name) + .orElse(null), true); } @Override @@ -139,13 +150,16 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic return edgeDao.findByRoutingKey(tenantId.getId(), routingKey); } - @CacheEvict(cacheNames = EDGE_CACHE, key = "{#edge.tenantId, #edge.name}") + @Transactional(propagation = Propagation.SUPPORTS) @Override public Edge saveEdge(Edge edge) { log.trace("Executing saveEdge [{}]", edge); - edgeValidator.validate(edge, Edge::getTenantId); + Edge oldEdge = edgeValidator.validate(edge, Edge::getTenantId); + EdgeCacheEvictEvent evictEvent = new EdgeCacheEvictEvent(edge.getTenantId(), edge.getName(), oldEdge != null ? oldEdge.getName() : null); try { - return edgeDao.save(edge.getTenantId(), edge); + var savedEdge = edgeDao.save(edge.getTenantId(), edge); + publishEvictEvent(evictEvent); + return savedEdge; } catch (Exception t) { ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); if (e != null && e.getConstraintName() != null @@ -157,6 +171,7 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic } } + @Transactional(propagation = Propagation.SUPPORTS) @Override public Edge assignEdgeToCustomer(TenantId tenantId, EdgeId edgeId, CustomerId customerId) { log.trace("[{}] Executing assignEdgeToCustomer [{}][{}]", tenantId, edgeId, customerId); @@ -165,6 +180,7 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic return saveEdge(edge); } + @Transactional(propagation = Propagation.SUPPORTS) @Override public Edge unassignEdgeFromCustomer(TenantId tenantId, EdgeId edgeId) { log.trace("[{}] Executing unassignEdgeFromCustomer [{}]", tenantId, edgeId); @@ -173,6 +189,7 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic return saveEdge(edge); } + @Transactional(propagation = Propagation.SUPPORTS) @Override public void deleteEdge(TenantId tenantId, EdgeId edgeId) { log.trace("Executing deleteEdge [{}]", edgeId); @@ -182,9 +199,9 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic deleteEntityRelations(tenantId, edgeId); - cacheManager.removeEdgeFromCacheByName(edge.getTenantId(), edge.getName()); - edgeDao.removeById(tenantId, edgeId.getId()); + + publishEvictEvent(new EdgeCacheEvictEvent(edge.getTenantId(), edge.getName(), null)); } @Override @@ -284,6 +301,7 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic customerId.getId(), toUUIDs(edgeIds)); } + @Transactional(propagation = Propagation.SUPPORTS) @Override public void unassignCustomerEdges(TenantId tenantId, CustomerId customerId) { log.trace("Executing unassignCustomerEdges, tenantId [{}], customerId [{}]", tenantId, customerId); diff --git a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewRedisCache.java b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewRedisCache.java index 21da2337f9..9fcc8383f9 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewRedisCache.java +++ b/dao/src/main/java/org/thingsboard/server/dao/entityview/EntityViewRedisCache.java @@ -26,25 +26,13 @@ import org.thingsboard.server.common.data.CacheConstants; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.dao.asset.AssetCacheKey; import org.thingsboard.server.dao.cache.RedisTbTransactionalCache; +import org.thingsboard.server.dao.cache.TbRedisSerializer; @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") @Service("EntityViewCache") public class EntityViewRedisCache extends RedisTbTransactionalCache { public EntityViewRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { - super(CacheConstants.ENTITY_VIEW_CACHE, cacheSpecsMap, connectionFactory, configuration, new RedisSerializer<>() { - - private final RedisSerializer java = RedisSerializer.java(); - - @Override - public byte[] serialize(EntityViewCacheValue attributeKvEntry) throws SerializationException { - return java.serialize(attributeKvEntry); - } - - @Override - public EntityViewCacheValue deserialize(byte[] bytes) throws SerializationException { - return (EntityViewCacheValue) java.deserialize(bytes); - } - }); + super(CacheConstants.ENTITY_VIEW_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbRedisSerializer<>()); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/relation/RelationRedisCache.java b/dao/src/main/java/org/thingsboard/server/dao/relation/RelationRedisCache.java index 0e0dc261fb..e433379474 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/relation/RelationRedisCache.java +++ b/dao/src/main/java/org/thingsboard/server/dao/relation/RelationRedisCache.java @@ -26,25 +26,13 @@ import org.thingsboard.server.common.data.CacheConstants; import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.dao.attributes.AttributeCacheKey; import org.thingsboard.server.dao.cache.RedisTbTransactionalCache; +import org.thingsboard.server.dao.cache.TbRedisSerializer; @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") @Service("RelationCache") public class RelationRedisCache extends RedisTbTransactionalCache { public RelationRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { - super(CacheConstants.RELATIONS_CACHE, cacheSpecsMap, connectionFactory, configuration, new RedisSerializer<>() { - - private final RedisSerializer java = RedisSerializer.java(); - - @Override - public byte[] serialize(RelationCacheValue attributeKvEntry) throws SerializationException { - return java.serialize(attributeKvEntry); - } - - @Override - public RelationCacheValue deserialize(byte[] bytes) throws SerializationException { - return (RelationCacheValue) java.deserialize(bytes); - } - }); + super(CacheConstants.RELATIONS_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbRedisSerializer<>()); } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/tenant/TenantProfileRedisCache.java b/dao/src/main/java/org/thingsboard/server/dao/tenant/TenantProfileRedisCache.java index 6ed750b480..e6cc0a254c 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/tenant/TenantProfileRedisCache.java +++ b/dao/src/main/java/org/thingsboard/server/dao/tenant/TenantProfileRedisCache.java @@ -27,25 +27,13 @@ import org.thingsboard.server.common.data.TenantProfile; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.dao.asset.AssetCacheKey; import org.thingsboard.server.dao.cache.RedisTbTransactionalCache; +import org.thingsboard.server.dao.cache.TbRedisSerializer; @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") @Service("TenantProfileCache") public class TenantProfileRedisCache extends RedisTbTransactionalCache { public TenantProfileRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { - super(CacheConstants.TENANT_PROFILE_CACHE, cacheSpecsMap, connectionFactory, configuration, new RedisSerializer<>() { - - private final RedisSerializer java = RedisSerializer.java(); - - @Override - public byte[] serialize(TenantProfile attributeKvEntry) throws SerializationException { - return java.serialize(attributeKvEntry); - } - - @Override - public TenantProfile deserialize(byte[] bytes) throws SerializationException { - return (TenantProfile) java.deserialize(bytes); - } - }); + super(CacheConstants.TENANT_PROFILE_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbRedisSerializer<>()); } }