Class LRUFileCache

  • All Implemented Interfaces:
    FileCache

    public class LRUFileCache
    extends Object
    implements FileCache
    A LRU cache of Files with maximum filesystem size.

    Cache entries that are old enough and whose size makes the cache bigger than its maximum size are deleted.

    The cache keys are restricted to a subset of ASCII: letters, digits and dashes. Usually a MD5 or SHA1 hash is used.

    • Field Detail

      • SIMPLE_ASCII

        public static final Pattern SIMPLE_ASCII
        Allowed key pattern, used as file path.
      • CLEAR_OLD_ENTRIES_INTERVAL_MILLIS_DEFAULT

        public static final long CLEAR_OLD_ENTRIES_INTERVAL_MILLIS_DEFAULT
        See Also:
        Constant Field Values
      • clearOldEntriesIntervalMillis

        protected long clearOldEntriesIntervalMillis
      • dir

        protected final Path dir
      • maxSize

        protected final long maxSize
      • maxCount

        protected final long maxCount
      • minAgeMillis

        protected final long minAgeMillis
      • clearOldEntriesLock

        protected Lock clearOldEntriesLock
      • clearOldEntriesLast

        protected long clearOldEntriesLast
    • Constructor Detail

      • LRUFileCache

        public LRUFileCache​(File dir,
                            long maxSize,
                            long maxCount,
                            long minAge)
        Constructs a cache in the given directory with the given maximum size (in bytes).
        Parameters:
        dir - the directory to use to store cached files
        maxSize - the maximum size of the cache (in bytes)
        maxCount - the maximum number of files in the cache
        minAge - the minimum age of a file in the cache to be eligible for removal (in seconds)
    • Method Detail

      • setClearOldEntriesIntervalMillis

        public void setClearOldEntriesIntervalMillis​(long millis)
      • getSize

        public long getSize()
        Description copied from interface: FileCache
        Gets the size of the cache, in bytes.
        Specified by:
        getSize in interface FileCache
      • getNumberOfItems

        public int getNumberOfItems()
        Description copied from interface: FileCache
        Gets the number of items in the cache.
        Specified by:
        getNumberOfItems in interface FileCache
      • clear

        public void clear()
        Description copied from interface: FileCache
        Clears the cache.

        Files will not be deleted from the filesystm while the returned file objects are still referenced.

        Specified by:
        clear in interface FileCache
      • clearOldEntries

        protected void clearOldEntries()
        Clears cache entries if they are old enough and their size makes the cache bigger than its maximum size.
      • doClearOldEntries

        protected void doClearOldEntries()
      • putFile

        public File putFile​(String key,
                            InputStream in)
                     throws IOException
        Puts a file in the cache.

        The key is used as a file name in the directory cache.

        Specified by:
        putFile in interface FileCache
        Parameters:
        key - the cache key
        in - the input stream to cache (closed afterwards)
        Returns:
        the cached file
        Throws:
        IOException
      • getFile

        public File getFile​(String key)
        Description copied from interface: FileCache
        Gets a file from the cache.

        A returned file will never be deleted from the filesystem while the returned file object is still referenced, although it may be purged from the cache.

        Specified by:
        getFile in interface FileCache
        Parameters:
        key - the cache key
        Returns:
        the cached file, or null if absent
      • recordAccess

        protected void recordAccess​(Path path)
        Records access to a file by changing its modification time.