Package net.targetr.wtm3.cache
Class ResourceCache
java.lang.Object
net.targetr.wtm3.cache.ResourceCache
- Direct Known Subclasses:
ClassPathResourceCache,SpeedTestResourceCache,ZipFileResourceCache
Caches resources on the classpath in memory.
- Author:
- Dr Michael Gardiner
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents a single entry in the resource cache. -
Constructor Summary
ConstructorsConstructorDescriptionResourceCache(int maxCacheEntries, long maxCacheSizeBytes) Constructs a ResourceCache with specified maximum entries and size limits. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCheck if a resource is in the classpath.Get a resource quickly by using cache if possible.voidInvalidate the cache.protected abstract InputStreamopenInputStream(String path) Opens an input stream to the resource.
-
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
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
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
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.
-