Ticket #3289: cachefiles.patch

File cachefiles.patch, 5.7 KB (added by xeen, 17 years ago)
  • src/org/openstreetmap/josm/io/CacheFiles.java

     
    5454     * @param ident
    5555     */
    5656    public CacheFiles(String ident) {
    57        String pref = Main.pref.getPluginsDirFile().getPath();
    58        boolean dir_writeable;
    59        this.ident = ident;
    60        String cacheDir = Main.pref.get("cache." + ident + "." + "path", pref + "/" + ident + "/cache/");
    61        this.dir = new File(cacheDir);
    62        try {
    63            this.dir.mkdirs();
    64            dir_writeable = true;
     57        String pref = Main.pref.getPluginsDirFile().getPath();
     58        boolean dir_writeable;
     59        this.ident = ident;
     60        String cacheDir = Main.pref.get("cache." + ident + "." + "path", pref + "/" + ident + "/cache/");
     61        this.dir = new File(cacheDir);
     62        try {
     63            this.dir.mkdirs();
     64            dir_writeable = true;
    6565        } catch(Exception e) {
    66            // We have no access to this directory, so don't to anything
    67            dir_writeable = false;
     66            // We have no access to this directory, so don't do anything
     67            dir_writeable = false;
    6868        }
    6969        this.enabled = dir_writeable;
    7070        this.expire = Main.pref.getLong("cache." + ident + "." + "expire", EXPIRE_DAILY);
    71         if(this.expire < 0)
     71        if(this.expire < 0) {
    7272            this.expire = CacheFiles.EXPIRE_NEVER;
     73        }
    7374        this.maxsize = Main.pref.getLong("cache." + ident + "." + "maxsize", 50);
    74         if(this.maxsize < 0)
     75        if(this.maxsize < 0) {
    7576            this.maxsize = -1;
     77        }
    7678    }
    7779
    7880    /**
     
    9395            }
    9496
    9597            // Update last mod time so we don't expire recently used data
    96             if(updateModTime)
     98            if(updateModTime) {
    9799                data.setLastModified(new Date().getTime());
     100            }
    98101
    99102            byte[] bytes = new byte[(int) data.length()];
    100103            new RandomAccessFile(data, "r").readFully(bytes);
     
    114117        if(!enabled) return;
    115118        try {
    116119            File f = getPath(ident);
    117             if(f.exists())
     120            if(f.exists()) {
    118121                f.delete();
     122            }
    119123            // rws also updates the file meta-data, i.e. last mod time
    120124            new RandomAccessFile(f, "rws").write(data);
    121125        } catch(Exception e){
     
    143147                return null;
    144148            }
    145149            // Update last mod time so we don't expire recently used images
    146             if(updateModTime)
     150            if(updateModTime) {
    147151                img.setLastModified(new Date().getTime());
     152            }
    148153            return ImageIO.read(img);
    149154        } catch(Exception e) {
    150155            System.out.println(e.getMessage());
     
    176181     * @param force will also write it to the preferences
    177182     */
    178183    public void setExpire(int amount, boolean force) {
    179         if(amount < 0)
    180             this.expire = EXPIRE_NEVER;
    181         else
    182             this.expire = amount;
    183184        String key = "cache." + ident + "." + "expire";
    184         if(force || !Main.pref.hasKey(key))
    185             Main.pref.putLong(key, this.expire);
     185        if(Main.pref.hasKey(key) && !force)
     186            return;
     187
     188        this.expire = amount > 0 ? amount : EXPIRE_NEVER;
     189        Main.pref.putLong(key, this.expire);
    186190    }
    187191
    188192    /**
     
    191195     * @param force will also write it to the preferences
    192196     */
    193197    public void setMaxSize(int amount, boolean force) {
    194         this.maxsize = amount > 0 ? amount : -1;
    195198        String key = "cache." + ident + "." + "maxsize";
    196         if(force || !Main.pref.hasKey(key))
    197             Main.pref.putLong(key, this.maxsize);
     199        if(Main.pref.hasKey(key) && !force)
     200            return;
     201
     202        this.maxsize = amount > 0 ? amount : -1;
     203        Main.pref.putLong(key, this.maxsize);
    198204    }
    199205
    200206    /**
     
    210216     * Checks if a clean up is needed and will do so if necessary
    211217     */
    212218    public void checkCleanUp() {
    213         if(this.writes > this.cleanUpInterval)
     219        if(this.writes > this.cleanUpInterval) {
    214220            cleanUp();
     221        }
    215222    }
    216223
    217224    /**
     
    224231        long dirsize = 0;
    225232
    226233        for(File f : dir.listFiles()) {
    227             if(isExpired(f))
     234            if(isExpired(f)) {
    228235                f.delete();
    229             else {
     236            } else {
    230237                dirsize += f.length();
    231238                modtime.put(f.lastModified(), f);
    232239            }
     
    258265     */
    259266    public void customCleanUp(int type, int size) {
    260267        switch(type) {
    261             case CLEAN_ALL:
    262                 for(File f : dir.listFiles())
     268        case CLEAN_ALL:
     269            for(File f : dir.listFiles()) {
     270                f.delete();
     271            }
     272            break;
     273        case CLEAN_SMALL_FILES:
     274            for(File f: dir.listFiles())
     275                if(f.length() < size) {
    263276                    f.delete();
    264                 break;
    265             case CLEAN_SMALL_FILES:
    266                 for(File f: dir.listFiles())
    267                     if(f.length() < size)
    268                         f.delete();
    269                 break;
    270             case CLEAN_BY_DATE:
    271                 cleanUp();
    272                 break;
     277                }
     278            break;
     279        case CLEAN_BY_DATE:
     280            cleanUp();
     281            break;
    273282        }
    274283    }
    275284
     
    281290        if(!enabled) return -1;
    282291        long dirsize = 0;
    283292
    284         for(File f : this.dir.listFiles())
     293        for(File f : this.dir.listFiles()) {
    285294            dirsize += f.length();
     295        }
    286296        return dirsize;
    287297    }
    288298