Changeset 14295 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2018-10-04T19:06:23+02:00 (6 years ago)
Author:
wiktorn
Message:

Always check if tile cache files are oversized

If tile cache file is oversized, remove it. When removing one file (.data or
.key) remove also the other file (resp. .key or .data).

Notify user about the need of the restart only, when there was something
deleted

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CommonSettingsPanel.java

    r11873 r14295  
    8989
    9090        boolean restartRequired = false;
     91        restartRequired |= removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(), 1024L * 1024L * ((Integer) this.maxElementsOnDisk.getValue()));
     92
    9193        if (!AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().equals(this.maxElementsOnDisk.getValue())) {
    92             if (((Integer) this.maxElementsOnDisk.getValue()) < AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get() &&
    93                     JCSCacheManager.USE_BLOCK_CACHE.get()) {
    94                 // reducing size of the cache, this requires deletion of the files
    95                 removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
    96             }
    9794            AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.put((Integer) this.maxElementsOnDisk.getValue());
    9895            restartRequired = true;
     
    10299        if (!CachedTileLoaderFactory.PROP_TILECACHE_DIR.get().equals(this.tilecacheDir.getText())) {
    103100            restartRequired = true;
    104             removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get()); // clear old cache directory
     101            restartRequired |= removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(), 0); // clear old cache directory
    105102            CachedTileLoaderFactory.PROP_TILECACHE_DIR.put(this.tilecacheDir.getText());
    106103        }
     
    114111    }
    115112
    116     private static void removeCacheFiles(String path) {
     113    private static boolean removeCacheFiles(String path, long maxSize) {
     114
    117115        File directory = new File(path);
    118116        File[] cacheFiles = directory.listFiles((FilenameFilter) (dir, name) -> name.endsWith(".data") || name.endsWith(".key"));
    119         JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted
     117        boolean restartRequired = false;
    120118        if (cacheFiles != null) {
    121119            for (File cacheFile: cacheFiles) {
    122                 Utils.deleteFile(cacheFile);
     120                if (cacheFile.length() > maxSize) {
     121                    if (!restartRequired) {
     122                        JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted
     123                        restartRequired = true;
     124                    }
     125                    Utils.deleteFile(cacheFile);
     126                    File otherFile = null;
     127                    if (cacheFile.getName().endsWith(".data")) {
     128                        otherFile = new File(cacheFile.getPath().replaceAll("\\.data$", ".key"));
     129                    } else if (cacheFile.getName().endsWith(".key")) {
     130                        otherFile = new File(cacheFile.getPath().replaceAll("\\.key$", ".data"));
     131                    }
     132                    if (otherFile != null) {
     133                        Utils.deleteFileIfExists(otherFile);
     134                    }
     135                }
    123136            }
    124137        }
     138        return restartRequired;
    125139    }
    126140}
Note: See TracChangeset for help on using the changeset viewer.