Skip to content

Commit 8961dd0

Browse files
committed
Move all caches to the new centralized class Caches
1 parent e796118 commit 8961dd0

File tree

8 files changed

+168
-98
lines changed

8 files changed

+168
-98
lines changed

src/org/openstreetmap/josm/plugins/mapillary/MapillaryPlugin.java

-23
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ public class MapillaryPlugin extends Plugin {
5151

5252
public static final ImageProvider LOGO = new ImageProvider("mapillary-logo");
5353

54-
/** Cache that stores the pictures the downloaded pictures. */
55-
private static final CacheAccess<String, BufferedImageCacheEntry> IMAGE_CACHE;
56-
5754
private static final MapillaryDownloadAction DOWNLOAD_ACTION = new MapillaryDownloadAction();
5855
private static final MapillaryExportAction EXPORT_ACTION = new MapillaryExportAction();
5956
/** Import action */
@@ -88,14 +85,6 @@ public class MapillaryPlugin extends Plugin {
8885
private static final JMenuItem UPLOAD_MENU;
8986

9087
static {
91-
CacheAccess<String, BufferedImageCacheEntry> cache = null;
92-
try {
93-
cache = JCSCacheManager.getCache("mapillary", 10, 10000, getCacheDirectory().getPath());
94-
} catch (IOException e) {
95-
Main.warn(e, "Could not initialize the Mapillary image cache.");
96-
}
97-
IMAGE_CACHE = cache;
98-
9988
if (Main.main == null) {
10089
EXPORT_MENU = null;
10190
DOWNLOAD_MENU = null;
@@ -272,18 +261,6 @@ public PreferenceSetting getPreferenceSetting() {
272261
return new MapillaryPreferenceSetting();
273262
}
274263

275-
public static CacheAccess<String, BufferedImageCacheEntry> getImageCache() {
276-
return IMAGE_CACHE;
277-
}
278-
279-
public static File getCacheDirectory() {
280-
final File f = new File(Main.pref.getPluginsDirectory().getPath() + "/Mapillary/cache");
281-
if (!f.exists()) {
282-
f.mkdirs();
283-
}
284-
return f;
285-
}
286-
287264
/**
288265
* @return the current {@link MapView} without throwing a {@link NullPointerException}
289266
*/

src/org/openstreetmap/josm/plugins/mapillary/MapillarySequence.java

+9-20
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
import javax.json.Json;
99

10-
import org.apache.commons.jcs.access.CacheAccess;
11-
import org.apache.commons.jcs.engine.behavior.IElementAttributes;
1210
import org.openstreetmap.josm.Main;
13-
import org.openstreetmap.josm.data.cache.JCSCacheManager;
11+
import org.openstreetmap.josm.plugins.mapillary.cache.Caches;
1412
import org.openstreetmap.josm.plugins.mapillary.model.UserProfile;
1513
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryURL;
1614
import org.openstreetmap.josm.plugins.mapillary.utils.api.JsonUserProfileDecoder;
@@ -22,20 +20,6 @@
2220
* @see MapillaryAbstractImage
2321
*/
2422
public class MapillarySequence {
25-
private static final CacheAccess<String, UserProfile> USER_CACHE;
26-
27-
static {
28-
CacheAccess<String, UserProfile> cache = null;
29-
try {
30-
cache = JCSCacheManager.getCache("userProfile", 100, 1000, MapillaryPlugin.getCacheDirectory().getPath());
31-
IElementAttributes atts = cache.getDefaultElementAttributes();
32-
atts.setMaxLife(604_800_000); // Sets lifetime to 7 days (604800000=1000*60*60*24*7)
33-
cache.setDefaultElementAttributes(atts);
34-
} catch (IOException e) {
35-
Main.warn(e, "Could not initialize user profile cache.");
36-
}
37-
USER_CACHE = cache;
38-
}
3923

4024
/**
4125
* The images in the sequence.
@@ -187,16 +171,21 @@ public void remove(MapillaryAbstractImage image) {
187171

188172
private void setUser(String userKey) {
189173
(new Thread(() -> {
190-
UserProfile cachedProfile = USER_CACHE == null ? null : USER_CACHE.get(userKey);
174+
UserProfile cachedProfile = Caches.UserProfileCache.getInstance().get(userKey);
191175
if(cachedProfile == null) {
192176
try {
193-
USER_CACHE.put(userKey, JsonUserProfileDecoder.decodeUserProfile(Json.createReader(MapillaryURL.APIv3.getUser(userKey).openStream()).readObject()));
177+
Caches.UserProfileCache.getInstance().put(
178+
userKey,
179+
JsonUserProfileDecoder.decodeUserProfile(
180+
Json.createReader(MapillaryURL.APIv3.getUser(userKey).openStream()).readObject()
181+
)
182+
);
194183
} catch (IOException var4) {
195184
Main.warn(var4, "Error when downloading user profile for user key '" + userKey + "'!");
196185
}
197186
}
198187

199-
this.user = USER_CACHE == null ? null : USER_CACHE.get(userKey);
188+
this.user = Caches.UserProfileCache.getInstance().get(userKey);
200189
}, "userProfileDownload_" + userKey)).start();
201190
}
202191
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// License: GPL. For details, see LICENSE file.
2+
package org.openstreetmap.josm.plugins.mapillary.cache;
3+
4+
import java.io.File;
5+
import java.io.IOException;
6+
7+
import javax.swing.ImageIcon;
8+
9+
import org.apache.commons.jcs.access.CacheAccess;
10+
import org.apache.commons.jcs.engine.behavior.IElementAttributes;
11+
12+
import org.openstreetmap.josm.Main;
13+
import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
14+
import org.openstreetmap.josm.data.cache.JCSCacheManager;
15+
import org.openstreetmap.josm.plugins.mapillary.model.UserProfile;
16+
17+
public final class Caches {
18+
19+
private Caches() {
20+
// Private constructor to avoid instantiation
21+
}
22+
23+
public static File getCacheDirectory() {
24+
final File f = new File(Main.pref.getPluginsDirectory().getPath() + "/Mapillary/cache");
25+
if (!f.exists()) {
26+
f.mkdirs();
27+
}
28+
return f;
29+
}
30+
31+
public abstract static class CacheProxy<K, V> {
32+
private final CacheAccess<K, V> cache;
33+
34+
public CacheProxy() {
35+
CacheAccess<K, V> c;
36+
try {
37+
c = createNewCache();
38+
} catch (IOException e) {
39+
Main.warn(e, "Could not initialize cache for " + getClass().getName());
40+
c = null;
41+
}
42+
cache = c;
43+
}
44+
45+
protected abstract CacheAccess<K, V> createNewCache() throws IOException;
46+
47+
public V get(final K key) {
48+
return cache == null ? null : cache.get(key);
49+
}
50+
51+
public void put(final K key, final V value) {
52+
if (cache != null) {
53+
cache.put(key, value);
54+
}
55+
}
56+
}
57+
58+
public static class ImageCache {
59+
private static ImageCache instance;
60+
private final CacheAccess<String, BufferedImageCacheEntry> cache;
61+
62+
public ImageCache() {
63+
CacheAccess<String, BufferedImageCacheEntry> c;
64+
try {
65+
c = JCSCacheManager.getCache("mapillary", 10, 10000, getCacheDirectory().getPath());
66+
} catch (IOException e) {
67+
Main.warn("Could not initialize the Mapillary image cache.");
68+
c = null;
69+
}
70+
cache = c;
71+
}
72+
73+
public CacheAccess<String, BufferedImageCacheEntry> getCache() {
74+
return cache;
75+
}
76+
77+
public static ImageCache getInstance() {
78+
synchronized (ImageCache.class) {
79+
if (instance == null) {
80+
instance = new ImageCache();
81+
}
82+
return instance;
83+
}
84+
}
85+
}
86+
87+
public static class MapObjectIconCache extends CacheProxy<String, ImageIcon> {
88+
private static CacheProxy<String, ImageIcon> instance;
89+
90+
public static CacheProxy<String, ImageIcon> getInstance() {
91+
synchronized (MapObjectIconCache.class) {
92+
if (instance == null) {
93+
instance = new MapObjectIconCache();
94+
}
95+
return instance;
96+
}
97+
}
98+
99+
@Override
100+
protected CacheAccess<String, ImageIcon> createNewCache() throws IOException {
101+
return JCSCacheManager.getCache("mapillaryObjectIcons", 100, 1000, getCacheDirectory().getPath());
102+
}
103+
}
104+
105+
public static class UserProfileCache extends CacheProxy<String, UserProfile> {
106+
private static CacheProxy<String, UserProfile> instance;
107+
108+
public static CacheProxy<String, UserProfile> getInstance() {
109+
synchronized (UserProfileCache.class) {
110+
if (instance == null) {
111+
instance = new UserProfileCache();
112+
}
113+
return instance;
114+
}
115+
}
116+
117+
@Override
118+
protected CacheAccess<String, UserProfile> createNewCache() throws IOException {
119+
CacheAccess<String, UserProfile> cache =
120+
JCSCacheManager.getCache("userProfile", 100, 1000, getCacheDirectory().getPath());
121+
IElementAttributes atts = cache.getDefaultElementAttributes();
122+
atts.setMaxLife(604_800_000); // Sets lifetime to 7 days (604800000=1000*60*60*24*7)
123+
cache.setDefaultElementAttributes(atts);
124+
return cache;
125+
}
126+
}
127+
}

src/org/openstreetmap/josm/plugins/mapillary/cache/MapillaryCache.java

+11-26
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// License: GPL. For details, see LICENSE file.
22
package org.openstreetmap.josm.plugins.mapillary.cache;
33

4-
import java.net.MalformedURLException;
54
import java.net.URL;
65
import java.util.HashMap;
76

87
import org.apache.commons.jcs.access.CacheAccess;
9-
import org.openstreetmap.josm.Main;
8+
109
import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
1110
import org.openstreetmap.josm.data.cache.JCSCachedTileLoaderJob;
12-
import org.openstreetmap.josm.plugins.mapillary.MapillaryPlugin;
11+
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryURL.Cloudfront;
1312

1413
/**
1514
* Stores the downloaded pictures locally.
@@ -44,33 +43,18 @@ public enum Type {
4443
* FULL_IMAGE).
4544
*/
4645
public MapillaryCache(String key, Type type) {
47-
this(MapillaryPlugin.getImageCache(), key, type);
46+
this(Caches.ImageCache.getInstance().getCache(), key, type);
4847
}
4948

5049
protected MapillaryCache(CacheAccess<String, BufferedImageCacheEntry> cache, String key, Type type) {
5150
super(cache, 50000, 50000, new HashMap<>());
52-
String k = null;
53-
URL u = null;
54-
if (key != null && type != null) {
55-
try {
56-
switch (type) {
57-
case FULL_IMAGE:
58-
k = key + ".FULL_IMAGE";
59-
u = new URL("https://d1cuyjsrcm0gby.cloudfront.net/" + key + "/thumb-2048.jpg");
60-
break;
61-
case THUMBNAIL:
62-
default:
63-
k = key + ".THUMBNAIL";
64-
u = new URL("https://d1cuyjsrcm0gby.cloudfront.net/" + key + "/thumb-320.jpg");
65-
break;
66-
}
67-
} catch (MalformedURLException e) {
68-
// TODO: Throw exception, so that a MapillaryCache with malformed URL can't be instantiated.
69-
Main.error(e);
70-
}
51+
if (key == null || type == null) {
52+
this.key = null;
53+
this.url = null;
54+
} else {
55+
this.key = key + (type == Type.FULL_IMAGE ? ".FULL_IMAGE" : ".THUMBNAIL");
56+
this.url = Cloudfront.thumbnail(key, type == Type.FULL_IMAGE);
7157
}
72-
this.key = k;
73-
this.url = u;
7458
}
7559

7660
@Override
@@ -90,8 +74,9 @@ protected BufferedImageCacheEntry createCacheEntry(byte[] content) {
9074

9175
@Override
9276
protected boolean isObjectLoadable() {
93-
if (this.cacheData == null)
77+
if (this.cacheData == null) {
9478
return false;
79+
}
9580
byte[] content = this.cacheData.getContent();
9681
return content != null && content.length > 0;
9782
}

src/org/openstreetmap/josm/plugins/mapillary/model/MapObject.java

+3-21
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,16 @@
88
import javax.imageio.ImageIO;
99
import javax.swing.ImageIcon;
1010

11-
import org.apache.commons.jcs.access.CacheAccess;
1211
import org.openstreetmap.josm.Main;
13-
import org.openstreetmap.josm.data.cache.JCSCacheManager;
1412
import org.openstreetmap.josm.data.coor.LatLon;
15-
import org.openstreetmap.josm.plugins.mapillary.MapillaryPlugin;
13+
import org.openstreetmap.josm.plugins.mapillary.cache.Caches.MapObjectIconCache;
1614
import org.openstreetmap.josm.plugins.mapillary.utils.MapillaryURL.MainWebsite;
1715
import org.openstreetmap.josm.tools.ImageProvider;
1816

1917
public class MapObject {
2018
private static final ImageIcon ICON_UNKNOWN_TYPE = ImageProvider.get("unknown-mapobject-type");
21-
private static final CacheAccess<String, ImageIcon> MAP_OBJECT_ICON_CACHE;
2219
private static Function<String, URL> iconUrlGen = MainWebsite::mapObjectIcon;
2320

24-
static {
25-
CacheAccess<String, ImageIcon> o;
26-
try {
27-
o = JCSCacheManager.getCache(
28-
"mapillaryObjectIcons", 100, 1000, MapillaryPlugin.getCacheDirectory().getPath()
29-
);
30-
} catch (IOException e) {
31-
Main.warn(e, "Could not initialize cache for map objects");
32-
o = null;
33-
}
34-
MAP_OBJECT_ICON_CACHE = o;
35-
}
36-
3721
private final LatLon coordinate;
3822
private final String key;
3923
private final String objPackage;
@@ -68,15 +52,13 @@ public LatLon getCoordinate() {
6852
* @return the icon, which represents the given objectTypeID
6953
*/
7054
public static ImageIcon getIcon(final String objectTypeID) {
71-
final ImageIcon cachedIcon = MAP_OBJECT_ICON_CACHE != null ? MAP_OBJECT_ICON_CACHE.get(objectTypeID) : null;
55+
final ImageIcon cachedIcon = MapObjectIconCache.getInstance().get(objectTypeID);
7256
if ("not-in-set".equals(objectTypeID)) {
7357
return ICON_UNKNOWN_TYPE;
7458
} else if (cachedIcon == null) {
7559
try {
7660
final ImageIcon downloadedIcon = new ImageIcon(ImageIO.read(iconUrlGen.apply(objectTypeID)));
77-
if (MAP_OBJECT_ICON_CACHE != null) {
78-
MAP_OBJECT_ICON_CACHE.put(objectTypeID, downloadedIcon);
79-
}
61+
MapObjectIconCache.getInstance().put(objectTypeID, downloadedIcon);
8062
return downloadedIcon;
8163
} catch (IOException e) {
8264
Main.warn(e, "Failed to download icon " + objectTypeID);

src/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryURL.java

+12
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ public static String queryString(final Bounds bounds) {
100100
}
101101
}
102102

103+
public static final class Cloudfront {
104+
private static final String BASE_URL = "https://d1cuyjsrcm0gby.cloudfront.net/";
105+
106+
private Cloudfront() {
107+
// Private constructor to avoid instantiation
108+
}
109+
110+
public static URL thumbnail(final String key, final boolean large) {
111+
return string2URL(BASE_URL, key, "/thumb-", large ? "2048" : "320", ".png");
112+
}
113+
}
114+
103115
public static final class MainWebsite {
104116
private static final String BASE_URL = "https://www.mapillary.com/";
105117

test/unit/org/openstreetmap/josm/plugins/mapillary/cache/MapillaryCacheTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99

1010
import org.apache.commons.jcs.access.CacheAccess;
1111
import org.junit.Test;
12+
1213
import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
1314
import org.openstreetmap.josm.data.cache.JCSCacheManager;
1415
import org.openstreetmap.josm.plugins.mapillary.AbstractTest;
15-
import org.openstreetmap.josm.plugins.mapillary.MapillaryPlugin;
1616
import org.openstreetmap.josm.plugins.mapillary.cache.MapillaryCache.Type;
1717

1818
public class MapillaryCacheTest extends AbstractTest {
1919

2020
@Test
2121
public void test() throws IOException {
2222
CacheAccess<String, BufferedImageCacheEntry> cacheAccess = JCSCacheManager.getCache("mapillary", 10, 10000,
23-
MapillaryPlugin.getCacheDirectory().getPath());
23+
Caches.getCacheDirectory().getPath());
2424

2525
MapillaryCache cache = new MapillaryCache(cacheAccess, "00000", Type.FULL_IMAGE);
2626
assertNotEquals(null, cache.getUrl());

0 commit comments

Comments
 (0)