diff options
author | Anton Tananaev <anton@traccar.org> | 2022-06-18 16:48:15 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-06-18 16:48:15 -0700 |
commit | 45940f73c57b871711ee9fb41ebeba3c0ca9e9b6 (patch) | |
tree | ad93db24ab3500f38eb5d5113fc76ce1e735e1f4 /src/main/java/org/traccar/session/cache | |
parent | 440c6069b81bde2a241b5c0829cf951246a386f2 (diff) | |
download | trackermap-server-45940f73c57b871711ee9fb41ebeba3c0ca9e9b6.tar.gz trackermap-server-45940f73c57b871711ee9fb41ebeba3c0ca9e9b6.tar.bz2 trackermap-server-45940f73c57b871711ee9fb41ebeba3c0ca9e9b6.zip |
Fix offline device notifications
Diffstat (limited to 'src/main/java/org/traccar/session/cache')
-rw-r--r-- | src/main/java/org/traccar/session/cache/CacheManager.java | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java index 87384f746..abc8ca4c9 100644 --- a/src/main/java/org/traccar/session/cache/CacheManager.java +++ b/src/main/java/org/traccar/session/cache/CacheManager.java @@ -60,6 +60,7 @@ public class CacheManager { private final ReadWriteLock lock = new ReentrantReadWriteLock(); private final Map<CacheKey, CacheValue> deviceCache = new HashMap<>(); + private final Map<Long, Integer> deviceReferences = new HashMap<>(); private final Map<Long, Map<Class<? extends BaseModel>, List<Long>>> deviceLinks = new HashMap<>(); private final Map<Long, Position> devicePositions = new HashMap<>(); @@ -136,9 +137,14 @@ public class CacheManager { public void addDevice(long deviceId) throws StorageException { try { lock.writeLock().lock(); - if (!deviceLinks.containsKey(deviceId)) { + Integer references = deviceReferences.get(deviceId); + if (references != null) { + references += 1; + } else { unsafeAddDevice(deviceId); + references = 1; } + deviceReferences.put(deviceId, references); } finally { lock.writeLock().unlock(); } @@ -147,8 +153,15 @@ public class CacheManager { public void removeDevice(long deviceId) { try { lock.writeLock().lock(); - if (deviceLinks.containsKey(deviceId)) { - unsafeRemoveDevice(deviceId); + Integer references = deviceReferences.get(deviceId); + if (references != null) { + references -= 1; + if (references <= 0) { + unsafeRemoveDevice(deviceId); + deviceReferences.remove(deviceId); + } else { + deviceReferences.put(deviceId, references); + } } } finally { lock.writeLock().unlock(); |