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 | Field and Description |
---|---|
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 |
---|---|
void |
clear()
Clears the cache.
|
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.
|
public static final Pattern SIMPLE_ASCII
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
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.
Copyright © 2013 Nuxeo SA. All Rights Reserved.