Index: applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Cache.java
===================================================================
--- applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Cache.java	(revision 15091)
+++ 	(revision )
@@ -1,168 +1,0 @@
-package wmsplugin;
-
-import java.awt.image.BufferedImage;
-import java.io.File;
-import java.io.FileFilter;
-import java.math.BigInteger;
-import java.security.MessageDigest;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.Random;
-import java.util.Set;
-import java.util.TreeMap;
-import javax.imageio.*;
-
-import org.openstreetmap.josm.Main;
-
-public class Cache {
-    final File dir;
-    // Last 5 days
-    private final long expire = Main.pref.getInteger("wmsplugin.cache.expire", 1000*60*60*24*5);
-    // 40 MBytes
-    private final long maxsize = Main.pref.getInteger("wmsplugin.cache.size", 1000*1000*40);
-    private boolean disabled = false;
-    // If the cache is full, we don't want to delete just one file
-    private final int cleanUpThreshold = 20;
-    // After how many file-writes do we want to check if the cache needs emptying?
-    private final int cleanUpInterval = 5;
-
-    Cache() {
-        this(Main.pref.get("wmsplugin.cache.path", WMSPlugin.getPrefsPath() + "cache"));
-    }
-
-    Cache(String working_dir) {
-        // Override default working directory
-        this.dir = new File(working_dir);
-        try {
-            this.dir.mkdirs();
-        } catch(Exception e) {
-            // We probably won't be able to do anything anyway
-            disabled = true;
-        }
-
-        if(expire <= 0 || maxsize <= 0)
-            disabled = true;
-    }
-
-    public BufferedImage getImg(String ident) {
-        if(disabled) return null;
-        try {
-            File img = getPath(ident);
-            if(!img.exists()) {
-                //System.out.println("Miss");
-                return null;
-            }
-            if(img.lastModified() < (new Date().getTime() - expire)) {
-                img.delete();
-                //System.out.println("Miss");
-                return null;
-            }
-            //System.out.println("Hit");
-            return ImageIO.read(img);
-        } catch(Exception e) {
-            System.out.println(e.getMessage());
-        }
-        //System.out.println("Miss");
-        return null;
-    }
-
-    public void saveImg(String ident, BufferedImage image) {
-        if(disabled) return;
-        try {
-            ImageIO.write(image, "png", getPath(ident));
-        } catch(Exception e){
-            System.out.println(e.getMessage());
-        }
-
-        checkCleanUp();
-    }
-
-    public BufferedImage saveImg(String ident, BufferedImage image, boolean passThrough) {
-        saveImg(ident, image);
-        return image;
-    }
-
-    public void checkCleanUp() {
-        // The Cache class isn't persistent in its current implementation,
-        // therefore clean up on random intervals, but not every write
-        if(new Random().nextInt(cleanUpInterval) == 0)
-            cleanUp();
-    }
-
-    public void cleanUp() {
-        if(disabled) return;
-
-        TreeMap<Long, File> modtime = new TreeMap<Long, File>();
-        long time = new Date().getTime() - expire;
-        long dirsize = 0;
-
-        for(File f : getFiles()) {
-            if(f.lastModified() < time)
-                f.delete();
-            else {
-                dirsize += f.length();
-                modtime.put(f.lastModified(), f);
-            }
-        }
-
-        if(dirsize < maxsize) return;
-
-        Set keySet = modtime.keySet();
-        Iterator it = keySet.iterator();
-        int i=0;
-        while (it.hasNext()) {
-            i++;
-            ((File)modtime.get(it.next())).delete();
-
-            // Delete a couple of files, then check again
-            if(i % cleanUpThreshold == 0 && getDirSize() < maxsize)
-                return;
-        }
-    }
-
-    public void deleteSmallFiles(int size) {
-        if(disabled) return;
-        for(File f : getFiles()) {
-            if(f.length() < size)
-                f.delete();
-        }
-    }
-
-    private long getDirSize() {
-        if(disabled) return -1;
-        long dirsize = 0;
-
-        for(File f : getFiles())
-            dirsize += f.length();
-        return dirsize;
-    }
-
-    private File[] getFiles() {
-        if(disabled) return null;
-        return dir.listFiles(
-            new FileFilter() {
-                public boolean accept(File file) {
-                    return file.getName().endsWith(".png");
-                }
-            }
-        );
-    }
-
-    private static String getUniqueFilename(String ident) {
-        try {
-            MessageDigest md = MessageDigest.getInstance("MD5");
-            BigInteger number = new BigInteger(1, md.digest(ident.getBytes()));
-            return number.toString(16);
-        } catch(Exception e) {
-            // Fall back. Remove unsuitable characters and some random ones to shrink down path length.
-            // Limit it to 70 characters, that leaves about 190 for the path on Windows/NTFS
-            ident = ident.replaceAll("[^a-zA-Z0-9]", "");
-            ident = ident.replaceAll("[acegikmoqsuwy]", "");
-            return ident.substring(ident.length() - 70);
-        }
-    }
-
-    private File getPath(String ident) {
-        return new File(dir, getUniqueFilename(ident) + ".png");
-    }
-}
Index: applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Grabber.java
===================================================================
--- applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Grabber.java	(revision 15091)
+++ applications/editors/josm/plugins/wmsplugin/src/wmsplugin/Grabber.java	(revision 15185)
@@ -2,4 +2,9 @@
 
 import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.awt.image.BufferedImage;
