public class LRUFileCache extends Object implements FileCache
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.
Modifier and Type | Class and Description |
---|---|
protected static class |
LRUFileCache.PathInfo |
protected static class |
LRUFileCache.RegularFileFilter
Filter keeping regular files that aren't temporary.
|
Modifier and Type | Field and Description |
---|---|
static long |
CLEAR_OLD_ENTRIES_INTERVAL_MILLIS |
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 and Description |
---|
LRUFileCache(File dir,
long maxSize,
long maxCount,
long minAge)
Constructs a cache in the given directory with the given maximum size (in bytes).
|
Modifier and Type | Method and 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.
|
public static final Pattern SIMPLE_ASCII
public static long CLEAR_OLD_ENTRIES_INTERVAL_MILLIS
protected final long maxSize
protected final long maxCount
protected final long minAgeMillis
protected Lock clearOldEntriesLock
protected long clearOldEntriesLast
public LRUFileCache(File dir, long maxSize, long maxCount, long minAge)
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)public long getSize()
FileCache
public int getNumberOfItems()
FileCache
getNumberOfItems
in interface FileCache
public void clear()
FileCache
Files will not be deleted from the filesystm while the returned file objects are still referenced.
protected void clearOldEntries()
protected void doClearOldEntries()
public File getTempFile() throws IOException
FileCache
getTempFile
in interface FileCache
IOException
protected void checkKey(String key) throws IllegalArgumentException
IllegalArgumentException
public File putFile(String key, InputStream in) throws IOException
The key is used as a file name in the directory cache.
putFile
in interface FileCache
key
- the cache keyin
- the input stream to cache (closed afterwards)IOException
public File putFile(String key, File file) throws IllegalArgumentException, IOException
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.
putFile
in interface FileCache
key
- the cache keyfile
- the file to cacheIllegalArgumentException
- if the key is illegalIOException
public File getFile(String key)
FileCache
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.
protected void recordAccess(Path path)
Copyright © 2018 Nuxeo. All rights reserved.