Remove device from cache in case null value cached in the distributed redis

This commit is contained in:
Volodymyr Babak 2021-02-04 09:02:26 +02:00 committed by Andrew Shvayka
parent 0f3d320933
commit 5391d296f3

View File

@ -203,6 +203,8 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) {
// remove device from cache in case null value cached in the distributed redis.
removeDeviceFromCache(device.getTenantId(), device.getName());
throw new DataValidationException("Device with such name already exists!");
} else {
throw t;
@ -281,15 +283,19 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
}
deleteEntityRelations(tenantId, deviceId);
List<Object> list = new ArrayList<>();
list.add(device.getTenantId());
list.add(device.getName());
Cache cache = cacheManager.getCache(DEVICE_CACHE);
cache.evict(list);
removeDeviceFromCache(tenantId, device.getName());
deviceDao.removeById(tenantId, deviceId.getId());
}
private void removeDeviceFromCache(TenantId tenantId, String name) {
List<Object> list = new ArrayList<>();
list.add(tenantId);
list.add(name);
Cache cache = cacheManager.getCache(DEVICE_CACHE);
cache.evict(list);
}
@Override
public PageData<Device> findDevicesByTenantId(TenantId tenantId, PageLink pageLink) {
log.trace("Executing findDevicesByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink);