diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java index 10ffe750f5..b8b92e0f26 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -18,6 +18,8 @@ package io.entgra.device.mgt.core.device.mgt.core.metadata.mgt; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; @@ -42,6 +44,7 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.TimeUnit; public class DeviceStatusManagementServiceImpl implements DeviceStatusManagementService { @@ -54,6 +57,10 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement private final Gson gson; + private static final Cache> cache = CacheBuilder.newBuilder() + .expireAfterWrite(5, TimeUnit.HOURS) + .build(); + private DeviceStatusManagementServiceImpl() { this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); this.gson = new Gson(); @@ -282,27 +289,29 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement log.error(msg); throw new IllegalArgumentException(msg); } - MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); - Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - String metaValue; - if (metadata != null) { - metaValue = metadata.getMetaValue(); - } else { - List statusList = getDefaultDeviceStatus(); - metaValue = gson.toJson(statusList); - addDefaultDeviceStatusFilters(tenantId); + List allowedDeviceStatus = cache.getIfPresent(Integer.toString(tenantId)); + if (allowedDeviceStatus == null) { + MetadataManagementService metadataManagementService = new MetadataManagementServiceImpl(); + Metadata metadata = metadataManagementService.retrieveMetadata(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + String metaValue; + if (metadata != null) { + metaValue = metadata.getMetaValue(); + } else { + List statusList = getDefaultDeviceStatus(); + metaValue = gson.toJson(statusList); + addDefaultDeviceStatusFilters(tenantId); + } + Type listType = new TypeToken>() { + }.getType(); + allowedDeviceStatus = gson.fromJson(metaValue, listType); + cache.put(Integer.toString(tenantId), allowedDeviceStatus); } - Type listType = new TypeToken>() { - }.getType(); - List statusList = gson.fromJson(metaValue, listType); - - for (AllowedDeviceStatus status : statusList) { + for (AllowedDeviceStatus status : allowedDeviceStatus) { if (deviceType.equalsIgnoreCase(status.getType())) { List allowedStatus = status.getStatus(); return allowedStatus.contains(deviceStatus); } } - return false; // Device type not found in metadata }