我想在目录中缓存本地的网络文件。高速缓存应该是持久的,只读的,并且应该能够管理它的大小(最大磁盘使用量,已满时删除未使用的文件)。我调查了Apache JVS,但越来越困惑,如何使用它(缓存/复制器)或编写新的提供程序?
我开始编写自定义缓存,但是不确定我是否走对了。 另一件事是,它不是在访问InputStream时在resolveFile触发的。
public static void main(final String[] args) {
new PlayCache().run();
}
private void run() {
try {
final StandardFileSystemManager fm = new StandardFileSystemManager();
fm.setFilesCache(new WarpCache());
fm.init();
VFS.setManager(fm);
final FileSystemManager fsm = VFS.getManager();
final FileObject fo = fsm.resolveFile("/mnt/archive/someFile.tgz");
final InputStream is = fo.getContent().getInputStream();
long counter=0;
while (is.read()!=-1)
{
counter++;
}
System.out.println(counter);
// replicator.close();
//dfsm.close();
} catch (final FileSystemException e) {
e.printStackTrace();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class WarpCache extends AbstractFilesCache{
private final FilesCache subCache;
public WarpCache() {
this(new SoftRefFilesCache());
}
public WarpCache(final FilesCache cache) {
super();
subCache = cache;
}
public void putFile(final FileObject file) {
subCache.putFile(file);
}
public boolean putFileIfAbsent(final FileObject file) {
return subCache.putFileIfAbsent(file);
}
public FileObject getFile(final FileSystem filesystem, final FileName name) {
return subCache.getFile(filesystem, name);
}
public void clear(final FileSystem fileSystem) {
subCache.clear(fileSystem);
}
public void removeFile(final FileSystem filesystem, final FileName name) {
subCache.removeFile(filesystem, name);
}
}