public class LRUFileCache extends Object implements FileCache
File
s with capped filesystem size.
When a new file is put in the cache, if the total size becomes more that the maximum size then least recently access entries are removed until the new file fits.
A file will never be actually removed from the filesystem while the File object returned by getFile(java.lang.String)
is still
referenced.
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.LRUFileCacheEntry
In-memory entry for a cached file.
|
Modifier and Type | Field and Description |
---|---|
protected Map<String,LRUFileCache.LRUFileCacheEntry> |
cache
Cached files.
|
protected long |
cacheSize
Size of the cached files.
|
protected File |
dir |
protected Map<String,File> |
files
Referenced files on the filesystem.
|
protected LinkedList<String> |
lru
Most recently used entries from the cache are first.
|
protected long |
maxSize |
static Pattern |
SIMPLE_ASCII
Allowed key pattern, used as file path.
|
Constructor and Description |
---|
LRUFileCache(File dir,
long maxSize)
Constructs a cache in the given directory with the given maximum size (in bytes).
|
Modifier and Type | Method and Description |
---|---|
protected void |
add(String key,
LRUFileCache.LRUFileCacheEntry entry) |
protected void |
checkKey(String key) |
void |
clear()
Clears the cache.
|
protected void |
ensureCapacity(long size) |
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 File |
putFileInCache(String key,
File file)
Puts a file that's already in the correct filesystem location in the internal cache datastructures.
|
protected void |
recordAccess(String key) |
protected void |
remove(String key) |
public static final Pattern SIMPLE_ASCII
protected final long maxSize
protected final Map<String,LRUFileCache.LRUFileCacheEntry> cache
protected final Map<String,File> files
protected long cacheSize
protected final LinkedList<String> lru
public LRUFileCache(File dir, long maxSize)
dir
- the directory to use to store cached filesmaxSize
- the maximum size of the cache (in bytes)public long getSize()
FileCache
public int getNumberOfItems()
FileCache
getNumberOfItems
in interface FileCache
public File getTempFile() throws IOException
FileCache
getTempFile
in interface FileCache
IOException
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
protected File putFileInCache(String key, File file)
protected void checkKey(String key) throws IllegalArgumentException
IllegalArgumentException
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.
public void clear()
FileCache
Files will not be deleted from the filesystm while the returned file objects are still referenced.
protected void recordAccess(String key)
protected void add(String key, LRUFileCache.LRUFileCacheEntry entry)
protected void ensureCapacity(long size)
Copyright © 2015 Nuxeo SA. All rights reserved.