Package org.apache.commons.vfs2.cache
Class SoftRefFilesCache
- java.lang.Object
-
- org.apache.commons.vfs2.provider.AbstractVfsComponent
-
- org.apache.commons.vfs2.cache.AbstractFilesCache
-
- org.apache.commons.vfs2.cache.SoftRefFilesCache
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,FilesCache
,VfsComponent
- Direct Known Subclasses:
WeakRefFilesCache
public class SoftRefFilesCache extends AbstractFilesCache
This implementation caches every file as long as it is strongly reachable by the JVM. As soon as the JVM needs memory - every softly reachable file will be discarded.- See Also:
SoftReference
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
SoftRefFilesCache.ReleaseThread
This thread will listen on the ReferenceQueue and remove the entry in the file cache as soon as the JVM removes the reference.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<FileSystem,java.util.Map<FileName,java.lang.ref.Reference<FileObject>>>
fileSystemCache
private static org.apache.commons.logging.Log
log
private java.lang.ref.ReferenceQueue<FileObject>
refQueue
private java.util.Map<java.lang.ref.Reference<FileObject>,FileSystemAndNameKey>
refReverseMap
private SoftRefFilesCache.ReleaseThread
releaseThread
-
Constructor Summary
Constructors Constructor Description SoftRefFilesCache()
Constructs a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear(FileSystem fileSystem)
Purges the entries corresponding to the FileSystem.void
close()
Closes the provider.private void
close(FileSystem fileSystem)
protected java.lang.ref.Reference<FileObject>
createReference(FileObject file, java.lang.ref.ReferenceQueue<FileObject> refqueue)
private void
endThread()
FileObject
getFile(FileSystem fileSystem, FileName fileName)
Retrieves a FileObject from the cache by name.protected java.util.Map<FileName,java.lang.ref.Reference<FileObject>>
getOrCreateFilesystemCache(FileSystem fileSystem)
private java.lang.String
getSafeName(FileName fileName)
private java.lang.String
getSafeName(FileObject fileObject)
void
putFile(FileObject fileObject)
Adds a FileObject to the cache.boolean
putFileIfAbsent(FileObject fileObject)
Adds a FileObject to the cache if it isn't already present.private void
removeFile(java.lang.ref.Reference<?> ref)
private boolean
removeFile(FileSystemAndNameKey key)
void
removeFile(FileSystem fileSystem, FileName fileName)
Removes a file from cache.private void
startThread()
java.lang.String
toString()
-
Methods inherited from class org.apache.commons.vfs2.cache.AbstractFilesCache
touchFile
-
Methods inherited from class org.apache.commons.vfs2.provider.AbstractVfsComponent
getContext, getLogger, init, setContext, setLogger
-
-
-
-
Field Detail
-
log
private static final org.apache.commons.logging.Log log
-
fileSystemCache
private final java.util.Map<FileSystem,java.util.Map<FileName,java.lang.ref.Reference<FileObject>>> fileSystemCache
-
refReverseMap
private final java.util.Map<java.lang.ref.Reference<FileObject>,FileSystemAndNameKey> refReverseMap
-
refQueue
private final java.lang.ref.ReferenceQueue<FileObject> refQueue
-
releaseThread
private SoftRefFilesCache.ReleaseThread releaseThread
-
-
Method Detail
-
clear
public void clear(FileSystem fileSystem)
Description copied from interface:FilesCache
Purges the entries corresponding to the FileSystem.- Parameters:
fileSystem
- The FileSystem.
-
close
public void close()
Description copied from class:AbstractVfsComponent
Closes the provider. This implementation does nothing.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceFilesCache
- Specified by:
close
in interfaceVfsComponent
- Overrides:
close
in classAbstractVfsComponent
-
close
private void close(FileSystem fileSystem)
- Parameters:
fileSystem
- The file system to close.
-
createReference
protected java.lang.ref.Reference<FileObject> createReference(FileObject file, java.lang.ref.ReferenceQueue<FileObject> refqueue)
-
endThread
private void endThread()
-
getFile
public FileObject getFile(FileSystem fileSystem, FileName fileName)
Description copied from interface:FilesCache
Retrieves a FileObject from the cache by name.- Parameters:
fileSystem
- The FileSystem.fileName
- the name- Returns:
- the file object or null if file is not cached
-
getOrCreateFilesystemCache
protected java.util.Map<FileName,java.lang.ref.Reference<FileObject>> getOrCreateFilesystemCache(FileSystem fileSystem)
-
getSafeName
private java.lang.String getSafeName(FileName fileName)
-
getSafeName
private java.lang.String getSafeName(FileObject fileObject)
-
putFile
public void putFile(FileObject fileObject)
Description copied from interface:FilesCache
Adds a FileObject to the cache.- Parameters:
fileObject
- the file
-
putFileIfAbsent
public boolean putFileIfAbsent(FileObject fileObject)
Description copied from interface:FilesCache
Adds a FileObject to the cache if it isn't already present.- Parameters:
fileObject
- the file- Returns:
- true if the file was stored, false otherwise.
-
removeFile
public void removeFile(FileSystem fileSystem, FileName fileName)
Description copied from interface:FilesCache
Removes a file from cache.- Parameters:
fileSystem
- file systemfileName
- file name
-
removeFile
private boolean removeFile(FileSystemAndNameKey key)
-
removeFile
private void removeFile(java.lang.ref.Reference<?> ref)
-
startThread
private void startThread()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-