clear MidnightTime cache

This commit is contained in:
Milos Kozak 2019-04-13 21:49:15 +02:00
parent 1286aabb5b
commit 0f6b05206f
2 changed files with 17 additions and 1 deletions

View file

@ -5,11 +5,13 @@ import android.util.LongSparseArray;
import java.util.Calendar; import java.util.Calendar;
public class MidnightTime { public class MidnightTime {
private static final LongSparseArray<Long> times = new LongSparseArray<>(); static final LongSparseArray<Long> times = new LongSparseArray<>();
private static long hits = 0; private static long hits = 0;
private static long misses = 0; private static long misses = 0;
private static final int THRESHOLD = 100000;
public static long calc() { public static long calc() {
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.HOUR_OF_DAY, 0);
@ -36,10 +38,17 @@ public class MidnightTime {
m = c.getTimeInMillis(); m = c.getTimeInMillis();
times.append(time, m); times.append(time, m);
++misses; ++misses;
if (times.size() > THRESHOLD) resetCache();
} }
return m; return m;
} }
static void resetCache() {
hits = 0;
misses = 0;
times.clear();
}
public static String log() { public static String log() {
return "Hits: " + hits + " misses: " + misses + " stored: " + times.size(); return "Hits: " + hits + " misses: " + misses + " stored: " + times.size();
} }

View file

@ -53,6 +53,13 @@ public class MidnightTimeTest {
Assert.assertEquals(midnight, MidnightTime.calc(now)); 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 @Test
public void log() { public void log() {
long now = DateUtil.now(); long now = DateUtil.now();