From 5490a0351abc9d6e741baf129300beb2d50051fd Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 22 Oct 2014 10:09:32 +1300 Subject: Put back cache timeout (fix #933) --- src/org/traccar/database/DataManager.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index bb44c6389..fc83e53a3 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -40,6 +40,14 @@ public class DataManager { public DataManager(Properties properties) throws Exception { if (properties != null) { initDatabase(properties); + + // Refresh delay + String refreshDelay = properties.getProperty("database.refreshDelay"); + if (refreshDelay != null) { + devicesRefreshDelay = Long.valueOf(refreshDelay) * 1000; + } else { + devicesRefreshDelay = DEFAULT_REFRESH_DELAY * 1000; + } } } @@ -127,14 +135,19 @@ public class DataManager { * Devices cache */ private Map devices; + private Calendar devicesLastUpdate; + private long devicesRefreshDelay; + private static final long DEFAULT_REFRESH_DELAY = 300; public Device getDeviceByImei(String imei) throws SQLException { - if (devices == null || !devices.containsKey(imei)) { + if (devices == null || !devices.containsKey(imei) || + (Calendar.getInstance().getTimeInMillis() - devicesLastUpdate.getTimeInMillis() > devicesRefreshDelay)) { devices = new HashMap(); for (Device device : getDevices()) { devices.put(device.getImei(), device); } + devicesLastUpdate = Calendar.getInstance(); } return devices.get(imei); -- cgit v1.2.3