From d0df0c2e33034efee4a3a0705c92b09dbed5e291 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 6 Nov 2022 10:01:50 -0800 Subject: Handle missing cache --- .../org/traccar/session/cache/CacheManager.java | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java index 8f2e7ba93..dc7382223 100644 --- a/src/main/java/org/traccar/session/cache/CacheManager.java +++ b/src/main/java/org/traccar/session/cache/CacheManager.java @@ -15,6 +15,8 @@ */ package org.traccar.session.cache; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.traccar.broadcast.BroadcastInterface; import org.traccar.broadcast.BroadcastService; import org.traccar.config.Config; @@ -42,6 +44,7 @@ import javax.inject.Inject; import javax.inject.Singleton; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; @@ -57,6 +60,7 @@ import java.util.stream.Collectors; @Singleton public class CacheManager implements BroadcastInterface { + private static final Logger LOGGER = LoggerFactory.getLogger(CacheManager.class); private static final int GROUP_DEPTH_LIMIT = 3; private static final Collection> CLASSES = Arrays.asList( Attribute.class, Driver.class, Geofence.class, Maintenance.class, Notification.class); @@ -102,13 +106,19 @@ public class CacheManager implements BroadcastInterface { public List getDeviceObjects(long deviceId, Class clazz) { try { lock.readLock().lock(); - return deviceLinks.get(deviceId).get(clazz).stream() - .map(id -> { - var cacheValue = deviceCache.get(new CacheKey(clazz, id)); - return cacheValue != null ? cacheValue.getValue() : null; - }) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + var links = deviceLinks.get(deviceId); + if (links != null) { + return links.getOrDefault(clazz, new LinkedHashSet<>()).stream() + .map(id -> { + var cacheValue = deviceCache.get(new CacheKey(clazz, id)); + return cacheValue != null ? cacheValue.getValue() : null; + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } else { + LOGGER.warn("Device {} cache missing", deviceId); + return Collections.emptyList(); + } } finally { lock.readLock().unlock(); } -- cgit v1.2.3