+import java.awt.Graphics;
+import java.awt.Color;
+import java.awt.Font;
 
 import org.openstreetmap.josm.data.Bounds;
@@ -7,10 +12,6 @@
 import org.openstreetmap.josm.gui.MapView;
 import org.openstreetmap.josm.Main;
-import java.awt.image.BufferedImage;
-import java.awt.Graphics;
-import java.awt.Color;
-import java.awt.Font;
-import javax.swing.JOptionPane;
 import org.openstreetmap.josm.data.coor.LatLon;
+import org.openstreetmap.josm.io.CacheFiles;
 
 abstract public class Grabber implements Runnable {
@@ -21,7 +22,8 @@
     protected WMSLayer layer;
     protected GeorefImage image;
+    protected CacheFiles cache;
 
     Grabber(Bounds b, Projection proj, double pixelPerDegree, GeorefImage image,
-    MapView mv, WMSLayer layer)
+    MapView mv, WMSLayer layer, CacheFiles cache)
     {
         if (b.min != null && b.max != null && WMSPlugin.doOverlap)
@@ -49,4 +51,5 @@
         this.mv = mv;
         this.layer = layer;
+        this.cache = cache;
     }
 
Index: applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSGrabber.java
===================================================================
--- applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSGrabber.java	(revision 15091)
+++ applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSGrabber.java	(revision 15185)
@@ -28,4 +28,5 @@
 import org.openstreetmap.josm.data.projection.Projection;
 import org.openstreetmap.josm.gui.MapView;
+import org.openstreetmap.josm.io.CacheFiles;
 import org.openstreetmap.josm.io.ProgressInputStream;
 
