Class ResourceCache

java.lang.Object
net.targetr.wtm3.cache.ResourceCache

public class ResourceCache extends Object
Caches resource bytes, ETags and missing-resource results from supplied resource loaders. Entries are keyed by the loader's hash code and the resource path.
Author:
Dr Michael Gardiner
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Identifies a resource by its source and path.
    static class 
    Represents a single entry in the resource cache.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final AtomicLong
    Current size in bytes of all cached resources
    protected int
    Entry-count threshold that triggers eviction from this cache when exceeded.
    protected long
    Byte-count threshold for this cache that triggers eviction when exceeded.
    protected int
    Maximum size in bytes of a single resource to be loaded and cached.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ResourceCache(int maxCacheEntries, long maxCacheSizeBytes, int maxResourceSizeBytes)
    Constructs a ResourceCache with the specified entry-count and size limits.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes the least recently accessed quarter of the entries when this cache's entry-count or byte-count threshold is exceeded.
    get(String path, ResourceLoader loader)
    Gets a cached resource or loads it through the supplied loader.
    Reports this cache's limits, usage totals and resource details by path.
    void
    Invalidate the cache.
    void
    Removes cached resource with the specified key.
    void
    setCacheLimits(int maxCacheEntries, long maxCacheSizeBytes, int maxResourceSizeBytes)
    Updates the cache limits used during subsequent resource loads.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • maxCacheEntries

      protected int maxCacheEntries
      Entry-count threshold that triggers eviction from this cache when exceeded.
    • maxCacheSizeBytes

      protected long maxCacheSizeBytes
      Byte-count threshold for this cache that triggers eviction when exceeded.
    • maxResourceSizeBytes

      protected int maxResourceSizeBytes
      Maximum size in bytes of a single resource to be loaded and cached.
    • cacheSizeBytes

      protected final AtomicLong cacheSizeBytes
      Current size in bytes of all cached resources
  • Constructor Details

    • ResourceCache

      public ResourceCache(int maxCacheEntries, long maxCacheSizeBytes, int maxResourceSizeBytes)
      Constructs a ResourceCache with the specified entry-count and size limits.
      Parameters:
      maxCacheEntries - entry-count threshold that triggers eviction, including cached misses
      maxCacheSizeBytes - byte-count threshold for this cache that triggers eviction
      maxResourceSizeBytes - maximum size in bytes of a single resource to be loaded and cached
  • Method Details

    • setCacheLimits

      public void setCacheLimits(int maxCacheEntries, long maxCacheSizeBytes, int maxResourceSizeBytes)
      Updates the cache limits used during subsequent resource loads.
      Parameters:
      maxCacheEntries - entry-count threshold that triggers eviction from this cache
      maxCacheSizeBytes - byte-count threshold for this cache that triggers eviction
      maxResourceSizeBytes - maximum size in bytes of a single resource to be loaded and cached
    • get

      public ResourceCache.CacheValue get(String path, ResourceLoader loader) throws IOException
      Gets a cached resource or loads it through the supplied loader.
      Parameters:
      path - the resource path understood by the loader
      loader - the resource source whose hash code selects the cache namespace
      Returns:
      the shared cache entry containing resource data
      Throws:
      FileNotFoundException - if the resource is absent, including a cached miss
      IOException - if reading fails or the resource exceeds the size limit
    • cleanupCacheSize

      public void cleanupCacheSize()
      Removes the least recently accessed quarter of the entries when this cache's entry-count or byte-count threshold is exceeded.
    • invalidate

      public void invalidate()
      Invalidate the cache. Removes this cache's entries.
    • remove

      public void remove(ResourceCache.CacheKey key)
      Removes cached resource with the specified key.
      Parameters:
      key - the key of the resource to remove
    • getState

      public Map<String,Object> getState()
      Reports this cache's limits, usage totals and resource details by path.
      Returns:
      a map containing cache limits, usage totals and resource details