From 0f6b05206f7f7d8a288a56f497415c3396a23b6e Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 13 Apr 2019 21:49:15 +0200 Subject: [PATCH] clear MidnightTime cache --- .../nightscout/androidaps/utils/MidnightTime.java | 11 ++++++++++- .../nightscout/androidaps/utils/MidnightTimeTest.java | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java index 668ca02e96..235bb9c6e9 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java @@ -5,11 +5,13 @@ import android.util.LongSparseArray; import java.util.Calendar; public class MidnightTime { - private static final LongSparseArray times = new LongSparseArray<>(); + static final LongSparseArray times = new LongSparseArray<>(); private static long hits = 0; private static long misses = 0; + private static final int THRESHOLD = 100000; + public static long calc() { Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, 0); @@ -36,10 +38,17 @@ public class MidnightTime { m = c.getTimeInMillis(); times.append(time, m); ++misses; + if (times.size() > THRESHOLD) resetCache(); } return m; } + static void resetCache() { + hits = 0; + misses = 0; + times.clear(); + } + public static String log() { return "Hits: " + hits + " misses: " + misses + " stored: " + times.size(); } diff --git a/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java b/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java index a15efa6c24..823653c5ab 100644 --- a/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java +++ b/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java @@ -53,6 +53,13 @@ public class MidnightTimeTest { Assert.assertEquals(midnight, MidnightTime.calc(now)); } + @Test + public void resetCache() { + long now = DateUtil.now(); + MidnightTime.calc(now); + MidnightTime.resetCache(); + Assert.assertEquals(0, MidnightTime.times.size()); + } @Test public void log() { long now = DateUtil.now();