Package org.nuxeo.common.file
Class LRUFileCache
java.lang.Object
org.nuxeo.common.file.LRUFileCache
- All Implemented Interfaces:
FileCache
A LRU cache of
File
s 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.
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
protected static class
Filter keeping regular files that aren't temporary. -
Field Summary
-
Constructor Summary
ConstructorDescriptionLRUFileCache
(File dir, long maxSize, long maxCount, long minAge) Constructs a cache in the given directory with the given maximum size (in bytes). -
Method Summary
Modifier and TypeMethodDescriptionprotected void
void
clear()
Clears the cache.protected void
Clears cache entries if they are old enough and their size makes the cache bigger than its maximum size.protected void
Gets a file from the cache.int
Gets the number of items in the cache.long
getSize()
Gets the size of the cache, in bytes.Creates a temporary file.Puts a file in the cache.putFile
(String key, InputStream in) Puts a file in the cache.protected void
recordAccess
(Path path) Records access to a file by changing its modification time.void
setClearOldEntriesIntervalMillis
(long millis)
-
Field Details
-
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:
-
clearOldEntriesIntervalMillis
protected long clearOldEntriesIntervalMillis -
dir
-
maxSize
protected final long maxSize -
maxCount
protected final long maxCount -
minAgeMillis
protected final long minAgeMillis -
clearOldEntriesLock
-
clearOldEntriesLast
protected long clearOldEntriesLast
-
-
Constructor Details
-
LRUFileCache
Constructs a cache in the given directory with the given maximum size (in bytes).- Parameters:
dir
- the directory to use to store cached filesmaxSize
- the maximum size of the cache (in bytes)maxCount
- the maximum number of files in the cacheminAge
- the minimum age of a file in the cache to be eligible for removal (in seconds)
-
-
Method Details
-
setClearOldEntriesIntervalMillis
public void setClearOldEntriesIntervalMillis(long millis) -
getSize
public long getSize()Description copied from interface:FileCache
Gets the size of the cache, in bytes. -
getNumberOfItems
public int getNumberOfItems()Description copied from interface:FileCache
Gets the number of items in the cache.- Specified by:
getNumberOfItems
in interfaceFileCache
-
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.
-
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() -
getTempFile
Description copied from interface:FileCache
Creates a temporary file.- Specified by:
getTempFile
in interfaceFileCache
- Throws:
IOException
-
checkKey
- Throws:
IllegalArgumentException
-
putFile
Puts a file in the cache.The key is used as a file name in the directory cache.
- Specified by:
putFile
in interfaceFileCache
- Parameters:
key
- the cache keyin
- the input stream to cache (closed afterwards)- Returns:
- the cached file
- Throws:
IOException
-
putFile
Puts a file in the cache.The file must have been created through
FileCache.getTempFile()
. The file is "given" to this method, who will delete it or rename it.The key is used as a file name in the directory cache.
- Specified by:
putFile
in interfaceFileCache
- Parameters:
key
- the cache keyfile
- the file to cache- Returns:
- the cached file
- Throws:
IllegalArgumentException
- if the key is illegalIOException
-
getFile
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.
-
recordAccess
Records access to a file by changing its modification time.
-