@@ -33,11 +34,10 @@
 public class WMSGrabber extends Grabber {
     protected String baseURL;
-    protected Cache cache = new wmsplugin.Cache();
     private static Boolean shownWarning = false;
     private boolean urlWithPatterns;
 
     WMSGrabber(String baseURL, Bounds b, Projection proj,
-            double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) {
-        super(b, proj, pixelPerDegree, image, mv, layer);
+            double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer, CacheFiles cache) {
+        super(b, proj, pixelPerDegree, image, mv, layer, cache);
         this.baseURL = baseURL;
         /* URL containing placeholders? */
@@ -147,5 +147,6 @@
         is.close();
         
-        return cache.saveImg(url.toString(), img, true);
+        cache.saveImg(url.toString(), img);
+        return img;
     }
 
Index: applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java
===================================================================
--- applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java	(revision 15091)
+++ applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSLayer.java	(revision 15185)
@@ -255,5 +255,5 @@
             // Delete small files, because they're probably blank tiles.
             // See https://josm.openstreetmap.de/ticket/2307
-            new wmsplugin.Cache().deleteSmallFiles(2048);
+            WMSPlugin.cache.customCleanUp(WMSPlugin.cache.CLEAN_SMALL_FILES, 2048);
 
             for (int x = 0; x < dax; ++x) {
Index: applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPlugin.java
===================================================================
--- applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPlugin.java	(revision 15091)
+++ applications/editors/josm/plugins/wmsplugin/src/wmsplugin/WMSPlugin.java	(revision 15185)
@@ -24,4 +24,5 @@
 import org.openstreetmap.josm.gui.IconToggleButton;
 import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
+import org.openstreetmap.josm.io.CacheFiles;
 import org.openstreetmap.josm.io.MirroredInputStream;
 import org.openstreetmap.josm.actions.JosmAction;
@@ -31,4 +32,5 @@
 
 public class WMSPlugin extends Plugin {
+    static CacheFiles cache = new CacheFiles("wmsplugin");
 
     WMSLayer wmsLayer;
@@ -54,4 +56,6 @@
         }
         refreshMenu();
+        cache.setExpire(cache.EXPIRE_MONTHLY, false);
+        cache.setMaxSize(70, false);
     }
 
@@ -199,7 +203,7 @@
                      double _pixelPerDegree, GeorefImage _image, MapView _mv, WMSLayer _layer){
         if(_baseURL.startsWith("yahoo://"))
-            return new YAHOOGrabber(_baseURL, _b, _proj, _pixelPerDegree, _image, _mv, _layer);
+            return new YAHOOGrabber(_baseURL, _b, _proj, _pixelPerDegree, _image, _mv, _layer, cache);
         else
-            return new WMSGrabber(_baseURL, _b, _proj, _pixelPerDegree, _image, _mv, _layer);
+            return new WMSGrabber(_baseURL, _b, _proj, _pixelPerDegree, _image, _mv, _layer, cache);
         // OSBGrabber should be rewrite for thread support first
         //if (wmsurl.matches("(?i).*layers=npeoocmap.*") || wmsurl.matches("(?i).*layers=npe.*") ){
Index: applications/editors/josm/plugins/wmsplugin/src/wmsplugin/YAHOOGrabber.java
===================================================================
--- applications/editors/josm/plugins/wmsplugin/src/wmsplugin/YAHOOGrabber.java	(revision 15091)
+++ applications/editors/josm/plugins/wmsplugin/src/wmsplugin/YAHOOGrabber.java	(revision 15185)
@@ -16,15 +16,15 @@
 import org.openstreetmap.josm.data.Bounds;
 import org.openstreetmap.josm.data.projection.Projection;
+import org.openstreetmap.josm.io.CacheFiles;
 import org.openstreetmap.josm.gui.MapView;
 
 
-public class YAHOOGrabber extends WMSGrabber{
+public class YAHOOGrabber extends WMSGrabber {
     protected String browserCmd;
-    protected Cache cache = new wmsplugin.Cache();
 
     YAHOOGrabber(String baseURL, Bounds b, Projection proj,
-            double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) {
+            double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer, CacheFiles cache) {
         super("file:///" + WMSPlugin.getPrefsPath() + "ymap.html?"
-        , b, proj, pixelPerDegree, image, mv, layer);
+        , b, proj, pixelPerDegree, image, mv, layer, cache);
         this.browserCmd = baseURL.replaceFirst("yahoo://", "");
     }
@@ -53,6 +53,8 @@
             throw new IOException( "Could not start browser. Please check that the executable path is correct.\n" + ioe.getMessage() );
         }
-
-        return cache.saveImg(urlstring, ImageIO.read(browser.getInputStream()), true);
+        
+        BufferedImage img = ImageIO.read(browser.getInputStream());
+        cache.saveImg(urlstring, img);
+        return img;
     }
 }
