From 65d3654dcca878cfd839e40c0dd020da199be09e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 8 Jun 2022 08:14:20 -0700 Subject: More cache improvements --- .../java/org/traccar/api/BaseObjectResource.java | 4 +-- .../org/traccar/session/cache/CacheManager.java | 35 +++++++++++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) (limited to 'src/main/java/org/traccar') diff --git a/src/main/java/org/traccar/api/BaseObjectResource.java b/src/main/java/org/traccar/api/BaseObjectResource.java index c033fbb62..eb1db5e89 100644 --- a/src/main/java/org/traccar/api/BaseObjectResource.java +++ b/src/main/java/org/traccar/api/BaseObjectResource.java @@ -106,7 +106,7 @@ public abstract class BaseObjectResource extends BaseResour new Columns.Exclude("id"), new Condition.Equals("id", "id"))); } - cacheManager.updateOrInvalidate(baseClass, entity.getId()); + cacheManager.updateOrInvalidate(entity); LogAction.edit(getUserId(), entity); @@ -135,7 +135,7 @@ public abstract class BaseObjectResource extends BaseResour } else { storage.removeObject(baseClass, new Request(new Condition.Equals("id", "id", id))); } - cacheManager.updateOrInvalidate(baseClass, id); + cacheManager.invalidate(baseClass, id); LogAction.remove(getUserId(), baseClass, id); diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java index 8e1737441..a934431be 100644 --- a/src/main/java/org/traccar/session/cache/CacheManager.java +++ b/src/main/java/org/traccar/session/cache/CacheManager.java @@ -136,36 +136,43 @@ public class CacheManager { } } - public void updateOrInvalidate(Class clazz, long id) throws StorageException { - boolean invalidate = false; - var before = getObject(clazz, id); - var after = storage.getObject(clazz, new Request( + public void updateOrInvalidate(Class clazz, long id) throws StorageException { + var object = storage.getObject(clazz, new Request( new Columns.All(), new Condition.Equals("id", "id", id))); + if (object != null) { + updateOrInvalidate(object); + } else { + invalidate(clazz, id); + } + } + + public void updateOrInvalidate(T object) throws StorageException { + boolean invalidate = false; + var before = getObject(object.getClass(), object.getId()); if (before == null) { return; - } else if (after == null) { - invalidate = true; - } else if (clazz.isInstance(GroupedModel.class)) { - if (((GroupedModel) before).getGroupId() != ((GroupedModel) after).getGroupId()) { + } else if (object instanceof GroupedModel) { + if (((GroupedModel) before).getGroupId() != ((GroupedModel) object).getGroupId()) { invalidate = true; } } if (invalidate) { - invalidate(new CacheKey(clazz, id)); + invalidate(object.getClass(), object.getId()); } else { + // TODO if device, also need to update geofences 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 + deviceCache.get(new CacheKey(object.getClass(), object.getId())).setValue(object); } finally { lock.writeLock().unlock(); } } } + public void invalidate(Class clazz, long id) throws StorageException { + invalidate(new CacheKey(clazz, id)); + } + public void invalidate( Class clazz1, long id1, Class clazz2, long id2) throws StorageException { -- cgit v1.2.3