diff options
author | Anton Tananaev <anton@traccar.org> | 2022-06-08 07:52:59 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-06-08 07:52:59 -0700 |
commit | cd229daa23cfc43ad5056c45c4aaecefdbbf826c (patch) | |
tree | 4dd8932cda394d20e59143e5e44714695f21c549 /src/main/java/org/traccar/session/cache/CacheManager.java | |
parent | 5890e03199142f041dc19160329a740b4d01450d (diff) | |
download | trackermap-server-cd229daa23cfc43ad5056c45c4aaecefdbbf826c.tar.gz trackermap-server-cd229daa23cfc43ad5056c45c4aaecefdbbf826c.tar.bz2 trackermap-server-cd229daa23cfc43ad5056c45c4aaecefdbbf826c.zip |
Improve cache invalidation
Diffstat (limited to 'src/main/java/org/traccar/session/cache/CacheManager.java')
-rw-r--r-- | src/main/java/org/traccar/session/cache/CacheManager.java | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java index 586237655..8e1737441 100644 --- a/src/main/java/org/traccar/session/cache/CacheManager.java +++ b/src/main/java/org/traccar/session/cache/CacheManager.java @@ -20,9 +20,9 @@ import org.traccar.model.BaseModel; import org.traccar.model.Device; import org.traccar.model.Driver; 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; @@ -59,7 +59,6 @@ public class CacheManager { private final Map<Long, Map<Class<? extends BaseModel>, List<Long>>> deviceLinks = new HashMap<>(); private Server server; - private final Map<Long, Position> devicePositions = new HashMap<>(); private final Map<Long, List<User>> notificationUsers = new HashMap<>(); @Inject @@ -90,15 +89,6 @@ 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(); @@ -146,18 +136,34 @@ public class CacheManager { } } - public void updatePosition(Position position) { - try { - lock.writeLock().lock(); - devicePositions.put(position.getDeviceId(), position); - } finally { - lock.writeLock().unlock(); + public void updateOrInvalidate(Class<? extends BaseModel> clazz, long id) throws StorageException { + boolean invalidate = false; + var before = getObject(clazz, id); + var after = storage.getObject(clazz, new Request( + new Columns.All(), new Condition.Equals("id", "id", id))); + if (before == null) { + return; + } else if (after == null) { + invalidate = true; + } else if (clazz.isInstance(GroupedModel.class)) { + if (((GroupedModel) before).getGroupId() != ((GroupedModel) after).getGroupId()) { + invalidate = true; + } + } + if (invalidate) { + invalidate(new CacheKey(clazz, id)); + } else { + try { + lock.writeLock().lock(); + var cacheValue = deviceCache.get(new CacheKey(clazz, id)); + if (cacheValue != null) { + cacheValue.setValue(after); + } + // TODO if device, also need to update geofences + } finally { + lock.writeLock().unlock(); + } } - } - - public void invalidate( - Class<? extends BaseModel> clazz, long id) throws StorageException { - invalidate(new CacheKey(clazz, id)); } public void invalidate( |