|
| 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 | +} |
0 commit comments