From 617df639c1835b1710115403efe180c8f1804f02 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 7 Dec 2015 09:48:26 +1300 Subject: Refactor identity manager implementation --- src/org/traccar/database/DataManager.java | 23 ++++++++++++----------- src/org/traccar/database/IdentityManager.java | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 943dcb53c..deb31094e 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -111,17 +111,8 @@ public class DataManager implements IdentityManager { } } - @Override - public Device getDeviceById(long id) { - return devicesById.get(id); - } - - @Override - public Device getDeviceByUniqueId(String uniqueId) throws SQLException { - - if (System.currentTimeMillis() - devicesLastUpdate > devicesRefreshDelay - || !devicesByUniqueId.containsKey(uniqueId)) { - + private void updateDeviceCache(boolean force) throws SQLException { + if (System.currentTimeMillis() - devicesLastUpdate > devicesRefreshDelay || force) { devicesById.clear(); devicesByUniqueId.clear(); for (Device device : getAllDevices()) { @@ -130,7 +121,17 @@ public class DataManager implements IdentityManager { } devicesLastUpdate = System.currentTimeMillis(); } + } + @Override + public Device getDeviceById(long id) throws SQLException { + updateDeviceCache(!devicesById.containsKey(id)); + return devicesById.get(id); + } + + @Override + public Device getDeviceByUniqueId(String uniqueId) throws SQLException { + updateDeviceCache(!devicesByUniqueId.containsKey(uniqueId)); return devicesByUniqueId.get(uniqueId); } diff --git a/src/org/traccar/database/IdentityManager.java b/src/org/traccar/database/IdentityManager.java index 8bdce09a3..761219dd5 100644 --- a/src/org/traccar/database/IdentityManager.java +++ b/src/org/traccar/database/IdentityManager.java @@ -19,7 +19,7 @@ import org.traccar.model.Device; public interface IdentityManager { - Device getDeviceById(long id); + Device getDeviceById(long id) throws Exception; Device getDeviceByUniqueId(String uniqueId) throws Exception; -- cgit v1.2.3