Skip to content

Commit 345fa1a

Browse files
committed
Make CacheCache size dynamic (rel. to cgeo#18108)
(Opt-in feature, create preference key cachecache_size of type int with value 0)
1 parent 7bae216 commit 345fa1a

5 files changed

Lines changed: 48 additions & 2 deletions

File tree

main/src/main/java/cgeo/geocaching/settings/Settings.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import cgeo.geocaching.sensors.MagnetometerAndAccelerometerProvider;
3333
import cgeo.geocaching.sensors.OrientationProvider;
3434
import cgeo.geocaching.sensors.RotationProvider;
35+
import cgeo.geocaching.storage.CacheCache;
3536
import cgeo.geocaching.storage.DataStore;
3637
import cgeo.geocaching.storage.LocalStorage;
3738
import cgeo.geocaching.storage.PersistableFolder;
@@ -2673,7 +2674,6 @@ public static OfflineTranslateUtils.Language getTranslationTargetLanguage() {
26732674
return rawLanguage;
26742675
}
26752676

2676-
26772677
public static @NonNull Set<String> getLanguagesToNotTranslate() {
26782678
final Set<String> lngs = new HashSet<>();
26792679
if (sharedPrefs == null) {
@@ -2687,4 +2687,8 @@ public static OfflineTranslateUtils.Language getTranslationTargetLanguage() {
26872687
}
26882688
return lngs;
26892689
}
2690+
2691+
public static int getCacheCacheSize() {
2692+
return getInt(R.string.pref_cachecache_size, CacheCache.DEFAULT_CACHED_CACHES);
2693+
}
26902694
}

main/src/main/java/cgeo/geocaching/storage/CacheCache.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cgeo.geocaching.location.Viewport;
44
import cgeo.geocaching.models.Geocache;
5+
import cgeo.geocaching.settings.Settings;
56
import cgeo.geocaching.storage.DataStore.StorageLocation;
67
import cgeo.geocaching.utils.LeastRecentlyUsedMap;
78
import cgeo.geocaching.utils.Log;
@@ -19,13 +20,39 @@
1920
*/
2021
public class CacheCache {
2122

22-
private static final int MAX_CACHED_CACHES = 2000;
23+
/** Absolute lower bound for the cache size, kept for memory constrained devices. */
24+
private static final int MIN_CACHED_CACHES = 2000;
25+
/** Absolute upper bound to avoid unbounded growth even on devices with very large heaps. */
26+
private static final int MAX_CACHED_CACHES_LIMIT = 50000;
27+
/** Default value */
28+
public static final int DEFAULT_CACHED_CACHES = 2000;
29+
/** Actual value in this session */
30+
public static final int MAX_CACHED_CACHES = calculateMaxCachedCaches();
31+
2332
private final LeastRecentlyUsedMap<String, Geocache> cachesCache;
2433

2534
public CacheCache() {
2635
cachesCache = new LeastRecentlyUsedMap.LruCache<>(MAX_CACHED_CACHES);
2736
}
2837

38+
/**
39+
* Derive the maximum number of caches to keep in memory from the available heap.
40+
* <br>
41+
* Take up a quarter of heap at max, assuming 8kB per geocache on average
42+
*/
43+
private static int calculateMaxCachedCaches() {
44+
final int cacheSize = Settings.getCacheCacheSize();
45+
if (cacheSize == 0) {
46+
// dynamic setting
47+
final long fromHeap = (Runtime.getRuntime().maxMemory() / 4) / (8L * 1024);
48+
final int result = (int) Math.max(MIN_CACHED_CACHES, Math.min(MAX_CACHED_CACHES_LIMIT, fromHeap));
49+
Log.d("CacheCache: max cached caches = " + result + " (maxMemory=" + Runtime.getRuntime().maxMemory() + ")");
50+
return result;
51+
} else {
52+
return Math.max(MIN_CACHED_CACHES, Math.min(MAX_CACHED_CACHES_LIMIT, cacheSize));
53+
}
54+
}
55+
2956
public synchronized void removeAllFromCache() {
3057
cachesCache.clear();
3158
}
@@ -95,4 +122,8 @@ public synchronized String toString() {
95122
return StringUtils.join(cachesCache.keySet(), ' ');
96123
}
97124

125+
public synchronized int getSize() {
126+
return cachesCache.size();
127+
}
128+
98129
}

main/src/main/java/cgeo/geocaching/storage/DataStore.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6279,4 +6279,10 @@ public static void releaseDatabase(final boolean writable) {
62796279
}
62806280
Log.d("unlock db");
62816281
}
6282+
6283+
/** returns number of used entries in cacheCache */
6284+
public static int getActualCacheCacheSize() {
6285+
return cacheCache.getSize();
6286+
}
6287+
62826288
}

main/src/main/java/cgeo/geocaching/utils/SystemInformation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import cgeo.geocaching.sensors.OrientationProvider;
1818
import cgeo.geocaching.sensors.RotationProvider;
1919
import cgeo.geocaching.settings.Settings;
20+
import cgeo.geocaching.storage.CacheCache;
2021
import cgeo.geocaching.storage.ContentStorage;
2122
import cgeo.geocaching.storage.DataStore;
2223
import cgeo.geocaching.storage.FolderUtils;
@@ -46,6 +47,7 @@
4647
import androidx.core.util.Pair;
4748

4849
import java.io.File;
50+
import java.text.NumberFormat;
4951
import java.util.Arrays;
5052
import java.util.List;
5153
import java.util.Locale;
@@ -175,6 +177,8 @@ private static void appendMemoryInfo(@NonNull final Context context, @NonNull fi
175177
.append(", Threshold: ").append(Formatter.formatBytes(memoryInfo.threshold))
176178
.append(", low:").append(memoryInfo.lowMemory);
177179
}
180+
final NumberFormat intFormat = NumberFormat.getIntegerInstance(Locale.getDefault());
181+
body.append(", CacheCache: ").append(intFormat.format(DataStore.getActualCacheCacheSize())).append("/").append(intFormat.format(CacheCache.MAX_CACHED_CACHES));
178182
}
179183

180184
private static void appendDatabase(@NonNull final StringBuilder body) {

main/src/main/res/values/preference_keys.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@
656656
<string translatable="false" name="pref_home_location">home_location</string>
657657
<string translatable="false" name="pref_followMyLocation">followMyLocation</string>
658658
<string translatable="false" name="pref_autozoom_consider_lastcenter">list_viewport_calculation_consider_lastviewport</string>
659+
<string translatable="false" name="pref_cachecache_size">cachecache_size</string>
659660

660661

661662
<!-- ============================================================================================================================================================================== -->

0 commit comments

Comments
 (0)