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 ofFiles 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 classLRUFileCache.PathInfoprotected static classLRUFileCache.RegularFileFilterFilter keeping regular files that aren't temporary.
-
Field Summary
Fields Modifier and Type Field Description static longCLEAR_OLD_ENTRIES_INTERVAL_MILLIS_DEFAULTprotected longclearOldEntriesIntervalMillisprotected longclearOldEntriesLastprotected LockclearOldEntriesLockprotected Pathdirprotected longmaxCountprotected longmaxSizeprotected longminAgeMillisstatic PatternSIMPLE_ASCIIAllowed 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 voidcheckKey(String key)voidclear()Clears the cache.protected voidclearOldEntries()Clears cache entries if they are old enough and their size makes the cache bigger than its maximum size.protected voiddoClearOldEntries()FilegetFile(String key)Gets a file from the cache.intgetNumberOfItems()Gets the number of items in the cache.longgetSize()Gets the size of the cache, in bytes.FilegetTempFile()Creates a temporary file.FileputFile(String key, File file)Puts a file in the cache.FileputFile(String key, InputStream in)Puts a file in the cache.protected voidrecordAccess(Path path)Records access to a file by changing its modification time.voidsetClearOldEntriesIntervalMillis(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:FileCacheGets the size of the cache, in bytes.
-
getNumberOfItems
public int getNumberOfItems()
Description copied from interface:FileCacheGets the number of items in the cache.- Specified by:
getNumberOfItemsin interfaceFileCache
-
clear
public void clear()
Description copied from interface:FileCacheClears 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:FileCacheCreates a temporary file.- Specified by:
getTempFilein 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:
putFilein 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:
putFilein 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:FileCacheGets 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.
-
-