001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins.streetside.cache;
003
004import java.io.File;
005import java.io.IOException;
006import java.io.Serializable;
007
008import javax.swing.ImageIcon;
009
010import org.apache.commons.jcs.access.CacheAccess;
011import org.apache.commons.jcs.engine.behavior.IElementAttributes;
012
013import org.openstreetmap.josm.Main;
014import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
015import org.openstreetmap.josm.data.cache.JCSCacheManager;
016import org.openstreetmap.josm.plugins.streetside.model.UserProfile;
017import org.openstreetmap.josm.tools.Logging;
018
019public final class Caches {
020
021        private Caches() {
022                // Private constructor to avoid instantiation
023        }
024
025        public static File getCacheDirectory() {
026                final File f = new File(Main.pref.getPluginsDirectory().getPath() + "/MicrosoftStreetside/cache");
027                if (!f.exists()) {
028                        f.mkdirs();
029                }
030                return f;
031        }
032
033        public abstract static class CacheProxy<K, V extends Serializable> {
034                private final CacheAccess<K, V> cache;
035
036                public CacheProxy() {
037                        CacheAccess<K, V> c;
038                        try {
039                                c = createNewCache();
040                        } catch (IOException e) {
041                                Logging.log(Logging.LEVEL_WARN, "Could not initialize cache for " + getClass().getName(), e);
042                                c = null;
043                        }
044                        cache = c;
045                }
046
047                protected abstract CacheAccess<K, V> createNewCache() throws IOException;
048
049                public V get(final K key) {
050                        return cache == null ? null : cache.get(key);
051                }
052
053                public void put(final K key, final V value) {
054                        if (cache != null) {
055                                cache.put(key, value);
056                        }
057                }
058        }
059
060        public static class ImageCache {
061                private static ImageCache instance;
062                private final CacheAccess<String, BufferedImageCacheEntry> cache;
063
064                public ImageCache() {
065                        CacheAccess<String, BufferedImageCacheEntry> c;
066                        try {
067                                c = JCSCacheManager.getCache("streetside", 10, 10000, Caches.getCacheDirectory().getPath());
068                        } catch (Exception e) {
069                                Logging.log(Logging.LEVEL_WARN, "Could not initialize the Streetside image cache.", e);
070                                c = null;
071                        }
072                        cache = c;
073                }
074
075                public CacheAccess<String, BufferedImageCacheEntry> getCache() {
076                        return cache;
077                }
078
079                public static ImageCache getInstance() {
080                        synchronized (ImageCache.class) {
081                                if (ImageCache.instance == null) {
082                                        ImageCache.instance = new ImageCache();
083                                }
084                                return ImageCache.instance;
085                        }
086                }
087        }
088
089        public static class CubemapCache {
090                private static CubemapCache instance;
091                private final CacheAccess<String, BufferedImageCacheEntry> cache;
092
093                public CubemapCache() {
094                        CacheAccess<String, BufferedImageCacheEntry> c;
095                        try {
096                                c = JCSCacheManager.getCache("streetside", 10, 10000, Caches.getCacheDirectory().getPath());
097                        } catch (Exception e) {
098                                Logging.log(Logging.LEVEL_WARN, "Could not initialize the Streetside cubemap cache.", e);
099                                c = null;
100                        }
101                        cache = c;
102                }
103
104                public CacheAccess<String, BufferedImageCacheEntry> getCache() {
105                        return cache;
106                }
107
108                public static CubemapCache getInstance() {
109                        synchronized (CubemapCache.class) {
110                                if (CubemapCache.instance == null) {
111                                        CubemapCache.instance = new CubemapCache();
112                                }
113                                return CubemapCache.instance;
114                        }
115                }
116        }
117
118        public static class MapObjectIconCache extends CacheProxy<String, ImageIcon> {
119                private static CacheProxy<String, ImageIcon> instance;
120
121                public static CacheProxy<String, ImageIcon> getInstance() {
122                        synchronized (MapObjectIconCache.class) {
123                                if (MapObjectIconCache.instance == null) {
124                                        MapObjectIconCache.instance = new MapObjectIconCache();
125                                }
126                                return MapObjectIconCache.instance;
127                        }
128                }
129
130                @Override
131                protected CacheAccess<String, ImageIcon> createNewCache() throws IOException {
132                        return JCSCacheManager.getCache("streetsideObjectIcons", 100, 1000, Caches.getCacheDirectory().getPath());
133                }
134        }
135
136        public static class UserProfileCache extends CacheProxy<String, UserProfile> {
137                private static CacheProxy<String, UserProfile> instance;
138
139                public static CacheProxy<String, UserProfile> getInstance() {
140                        synchronized (UserProfileCache.class) {
141                                if (UserProfileCache.instance == null) {
142                                        UserProfileCache.instance = new UserProfileCache();
143                                }
144                                return UserProfileCache.instance;
145                        }
146                }
147
148                @Override
149                protected CacheAccess<String, UserProfile> createNewCache() throws IOException {
150                        CacheAccess<String, UserProfile> cache =
151                                        JCSCacheManager.getCache("userProfile", 100, 1000, Caches.getCacheDirectory().getPath());
152                        IElementAttributes atts = cache.getDefaultElementAttributes();
153                        atts.setMaxLife(604_800_000); // Sets lifetime to 7 days (604800000=1000*60*60*24*7)
154                        cache.setDefaultElementAttributes(atts);
155                        return cache;
156                }
157        }
158}