Class ResourceCache

java.lang.Object
net.targetr.wtm3.cache.ResourceCache
Direct Known Subclasses:
ClassPathResourceCache, SpeedTestResourceCache, ZipFileResourceCache

public abstract class ResourceCache extends Object
Caches resources on the classpath in memory.
Author:
Dr Michael Gardiner
  • Constructor Details

    • ResourceCache

      public ResourceCache(int maxCacheEntries, long maxCacheSizeBytes)
      Constructs a ResourceCache with specified maximum entries and size limits.
      Parameters:
      maxCacheEntries - the maximum number of top-level entries allowed before eviction.
      maxCacheSizeBytes - the maximum size in bytes of all entries combined before eviction.
  • Method Details

    • get

      public ResourceCache.CacheEntry get(String path) throws IOException
      Get a resource quickly by using cache if possible.
      Parameters:
      path - the path to the resource in the class path.
      Returns:
      CacheEntry containing byte array of resource data.
      Throws:
      IOException - An exception occurred while reading the resource from class path.
    • openInputStream

      protected abstract InputStream openInputStream(String path) throws IOException
      Opens an input stream to the resource.
      Parameters:
      path - the path to the resource in the class path.
      Returns:
      an input stream to the resource, or null if not found.
      Throws:
      IOException - if an I/O error occurs.
    • contains

      public boolean contains(String path)
      Check if a resource is in the classpath.
      Parameters:
      path - the path to the resource in the class path.
      Returns:
      true if the resource is in the classpath, false otherwise.
    • invalidate

      public void invalidate()
      Invalidate the cache. All items in the cache will be removed.