| | 1 | package wmsplugin; |
| | 2 | |
| | 3 | import java.awt.image.BufferedImage; |
| | 4 | import java.io.File; |
| | 5 | import java.io.FileFilter; |
| | 6 | import java.util.Date; |
| | 7 | import java.util.Iterator; |
| | 8 | import java.util.Set; |
| | 9 | import java.util.TreeMap; |
| | 10 | import javax.imageio.*; |
| | 11 | |
| | 12 | import org.openstreetmap.josm.Main; |
| | 13 | |
| | 14 | |
| | 15 | public class Cache { |
| | 16 | final File dir; |
| | 17 | // Last 5 days |
| | 18 | private final long expire = Main.pref.getInteger("wmsplugin.cache.expire", 1000*60*60*24*5); |
| | 19 | // 40 MBytes |
| | 20 | private final long maxsize = Main.pref.getInteger("wmsplugin.cache.size", 1000*1000*40); |
| | 21 | private boolean disabled = false; |
| | 22 | // If the cache is full, we don't want to delete just one file |
| | 23 | private final int cleanUpThreshold = 20; |
| | 24 | |
| | 25 | Cache() { |
| | 26 | this(Main.pref.getPreferencesDir() + "plugins/wmsplugin/cache"); |
| | 27 | } |
| | 28 | |
| | 29 | Cache(String working_dir) { |
| | 30 | // Override default working directory |
| | 31 | this.dir = new File(working_dir); |
| | 32 | try { |
| | 33 | this.dir.mkdirs(); |
| | 34 | } catch(Exception e) { |
| | 35 | // We probably won't be able to do anything anyway |
| | 36 | disabled = true; |
| | 37 | } |
| | 38 | |
| | 39 | if(expire <= 0 || maxsize <= 0) |
| | 40 | disabled = true; |
| | 41 | } |
| | 42 | |
| | 43 | public BufferedImage getImg(String ident) { |
| | 44 | if(disabled) return null; |
| | 45 | try { |
| | 46 | File img = getPath(ident); |
| | 47 | if(!img.exists()) { |
| | 48 | //System.out.println("Miss"); |
| | 49 | return null; |
| | 50 | } |
| | 51 | if(img.lastModified() < (new Date().getTime() - expire)) { |
| | 52 | img.delete(); |
| | 53 | //System.out.println("Miss"); |
| | 54 | return null; |
| | 55 | } |
| | 56 | //System.out.println("Hit"); |
| | 57 | return ImageIO.read(img); |
| | 58 | } catch(Exception e) { |
| | 59 | System.out.println(e.getMessage()); |
| | 60 | } |
| | 61 | //System.out.println("Miss"); |
| | 62 | return null; |
| | 63 | } |
| | 64 | |
| | 65 | public void saveImg(String ident, BufferedImage image) { |
| | 66 | if(disabled) return; |
| | 67 | try { |
| | 68 | ImageIO.write(image, "png", getPath(ident)); |
| | 69 | } catch(Exception e){ |
| | 70 | System.out.println(e.getMessage()); |
| | 71 | } |
| | 72 | |
| | 73 | // Clean up must be called manually |
| | 74 | } |
| | 75 | |
| | 76 | public BufferedImage saveImg(String ident, BufferedImage image, boolean passThrough) { |
| | 77 | saveImg(ident, image); |
| | 78 | return image; |
| | 79 | } |
| | 80 | |
| | 81 | public void cleanUp() { |
| | 82 | if(disabled) return; |
| | 83 | |
| | 84 | TreeMap modtime = new TreeMap<Long, File>(); |
| | 85 | long time = new Date().getTime() - expire; |
| | 86 | long dirsize = 0; |
| | 87 | |
| | 88 | for(File f : getFiles()) { |
| | 89 | if(f.lastModified() < time) |
| | 90 | f.delete(); |
| | 91 | else { |
| | 92 | dirsize += f.length(); |
| | 93 | modtime.put(f.lastModified(), f); |
| | 94 | } |
| | 95 | } |
| | 96 | |
| | 97 | if(dirsize < maxsize) return; |
| | 98 | |
| | 99 | Set keySet = modtime.keySet(); |
| | 100 | Iterator it = keySet.iterator(); |
| | 101 | int i=0; |
| | 102 | while (it.hasNext()) { |
| | 103 | i++; |
| | 104 | ((File)modtime.get(it.next())).delete(); |
| | 105 | |
| | 106 | // Delete a couple of files, then check again |
| | 107 | if(i % cleanUpThreshold == 0 && getDirSize() < maxsize) |
| | 108 | return; |
| | 109 | } |
| | 110 | } |
| | 111 | |
| | 112 | private long getDirSize() { |
| | 113 | long dirsize = 0; |
| | 114 | |
| | 115 | for(File f : getFiles()) |
| | 116 | dirsize += f.length(); |
| | 117 | return dirsize; |
| | 118 | } |
| | 119 | |
| | 120 | private File[] getFiles() { |
| | 121 | return dir.listFiles( |
| | 122 | new FileFilter() { |
| | 123 | public boolean accept(File file) { |
| | 124 | return file.getName().endsWith(".png"); |
| | 125 | } |
| | 126 | } |
| | 127 | ); |
| | 128 | } |
| | 129 | |
| | 130 | private String clean(String ident) { |
| | 131 | return ident.replaceAll("[^a-zA-Z0-9]", ""); |
| | 132 | } |
| | 133 | |
| | 134 | private File getPath(String ident) { |
| | 135 | return new File(dir, clean(ident) + ".png"); |
| | 136 | } |
| | 137 | } |
| | 138 | No newline at end of file |