Cache image data
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 25 Jun 2023 08:47:52 +0000 (10:47 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 25 Jun 2023 08:47:52 +0000 (10:47 +0200)
swt/org.argeo.cms.swt/src/org/argeo/cms/swt/osgi/BundleSvgTheme.java

index 038a1fca62a210ea331d12600bfac01fd44a67d5..74de83e09ce895ce442e214ef64e5bb835163f9f 100644 (file)
@@ -26,7 +26,7 @@ import org.osgi.framework.BundleContext;
 public class BundleSvgTheme extends BundleCmsSwtTheme {
        private final static Logger logger = System.getLogger(BundleSvgTheme.class.getName());
 
-//     private Map<String, Map<Integer, Image>> imageCache = Collections.synchronizedMap(new HashMap<>());
+       private Map<String, Map<Integer, ImageData>> imageDataCache = Collections.synchronizedMap(new HashMap<>());
 
        private Map<Integer, ImageTranscoder> transcoders = Collections.synchronizedMap(new HashMap<>());
 
@@ -64,6 +64,12 @@ public class BundleSvgTheme extends BundleCmsSwtTheme {
        }
 
        protected ImageData loadFromSvg(String path, int size) {
+               ImageData imageData = null;
+               if (imageDataCache.containsKey(path))
+                       imageData = imageDataCache.get(path).get(size);
+               if (imageData != null)
+                       return imageData;
+
                ImageTranscoder transcoder = null;
                synchronized (this) {
                        transcoder = transcoders.get(size);
@@ -75,7 +81,6 @@ public class BundleSvgTheme extends BundleCmsSwtTheme {
                                transcoders.put(size, transcoder);
                        }
                }
-               ImageData imageData;
                try (InputStream in = getResourceAsStream(path); ByteArrayOutputStream out = new ByteArrayOutputStream();) {
                        if (in == null)
                                throw new IllegalArgumentException(path + " not found");
@@ -90,6 +95,11 @@ public class BundleSvgTheme extends BundleCmsSwtTheme {
                        throw new RuntimeException("Cannot transcode SVG " + path, e);
                }
 
+               // cache it
+               if (!imageDataCache.containsKey(path))
+                       imageDataCache.put(path, Collections.synchronizedMap(new HashMap<>()));
+               imageDataCache.get(path).put(size, imageData);
+
                return imageData;
        }