Changeset 10417 in josm for trunk/src/org


Ignore:
Timestamp:
2016-06-18T15:26:31+02:00 (8 years ago)
Author:
wiktorn
Message:

Fix block cache growing outside the limit.

  • Missing setting of the block size prevented JCS to properly limit of the cache file size when using Block Disk. Set the block size to 4096
  • Block Disk Cache do not reduce the size of the file, when setting the limit lower than the actual file size, so now JOSM will remove all the cache files by itself when limit is set to lower than before value
  • Because of above two, bumped naming of the files to _v2 so older (possibly way above the file size limit) file will be removed

See: #12979, #12986

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCacheManager.java

    r10406 r10417  
    4545    private static long maxObjectTTL = -1;
    4646    private static final String PREFERENCE_PREFIX = "jcs.cache";
    47     private static BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true);
     47    public static BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true);
    4848
    4949    private static final AuxiliaryCacheFactory diskCacheFactory =
     
    203203            BlockDiskCacheAttributes blockAttr = new BlockDiskCacheAttributes();
    204204            blockAttr.setMaxKeySize(maxDiskObjects);
     205            blockAttr.setBlockSizeBytes(4096); // use 4k blocks
    205206            ret = blockAttr;
    206207        } else {
     
    216217            ret.setDiskPath(cachePath);
    217218        }
    218         ret.setCacheName(cacheName + (USE_BLOCK_CACHE.get() ? "_BLOCK" : "_INDEX"));
    219 
    220         removeStaleFiles(cachePath + File.separator + cacheName, (USE_BLOCK_CACHE.get() ? "_INDEX" : "_BLOCK"));
     219        ret.setCacheName(cacheName + (USE_BLOCK_CACHE.get() ? "_BLOCK_v2" : "_INDEX_v2"));
     220
     221        removeStaleFiles(cachePath + File.separator + cacheName, (USE_BLOCK_CACHE.get() ? "_INDEX_v2" : "_BLOCK_v2"));
    221222        return ret;
    222223    }
     
    224225    private static void removeStaleFiles(String basePathPart, String suffix) {
    225226        deleteCacheFiles(basePathPart); // TODO: this can be removed around 2016.09
     227        deleteCacheFiles(basePathPart + "_BLOCK"); // TODO: this can be removed around 2016.09
     228        deleteCacheFiles(basePathPart + "_INDEX"); // TODO: this can be removed around 2016.09
    226229        deleteCacheFiles(basePathPart + suffix);
    227230    }
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CommonSettingsPanel.java

    r8861 r10417  
    88import java.awt.event.ActionEvent;
    99import java.awt.event.ActionListener;
     10import java.io.File;
     11import java.io.FilenameFilter;
    1012
    1113import javax.swing.JButton;
     
    1820import javax.swing.SpinnerNumberModel;
    1921
     22import org.openstreetmap.josm.data.cache.JCSCacheManager;
    2023import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
    2124import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;
     
    2629import org.openstreetmap.josm.tools.ColorHelper;
    2730import org.openstreetmap.josm.tools.GBC;
     31import org.openstreetmap.josm.tools.Utils;
    2832
    2933/**
     
    129133        boolean restartRequired = false;
    130134        if (!AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().equals(this.maxElementsOnDisk.getValue())) {
     135            if (((Integer)this.maxElementsOnDisk.getValue()) < AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get() &&
     136                    JCSCacheManager.USE_BLOCK_CACHE.get()) {
     137                // reducing size of the cache, this requires deletion of the files
     138                removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
     139            }
    131140            AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.put((Integer) this.maxElementsOnDisk.getValue());
    132141            restartRequired = true;
     
    136145        if (!CachedTileLoaderFactory.PROP_TILECACHE_DIR.get().equals(this.tilecacheDir.getText())) {
    137146            restartRequired = true;
     147            removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get()); // clear old cache directory
    138148            CachedTileLoaderFactory.PROP_TILECACHE_DIR.put(this.tilecacheDir.getText());
    139149        }
     
    146156        return restartRequired;
    147157    }
     158
     159    private void removeCacheFiles(String path) {
     160        File directory = new File(path);
     161        File[] cacheFiles = directory.listFiles(new FilenameFilter() {
     162            @Override
     163            public boolean accept(File dir, String name) {
     164                return name.endsWith(".data") || name.endsWith(".key");
     165            }
     166
     167        });
     168        JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted
     169        for (File cacheFile: cacheFiles) {
     170            Utils.deleteFile(cacheFile);
     171        }
     172    }
    148173}
Note: See TracChangeset for help on using the changeset viewer.