aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/session
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/session')
-rw-r--r--src/main/java/org/traccar/session/cache/CacheManager.java36
1 files changed, 30 insertions, 6 deletions
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java
index 6ea0f252d..586237655 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.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;
import org.traccar.storage.StorageException;
@@ -57,12 +58,14 @@ 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 Server server;
private final Map<Long, Position> devicePositions = new HashMap<>();
private final Map<Long, List<User>> notificationUsers = new HashMap<>();
@Inject
public CacheManager(Storage storage) throws StorageException {
this.storage = storage;
+ invalidateServer();
invalidateUsers();
}
@@ -96,6 +99,15 @@ public class CacheManager {
}
}
+ public Server getServer() {
+ try {
+ lock.readLock().lock();
+ return server;
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
public List<User> getNotificationUsers(long notificationId) {
try {
lock.readLock().lock();
@@ -154,6 +166,10 @@ public class CacheManager {
invalidate(new CacheKey(clazz1, id1), new CacheKey(clazz2, id2));
}
+ private void invalidateServer() throws StorageException {
+ server = storage.getObject(Server.class, new Request(new Columns.All()));
+ }
+
private void invalidateUsers() throws StorageException {
Map<Long, User> users = new HashMap<>();
storage.getObjects(User.class, new Request(new Columns.All()))
@@ -220,21 +236,29 @@ public class CacheManager {
}
private void unsafeInvalidate(CacheKey[] keys) throws StorageException {
+ boolean invalidateServer = false;
boolean invalidateUsers = false;
Set<Long> linkedDevices = new HashSet<>();
for (var key : keys) {
- if (key.classIs(User.class) || key.classIs(Notification.class)) {
- invalidateUsers = true;
+ if (key.classIs(Server.class)) {
+ invalidateServer = true;
+ } else {
+ if (key.classIs(User.class) || key.classIs(Notification.class)) {
+ invalidateUsers = true;
+ }
+ deviceCache.computeIfPresent(key, (k, value) -> {
+ linkedDevices.addAll(value.getReferences());
+ return value;
+ });
}
- deviceCache.computeIfPresent(key, (k, value) -> {
- linkedDevices.addAll(value.getReferences());
- return value;
- });
}
for (long deviceId : linkedDevices) {
unsafeRemoveDevice(deviceId);
unsafeAddDevice(deviceId);
}
+ if (invalidateServer) {
+ invalidateServer();
+ }
if (invalidateUsers) {
invalidateUsers();
}