Merge pull request #787 from harshanL/master

Added null checks for device cache in-case cache is disabled.
4.x.x
Harshan Liyanage 8 years ago committed by GitHub
commit 1ba90eaa7b

@ -55,29 +55,35 @@ public class DeviceCacheManagerImpl implements DeviceCacheManager {
@Override @Override
public void addDeviceToCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) { public void addDeviceToCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) {
Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache(); Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache();
DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); if (lCache != null) {
if (lCache.containsKey(cacheKey)) { DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId);
this.updateDeviceInCache(deviceIdentifier, device, tenantId); if (lCache.containsKey(cacheKey)) {
} else { this.updateDeviceInCache(deviceIdentifier, device, tenantId);
lCache.put(cacheKey, device); } else {
lCache.put(cacheKey, device);
}
} }
} }
@Override @Override
public void removeDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId) { public void removeDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId) {
Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache(); Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache();
DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); if (lCache != null) {
if (lCache.containsKey(cacheKey)) { DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId);
lCache.remove(cacheKey); if (lCache.containsKey(cacheKey)) {
lCache.remove(cacheKey);
}
} }
} }
@Override @Override
public void removeDevicesFromCache(List<DeviceCacheKey> deviceList) { public void removeDevicesFromCache(List<DeviceCacheKey> deviceList) {
Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache(); Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache();
for (DeviceCacheKey cacheKey : deviceList) { if (lCache != null) {
if (lCache.containsKey(cacheKey)) { for (DeviceCacheKey cacheKey : deviceList) {
lCache.remove(cacheKey); if (lCache.containsKey(cacheKey)) {
lCache.remove(cacheKey);
}
} }
} }
} }
@ -85,16 +91,21 @@ public class DeviceCacheManagerImpl implements DeviceCacheManager {
@Override @Override
public void updateDeviceInCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) { public void updateDeviceInCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) {
Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache(); Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache();
DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); if (lCache != null) {
if (lCache.containsKey(cacheKey)) { DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId);
lCache.replace(cacheKey, device); if (lCache.containsKey(cacheKey)) {
lCache.replace(cacheKey, device);
}
} }
} }
@Override @Override
public Device getDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId) { public Device getDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId) {
Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache(); Cache<DeviceCacheKey, Device> lCache = DeviceManagerUtil.getDeviceCache();
return lCache.get(getCacheKey(deviceIdentifier, tenantId)); if (lCache != null) {
return lCache.get(getCacheKey(deviceIdentifier, tenantId));
}
return null;
} }

Loading…
Cancel
Save