added firmware info cache

This commit is contained in:
YevhenBondarenko 2021-04-23 17:43:03 +03:00
parent 3fbbfea331
commit b2ba2025de
2 changed files with 20 additions and 6 deletions

View File

@ -22,7 +22,6 @@ import org.thingsboard.rule.engine.api.RuleEngineTelemetryService;
import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.Firmware;
import org.thingsboard.server.common.data.FirmwareInfo; import org.thingsboard.server.common.data.FirmwareInfo;
import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.FirmwareId; import org.thingsboard.server.common.data.id.FirmwareId;
@ -35,7 +34,6 @@ import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry; import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.msg.queue.TbCallback;
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
import org.thingsboard.server.dao.device.DeviceProfileService; import org.thingsboard.server.dao.device.DeviceProfileService;
import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.device.DeviceService;
@ -155,7 +153,7 @@ public class DefaultFirmwareStateService implements FirmwareStateService {
} }
if (targetFirmwareId.equals(currentFirmwareId)) { if (targetFirmwareId.equals(currentFirmwareId)) {
update(device, firmwareService.findFirmwareById(device.getTenantId(), targetFirmwareId), ts); update(device, firmwareService.findFirmwareInfoById(device.getTenantId(), targetFirmwareId), ts);
isSuccess = true; isSuccess = true;
} else { } else {
log.warn("[{}] [{}] Can`t update firmware for the device, target firmwareId: [{}], current firmwareId: [{}]!", tenantId, deviceId, targetFirmwareId, currentFirmwareId); log.warn("[{}] [{}] Can`t update firmware for the device, target firmwareId: [{}], current firmwareId: [{}]!", tenantId, deviceId, targetFirmwareId, currentFirmwareId);

View File

@ -22,6 +22,7 @@ import org.apache.commons.lang3.StringUtils;
import org.hibernate.exception.ConstraintViolationException; import org.hibernate.exception.ConstraintViolationException;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.Firmware; import org.thingsboard.server.common.data.Firmware;
import org.thingsboard.server.common.data.FirmwareInfo; import org.thingsboard.server.common.data.FirmwareInfo;
@ -37,6 +38,7 @@ import org.thingsboard.server.dao.tenant.TenantDao;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import static org.thingsboard.server.common.data.CacheConstants.FIRMWARE_CACHE; import static org.thingsboard.server.common.data.CacheConstants.FIRMWARE_CACHE;
@ -68,7 +70,9 @@ public class BaseFirmwareService implements FirmwareService {
try { try {
FirmwareId firmwareId = firmwareInfo.getId(); FirmwareId firmwareId = firmwareInfo.getId();
if (firmwareId != null) { if (firmwareId != null) {
cacheManager.getCache(FIRMWARE_CACHE).evict(firmwareId.toString()); Cache cache = cacheManager.getCache(FIRMWARE_CACHE);
cache.evict(toFirmwareInfoKey(firmwareId));
cache.evict(toFirmwareDataKey(firmwareId));
} }
return firmwareInfoDao.save(firmwareInfo.getTenantId(), firmwareInfo); return firmwareInfoDao.save(firmwareInfo.getTenantId(), firmwareInfo);
} catch (Exception t) { } catch (Exception t) {
@ -88,7 +92,9 @@ public class BaseFirmwareService implements FirmwareService {
try { try {
FirmwareId firmwareId = firmware.getId(); FirmwareId firmwareId = firmware.getId();
if (firmwareId != null) { if (firmwareId != null) {
cacheManager.getCache(FIRMWARE_CACHE).evict(firmwareId.toString()); Cache cache = cacheManager.getCache(FIRMWARE_CACHE);
cache.evict(toFirmwareInfoKey(firmwareId));
cache.evict(toFirmwareDataKey(firmwareId));
} }
return firmwareDao.save(firmware.getTenantId(), firmware); return firmwareDao.save(firmware.getTenantId(), firmware);
} catch (Exception t) { } catch (Exception t) {
@ -109,6 +115,7 @@ public class BaseFirmwareService implements FirmwareService {
} }
@Override @Override
@Cacheable(cacheNames = FIRMWARE_CACHE, key = "{#firmwareId}")
public FirmwareInfo findFirmwareInfoById(TenantId tenantId, FirmwareId firmwareId) { public FirmwareInfo findFirmwareInfoById(TenantId tenantId, FirmwareId firmwareId) {
log.trace("Executing findFirmwareInfoById [{}]", firmwareId); log.trace("Executing findFirmwareInfoById [{}]", firmwareId);
validateId(firmwareId, INCORRECT_FIRMWARE_ID + firmwareId); validateId(firmwareId, INCORRECT_FIRMWARE_ID + firmwareId);
@ -137,7 +144,8 @@ public class BaseFirmwareService implements FirmwareService {
validateId(firmwareId, INCORRECT_FIRMWARE_ID + firmwareId); validateId(firmwareId, INCORRECT_FIRMWARE_ID + firmwareId);
try { try {
Cache cache = cacheManager.getCache(FIRMWARE_CACHE); Cache cache = cacheManager.getCache(FIRMWARE_CACHE);
cache.evict(Collections.singletonList(firmwareId)); cache.evict(toFirmwareInfoKey(firmwareId));
cache.evict(toFirmwareDataKey(firmwareId));
firmwareDao.removeById(tenantId, firmwareId.getId()); firmwareDao.removeById(tenantId, firmwareId.getId());
} catch (Exception t) { } catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
@ -316,4 +324,12 @@ public class BaseFirmwareService implements FirmwareService {
return Optional.empty(); return Optional.empty();
} }
} }
private static List<FirmwareId> toFirmwareInfoKey(FirmwareId firmwareId) {
return Collections.singletonList(firmwareId);
}
private static String toFirmwareDataKey(FirmwareId firmwareId) {
return firmwareId.toString();
}
} }