Class CachedFileAccess
java.lang.Object
dev.jcputney.elearning.parser.impl.access.CachedFileAccess
- All Implemented Interfaces:
FileAccess
A decorator implementation of
FileAccess that adds caching capability to any
FileAccess implementation. This class caches the results of file existence checks, file
listings, and file contents to improve performance for frequently accessed resources.
The cache uses a ConcurrentHashMap to ensure thread safety for concurrent access.
For file contents, the entire file is read into memory and stored as a byte array to allow for
multiple reads without reopening the file.
This implementation is particularly useful for scenarios where the same files are accessed repeatedly, such as during module parsing.
-
Constructor Summary
ConstructorsConstructorDescriptionCachedFileAccess(FileAccess delegate) Constructs a newCachedFileAccessinstance that wraps the specifiedFileAccessimplementation. -
Method Summary
Modifier and TypeMethodDescriptionvoidClears all caches, forcing subsequent calls to retrieve fresh data from the delegate.voidclearCache(String path) Clears the cache for a specific path, forcing subsequent calls for that path to retrieve fresh data from the delegate.booleanfileExistsInternal(String path) Checks if a file exists at the given path, using the cache if available.Gets cache statistics for monitoring.Retrieves the contents of a file as an InputStream, using the cache if available.Gets the root path from the delegateFileAccessimplementation.longGets the total size of all files by delegating to the underlying FileAccess implementation.listFilesInternal(String directoryPath) Lists all files within a specified directory path, using the cache if available.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.jcputney.elearning.parser.api.FileAccess
clearCaches, fileExists, fileExistsBatch, fullPath, getAllFiles, getFileContents, listFiles, prefetchCommonFiles
-
Constructor Details
-
CachedFileAccess
Constructs a newCachedFileAccessinstance that wraps the specifiedFileAccessimplementation.- Parameters:
delegate- TheFileAccessimplementation to delegate to.- Throws:
IllegalArgumentException- if the delegate is null
-
-
Method Details
-
getRootPath
Gets the root path from the delegateFileAccessimplementation.- Specified by:
getRootPathin interfaceFileAccess- Returns:
- The root path.
-
fileExistsInternal
Checks if a file exists at the given path, using the cache if available.- Specified by:
fileExistsInternalin interfaceFileAccess- Parameters:
path- The path to check (guaranteed to be non-null).- Returns:
- True if the file exists, false otherwise.
-
listFilesInternal
Lists all files within a specified directory path, using the cache if available.- Specified by:
listFilesInternalin interfaceFileAccess- Parameters:
directoryPath- The directory to list files from (guaranteed to be non-null).- Returns:
- A list of file paths within the directory.
- Throws:
IOException- if there's an error accessing the directory or listing its contents.
-
getFileContentsInternal
Retrieves the contents of a file as an InputStream, using the cache if available. The file contents are read fully into memory on the first access and cached as a byte array. Subsequent calls return a new ByteArrayInputStream wrapping the cached byte array.- Specified by:
getFileContentsInternalin interfaceFileAccess- Parameters:
path- The path to retrieve contents from (guaranteed to be non-null).- Returns:
- An InputStream of the file contents.
- Throws:
IOException- if the file can't be read.
-
clearCache
public void clearCache()Clears all caches, forcing subsequent calls to retrieve fresh data from the delegate. -
clearCache
Clears the cache for a specific path, forcing subsequent calls for that path to retrieve fresh data from the delegate.- Parameters:
path- The path to clear from the cache.- Throws:
IllegalArgumentException- if a path is null
-
getTotalSize
Gets the total size of all files by delegating to the underlying FileAccess implementation.Note: This method is not cached as file sizes might change between calls.
- Specified by:
getTotalSizein interfaceFileAccess- Returns:
- Total size of all files in bytes, or -1 if not supported
- Throws:
IOException- if there's an error accessing file sizes
-
getCacheStatistics
Gets cache statistics for monitoring.- Returns:
- map containing cache hits, misses, and hit ratio
-