Package net.targetr.wtm3.cache
Class ResourceCache
java.lang.Object
net.targetr.wtm3.cache.ResourceCache
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 ClassesModifier and TypeClassDescriptionstatic final recordIdentifies a resource by its source and path.static classRepresents a single entry in the resource cache. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final AtomicLongCurrent size in bytes of all cached resourcesprotected intEntry-count threshold that triggers eviction from this cache when exceeded.protected longByte-count threshold for this cache that triggers eviction when exceeded.protected intMaximum size in bytes of a single resource to be loaded and cached. -
Constructor Summary
ConstructorsConstructorDescriptionResourceCache(int maxCacheEntries, long maxCacheSizeBytes, int maxResourceSizeBytes) Constructs a ResourceCache with the specified entry-count and size limits. -
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves 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.getState()Reports this cache's limits, usage totals and resource details by path.voidInvalidate the cache.voidRemoves cached resource with the specified key.voidsetCacheLimits(int maxCacheEntries, long maxCacheSizeBytes, int maxResourceSizeBytes) Updates the cache limits used during subsequent resource loads.
-
Field Details
-
maxCacheEntries
protected int maxCacheEntriesEntry-count threshold that triggers eviction from this cache when exceeded. -
maxCacheSizeBytes
protected long maxCacheSizeBytesByte-count threshold for this cache that triggers eviction when exceeded. -
maxResourceSizeBytes
protected int maxResourceSizeBytesMaximum size in bytes of a single resource to be loaded and cached. -
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 missesmaxCacheSizeBytes- byte-count threshold for this cache that triggers evictionmaxResourceSizeBytes- 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 cachemaxCacheSizeBytes- byte-count threshold for this cache that triggers evictionmaxResourceSizeBytes- maximum size in bytes of a single resource to be loaded and cached
-
get
Gets a cached resource or loads it through the supplied loader.- Parameters:
path- the resource path understood by the loaderloader- 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 missIOException- 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
Removes cached resource with the specified key.- Parameters:
key- the key of the resource to remove
-
getState
Reports this cache's limits, usage totals and resource details by path.- Returns:
- a map containing cache limits, usage totals and resource details
-