Class DiskCache

java.lang.Object
net.jalbum.util.DiskCache

public class DiskCache extends Object
Manage the caching of remotely fetched URLs in a local directory (the primary cache dir) Also synchronizes a target cache dir with the local cache dir. No longer cached files are deleted from the primary cache dir and target cache dir
Since:
29
  • Constructor Details

    • DiskCache

      public DiskCache(File primaryCacheDir)
      Parameters:
      primaryCacheDir - - Local directory to store cached files in Uses ".diskcache.json" file to store local database of managed files Only managed files are copied and deleted
    • DiskCache

      public DiskCache(File primaryCacheDir, String dbName)
      Parameters:
      primaryCacheDir - - Local directory to store cached files in
      dbName - - Name of database file that keeps track of managed files (json format). Only managed files are copied and deleted
  • Method Details

    • onError

      public void onError(Consumer<DiskCache.DCException> errorHandler)
    • add

      public DiskCache add(String url)
      Add a file to be loaded into the cache. The actual loading is performed by refresh()
      Parameters:
      url - URL of file as a String
      Returns:
      this DiskCache object (for chaining add calls)
    • add

      public DiskCache add(URL url)
      Add a file to be loaded into the cache. The actual loading is performed by refresh()
      Parameters:
      url - URL of file as a URL object
      Returns:
      this DiskCache object (for chaining add calls)
    • add

      public DiskCache add(URL url, String localName)
      Add a file to be loaded into the cache.The actual loading is performed by refresh()
      Parameters:
      url - URL of file as a URL object
      localName - Alternative local name to use
      Returns:
      this DiskCache object (for chaining add calls)
    • add

      public DiskCache add(File file)
      Add a file to be loaded into the cache. The actual loading is performed by refresh()
      Parameters:
      file - File to add
      Returns:
      this DiskCache object (for chaining add calls)
    • add

      public DiskCache add(File file, String localName)
      Add a file to be loaded into the cache.The actual loading is performed by refresh()
      Parameters:
      file - File to add
      localName - Alternative local name to use
      Returns:
      this DiskCache object (for chaining add calls)
    • size

      public int size()
      Returns:
      Number of added files
    • clear

      public void clear()
      Clear list of files to be added
    • refresh

      public void refresh() throws DiskCache.DCException
      Load files that have been added by the add() calls into the primary cache dir.Older files present in local cache that aren't added are deleted from primary local cache. Files are only loaded if missing from local cache or in need of update. Files are loaded simultaneously in order to speed up loading.
      Throws:
      DiskCache.DCException - In case a file couldn't be loaded that was also missing in the local cache
    • refresh

      public void refresh(boolean abortOnError) throws OperationAbortedException, DiskCache.DCException
      Like refresh(), but will throw an OperationAbortedException on error
      Parameters:
      abortOnError - set to true in order to abort on error
      Throws:
      DiskCache.DCException - In case a file couldn't be loaded that was also missing in the local cache
      OperationAbortedException
    • getLastException

      public DiskCache.DCException getLastException()
      Returns:
      Last DCException from refresh() call or null if no error occurred.
    • getContents

      public Map<String,File> getContents()
      Returns:
      contents of primary cache dir
    • syncTo

      public void syncTo(File targetDir) throws IOException
      Synchronize targetDir with primary cache dir (Copy updated files and delete stale files)
      Parameters:
      targetDir -
      Throws:
      IOException