Class CachedFileAccess

java.lang.Object
dev.jcputney.elearning.parser.impl.access.CachedFileAccess
All Implemented Interfaces:
FileAccess

public final class CachedFileAccess extends Object implements 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 Details

  • Method Details

    • getRootPath

      public String getRootPath()
      Gets the root path from the delegate FileAccess implementation.
      Specified by:
      getRootPath in interface FileAccess
      Returns:
      The root path.
    • fileExistsInternal

      public boolean fileExistsInternal(String path)
      Checks if a file exists at the given path, using the cache if available.
      Specified by:
      fileExistsInternal in interface FileAccess
      Parameters:
      path - The path to check (guaranteed to be non-null).
      Returns:
      True if the file exists, false otherwise.
    • listFilesInternal

      public List<String> listFilesInternal(String directoryPath) throws IOException
      Lists all files within a specified directory path, using the cache if available.
      Specified by:
      listFilesInternal in interface FileAccess
      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

      public InputStream getFileContentsInternal(String path) throws IOException
      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:
      getFileContentsInternal in interface FileAccess
      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

      public void clearCache(String path)
      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

      public long getTotalSize() throws IOException
      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:
      getTotalSize in interface FileAccess
      Returns:
      Total size of all files in bytes, or -1 if not supported
      Throws:
      IOException - if there's an error accessing file sizes
    • getCacheStatistics

      public Map<String,Object> getCacheStatistics()
      Gets cache statistics for monitoring.
      Returns:
      map containing cache hits, misses, and hit ratio