Package org.nuxeo.common.file
Class LRUFileCache
- java.lang.Object
-
- org.nuxeo.common.file.LRUFileCache
-
- All Implemented Interfaces:
FileCache
public class LRUFileCache extends Object implements FileCache
A LRU cache ofFile
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
Nested Classes Modifier and Type Class Description protected static class
LRUFileCache.PathInfo
protected static class
LRUFileCache.RegularFileFilter
Filter keeping regular files that aren't temporary.
-
Field Summary
Fields Modifier and Type Field Description static long
CLEAR_OLD_ENTRIES_INTERVAL_MILLIS_DEFAULT
protected long
clearOldEntriesIntervalMillis
protected long
clearOldEntriesLast
protected Lock
clearOldEntriesLock
protected Path
dir
protected long
maxCount
protected long
maxSize
protected long
minAgeMillis
static Pattern
SIMPLE_ASCII
Allowed key pattern, used as file path.
-
Constructor Summary
Constructors Constructor Description LRUFileCache(File dir, long maxSize, long maxCount, long minAge)
Constructs a cache in the given directory with the given maximum size (in bytes).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkKey(String key)
void
clear()
Clears the cache.protected void
clearOldEntries()
Clears cache entries if they are old enough and their size makes the cache bigger than its maximum size.protected void
doClearOldEntries()
File
getFile(String key)
Gets a file from the cache.int
getNumberOfItems()
Gets the number of items in the cache.long
getSize()
Gets the size of the cache, in bytes.File
getTempFile()
Creates a temporary file.File
putFile(String key, File file)
Puts a file in the cache.File
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 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 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 Detail
-
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
public File getTempFile() throws IOException
Description copied from interface:FileCache
Creates a temporary file.- Specified by:
getTempFile
in interfaceFileCache
- Throws:
IOException
-
checkKey
protected void checkKey(String key) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
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 interfaceFileCache
- Parameters:
key
- the cache keyin
- the input stream to cache (closed afterwards)- Returns:
- the cached file
- Throws:
IOException
-
putFile
public File putFile(String key, File file) throws IllegalArgumentException, IOException
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
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.
-
recordAccess
protected void recordAccess(Path path)
Records access to a file by changing its modification time.
-
-