Nuxeo Enterprise Platform 5.4

org.nuxeo.common.file
Class LRUFileCache

java.lang.Object
  extended by org.nuxeo.common.file.LRUFileCache
All Implemented Interfaces:
FileCache

public class LRUFileCache
extends Object
implements FileCache

A LRU cache of Files 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.


Field Summary
static Pattern SIMPLE_ASCII
          Allowed key pattern, used as file path.
 
Constructor Summary
LRUFileCache(File dir, long maxSize)
          Constructs a cache in the given directory with the given maximum size (in bytes).
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIMPLE_ASCII

public static final Pattern SIMPLE_ASCII
Allowed key pattern, used as file path.

Constructor Detail

LRUFileCache

public LRUFileCache(File dir,
                    long maxSize)
Constructs a cache in the given directory with the given maximum size (in bytes).

Parameters:
dir - the directory to use to store cached files
maxSize - the maximum size of the cache (in bytes)
Method Detail

getSize

public long getSize()
Description copied from interface: FileCache
Gets the size of the cache, in bytes.

Specified by:
getSize in interface FileCache

getNumberOfItems

public int getNumberOfItems()
Description copied from interface: FileCache
Gets the number of items in the cache.

Specified by:
getNumberOfItems in interface FileCache

getTempFile

public File getTempFile()
                 throws IOException
Description copied from interface: FileCache
Creates a temporary file.

Specified by:
getTempFile in interface FileCache
Throws:
IOException

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 interface FileCache
Parameters:
key - the cache key
in - 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 interface FileCache
Parameters:
key - the cache key
file - the file to cache
Returns:
the cached file
Throws:
IllegalArgumentException - if the key is illegal
IOException

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.

Specified by:
getFile in interface FileCache
Parameters:
key - the cache key
Returns:
the cached file, or null if absent

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.

Specified by:
clear in interface FileCache

Nuxeo Enterprise Platform 5.4

Copyright © 2011 Nuxeo SAS. All Rights Reserved.