Ignore:
Timestamp:
2020-11-26T22:24:27+01:00 (3 years ago)
Author:
simon04
Message:

see #20141 - ImageProvider: cache rendered SVG images using JCS

This experimental feature is disabled by default. Set the advanced preference jcs.cache.use_image_resource_cache=true to enable. No cache eviction for altered images is implemented at the moment.

File:
1 edited

Legend:

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

    r16401 r17364  
    3131import org.openstreetmap.josm.data.preferences.IntegerProperty;
    3232import org.openstreetmap.josm.spi.preferences.Config;
     33import org.openstreetmap.josm.tools.ImageResource;
    3334import org.openstreetmap.josm.tools.Logging;
    3435import org.openstreetmap.josm.tools.Utils;
     
    4546    private static final String PREFERENCE_PREFIX = "jcs.cache";
    4647    public static final BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true);
     48
     49    /**
     50     * The preference key {@code jcs.cache.use_image_resource_cache} controls the caching mechanism used for {@link ImageResource}.
     51     * If set to {@code true}, a combined memory/disk is used. Otherwise, an in-memory-cache is used.
     52     */
     53    public static final BooleanProperty USE_IMAGE_RESOURCE_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_image_resource_cache", false);
    4754
    4855    private static final AuxiliaryCacheFactory DISK_CACHE_FACTORY =
     
    171178     * @return cache access object
    172179     */
     180    public static <K, V> CacheAccess<K, V> getCache(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
     181        return getCache(cacheName, maxMemoryObjects, maxDiskObjects, cachePath, USE_BLOCK_CACHE.get() ? 4096 : 0);
     182    }
     183
    173184    @SuppressWarnings("unchecked")
    174     public static <K, V> CacheAccess<K, V> getCache(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
     185    private static <K, V> CacheAccess<K, V> getCache(String cacheName, int maxMemoryObjects, int maxDiskObjects,
     186                                                     String cachePath, int blockSizeBytes) {
    175187        CacheAccess<K, V> cacheAccess = JCS.getInstance(cacheName, getCacheAttributes(maxMemoryObjects));
    176188        CompositeCache<K, V> cc = cacheAccess.getCacheControl();
    177189
    178190        if (cachePath != null && cacheDirLock != null) {
    179             IDiskCacheAttributes diskAttributes = getDiskCacheAttributes(maxDiskObjects, cachePath, cacheName);
     191            IDiskCacheAttributes diskAttributes = getDiskCacheAttributes(maxDiskObjects, cachePath, cacheName, blockSizeBytes);
     192            Logging.debug("Setting up cache: {0}", diskAttributes);
    180193            try {
    181194                if (cc.getAuxCaches().length == 0) {
     
    193206
    194207    /**
     208     * Returns a cache for {@link ImageResource}
     209     * @param <K> key type
     210     * @param <V> value type
     211     * @return cache access object
     212     */
     213    public static <K, V> CacheAccess<K, V> getImageResourceCache() {
     214        if (!USE_IMAGE_RESOURCE_CACHE.get()) {
     215            return getCache("images", 16 * 1024, 0, null);
     216        }
     217        String cachePath = new File(Config.getDirs().getCacheDirectory(true), "images").getAbsolutePath();
     218        Logging.warn("Using experimental disk cache {0} for ImageResource", cachePath);
     219        return getCache("images", 16 * 1024, 512 * 1024, cachePath, 1024);
     220    }
     221
     222    /**
    195223     * Close all files to ensure, that all indexes and data are properly written
    196224     */
     
    199227    }
    200228
    201     private static IDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath, String cacheName) {
     229    private static IDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath, String cacheName, int blockSizeBytes) {
    202230        IDiskCacheAttributes ret;
    203         removeStaleFiles(cachePath + File.separator + cacheName, USE_BLOCK_CACHE.get() ? "_INDEX_v2" : "_BLOCK_v2");
    204         String newCacheName = cacheName + (USE_BLOCK_CACHE.get() ? "_BLOCK_v2" : "_INDEX_v2");
    205 
    206         if (USE_BLOCK_CACHE.get()) {
     231        boolean isBlockDiskCache = blockSizeBytes > 0;
     232        removeStaleFiles(cachePath + File.separator + cacheName, isBlockDiskCache ? "_INDEX_v2" : "_BLOCK_v2");
     233        String newCacheName = cacheName + (isBlockDiskCache ? "_BLOCK_v2" : "_INDEX_v2");
     234
     235        if (isBlockDiskCache) {
    207236            BlockDiskCacheAttributes blockAttr = new BlockDiskCacheAttributes();
    208237            /*
     
    218247                blockAttr.setMaxKeySize(maxDiskObjects);
    219248            }
    220             blockAttr.setBlockSizeBytes(4096); // use 4k blocks
     249            blockAttr.setBlockSizeBytes(blockSizeBytes);
    221250            ret = blockAttr;
    222251        } else {
Note: See TracChangeset for help on using the changeset viewer.