diff options
Diffstat (limited to 'src/main/java/org/traccar/session/cache')
-rw-r--r-- | src/main/java/org/traccar/session/cache/CacheManager.java | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java index a934431be..4b42ea4e5 100644 --- a/src/main/java/org/traccar/session/cache/CacheManager.java +++ b/src/main/java/org/traccar/session/cache/CacheManager.java @@ -23,6 +23,7 @@ import org.traccar.model.Geofence; import org.traccar.model.GroupedModel; import org.traccar.model.Maintenance; import org.traccar.model.Notification; +import org.traccar.model.Position; import org.traccar.model.Server; import org.traccar.model.User; import org.traccar.storage.Storage; @@ -57,6 +58,7 @@ public class CacheManager { private final Map<CacheKey, CacheValue> deviceCache = new HashMap<>(); private final Map<Long, Map<Class<? extends BaseModel>, List<Long>>> deviceLinks = new HashMap<>(); + private final Map<Long, Position> devicePositions = new HashMap<>(); private Server server; private final Map<Long, List<User>> notificationUsers = new HashMap<>(); @@ -89,6 +91,15 @@ public class CacheManager { } } + public Position getPosition(long deviceId) { + try { + lock.readLock().lock(); + return devicePositions.get(deviceId); + } finally { + lock.readLock().unlock(); + } + } + public Server getServer() { try { lock.readLock().lock(); @@ -136,6 +147,17 @@ public class CacheManager { } } + public void updatePosition(Position position) { + try { + lock.writeLock().lock(); + if (deviceLinks.containsKey(position.getDeviceId())) { + devicePositions.put(position.getDeviceId(), position); + } + } finally { + lock.writeLock().unlock(); + } + } + public <T extends BaseModel> void updateOrInvalidate(Class<T> clazz, long id) throws StorageException { var object = storage.getObject(clazz, new Request( new Columns.All(), new Condition.Equals("id", "id", id))); @@ -201,8 +223,9 @@ public class CacheManager { private void unsafeAddDevice(long deviceId) throws StorageException { Map<Class<? extends BaseModel>, List<Long>> links = new HashMap<>(); - addObject(deviceId, storage.getObject(Device.class, new Request( - new Columns.All(), new Condition.Equals("id", "id", deviceId)))); + Device device = storage.getObject(Device.class, new Request( + new Columns.All(), new Condition.Equals("id", "id", deviceId))); + addObject(deviceId, device); for (Class<? extends BaseModel> clazz : CLASSES) { var objects = storage.getObjects(clazz, new Request( @@ -226,6 +249,11 @@ public class CacheManager { } deviceLinks.put(deviceId, links); + + if (device.getPositionId() > 0) { + devicePositions.put(deviceId, storage.getObject(Position.class, new Request( + new Columns.All(), new Condition.Equals("id", "id", device.getPositionId())))); + } } private void unsafeRemoveDevice(long deviceId) { @@ -237,6 +265,7 @@ public class CacheManager { return value.getReferences().size() > 0 ? value : null; }); })); + devicePositions.remove(deviceId); } private void invalidate(CacheKey... keys) throws StorageException { |