Class FileSystemTileCache

  • All Implemented Interfaces:
    TileCache, ObservableInterface

    public class FileSystemTileCache
    extends java.lang.Object
    implements TileCache
    A thread-safe cache for image files with a fixed size and LRU policy.

    A FileSystemTileCache caches tiles in a dedicated path in the file system, specified in the constructor.

    When used for a TileRendererLayer, persistent caching may result in clipped labels when tiles from different instances are used. To work around this, either display labels in a separate LabelLayer (experimental) or disable persistence as described in FileSystemTileCache(int, File, GraphicFactory, boolean).

    Note: previously the FileSystemTileCache utilized threading to speed up response times. This is not the case anymore and the constructors have been removed.

    • Constructor Summary

      Constructors 
      Constructor Description
      FileSystemTileCache​(int capacity, java.io.File cacheDirectory, org.mapsforge.core.graphics.GraphicFactory graphicFactory)
      Compatibility constructor that creates a non-threaded, non-persistent FSTC.
      FileSystemTileCache​(int capacity, java.io.File cacheDirectory, org.mapsforge.core.graphics.GraphicFactory graphicFactory, boolean persistent)
      Creates a new FileSystemTileCache.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addObserver​(Observer observer)  
      boolean containsKey​(Job key)  
      void destroy()
      Destroys this cache.
      org.mapsforge.core.graphics.TileBitmap get​(Job key)  
      int getCapacity()  
      int getCapacityFirstLevel()  
      org.mapsforge.core.graphics.TileBitmap getImmediately​(Job key)
      Returns tileBitmap only if available at fastest cache in case of multi-layered cache, null otherwise.
      boolean isPersistent()
      Whether the cache is persistent.
      void purge()
      Purges this cache.
      void put​(Job key, org.mapsforge.core.graphics.TileBitmap bitmap)  
      void removeObserver​(Observer observer)  
      void setWorkingSet​(java.util.Set<Job> workingSet)
      Reserves a working set in this cache, for multi-level caches this means bringing the elements in workingSet into the fastest cache.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileSystemTileCache

        public FileSystemTileCache​(int capacity,
                                   java.io.File cacheDirectory,
                                   org.mapsforge.core.graphics.GraphicFactory graphicFactory)
        Compatibility constructor that creates a non-threaded, non-persistent FSTC.
        Parameters:
        capacity - the maximum number of entries in this cache.
        cacheDirectory - the directory where cached tiles will be stored.
        graphicFactory - the graphicFactory implementation to use.
        Throws:
        java.lang.IllegalArgumentException - if the capacity is negative.
      • FileSystemTileCache

        public FileSystemTileCache​(int capacity,
                                   java.io.File cacheDirectory,
                                   org.mapsforge.core.graphics.GraphicFactory graphicFactory,
                                   boolean persistent)
        Creates a new FileSystemTileCache.

        Use the persistent argument to specify whether cache contents should be kept across instances. A persistent cache will serve any tiles it finds in cacheDirectory. Calling destroy() on a persistent cache will not delete the cache directory. Conversely, a non-persistent cache will serve only tiles added to it via the put(Job, TileBitmap) method, and calling destroy() on a non-persistent cache will delete cacheDirectory.

        Parameters:
        capacity - the maximum number of entries in this cache.
        cacheDirectory - the directory where cached tiles will be stored.
        graphicFactory - the graphicFactory implementation to use.
        persistent - if cache data will be kept between instances
        Throws:
        java.lang.IllegalArgumentException - if the capacity is negative.
        java.lang.IllegalArgumentException - if the capacity is negative.
    • Method Detail

      • containsKey

        public boolean containsKey​(Job key)
        Specified by:
        containsKey in interface TileCache
        Returns:
        true if this cache contains an image for the given key, false otherwise.
        See Also:
        Map.containsKey(java.lang.Object)
      • destroy

        public void destroy()
        Destroys this cache.

        Applications are expected to call this method when they no longer require the cache.

        If the cache is not persistent, calling this method is equivalent to calling purge(). If the cache is persistent, it does nothing.

        Beginning with 0.5.1, accessing the cache after calling destroy() is discouraged. In order to empty the cache and force all tiles to be re-rendered or re-requested from the source, use purge() instead. Earlier versions lacked the purge() method and used destroy() instead, but this practice is now discouraged and may lead to unexpected results when used with features introduced in 0.5.1 or later.

        Specified by:
        destroy in interface TileCache
      • get

        public org.mapsforge.core.graphics.TileBitmap get​(Job key)
        Specified by:
        get in interface TileCache
        Returns:
        the image for the given key or null, if this cache contains no image for the key.
        See Also:
        Map.get(java.lang.Object)
      • getCapacity

        public int getCapacity()
        Specified by:
        getCapacity in interface TileCache
        Returns:
        the capacity of this cache.
      • getCapacityFirstLevel

        public int getCapacityFirstLevel()
        Specified by:
        getCapacityFirstLevel in interface TileCache
        Returns:
        the capacity of the first level of a multi-level cache.
      • getImmediately

        public org.mapsforge.core.graphics.TileBitmap getImmediately​(Job key)
        Description copied from interface: TileCache
        Returns tileBitmap only if available at fastest cache in case of multi-layered cache, null otherwise.
        Specified by:
        getImmediately in interface TileCache
        Returns:
        tileBitmap if available without getting from lower storage levels
      • isPersistent

        public boolean isPersistent()
        Whether the cache is persistent.
      • purge

        public void purge()
        Purges this cache.

        Calls to get(Job) issued after purging will not return any tiles added before the purge operation. Purging will also delete the cache directory on disk, freeing up disk space.

        Applications should purge the tile cache when map model parameters change, such as the render style for locally rendered tiles, or the source for downloaded tiles. Applications which frequently alternate between a limited number of map model configurations may want to consider using a different cache for each.

        Specified by:
        purge in interface TileCache
        Since:
        0.5.1
      • put

        public void put​(Job key,
                        org.mapsforge.core.graphics.TileBitmap bitmap)
        Specified by:
        put in interface TileCache
        See Also:
        Map.put(K, V)
      • setWorkingSet

        public void setWorkingSet​(java.util.Set<Job> workingSet)
        Description copied from interface: TileCache
        Reserves a working set in this cache, for multi-level caches this means bringing the elements in workingSet into the fastest cache.
        Specified by:
        setWorkingSet in interface TileCache