How does the Commons VFS Cache works?

I’m trying to learn how to use Apache Commons VFS2.

I’ve read all the docu I could find and I’m already kind of familiar with the API but still there is one thing that isn’t perfectly clear to me.

How does the Cache mechanism work? In particular:

  • I don’t get the difference between a Cache Strategy and the FilesCache interface. Which is used when?

  • It is said in the docu here: http://wiki.apache.org/commons/VfsCacheStrategy … that there are 3 possible cache strategies and each is explained in details. I get the “simplest” on_call strategy but the other two I don’t. For example – if we choose a MANUAL strategy it is said that “you have to use fileObject.refresh() to refresh your object with the filesystem“. But what exactly does this imply? Does it mean that if I write bytes to the FileContents of this FileObject they won’t actually get written until I close the file object or call refresh? What if I have 2 FileObjects that are resolved from the same URI and I delete() the first one? Will the second’s exists() method still return true since file objects are cached?

When I try to play with the different cache strategies locally on my machine I don’t really find any differences in the behaviour. They all behave the same and the files are always in sync with the FS (or at least it isn’t noticable that they aren’t).

Answer

The CacheStrategy basically controls the re-syncing of meta data inside a FileObject in-between multiple calls. The FileObject decides when to refresh its view of the world.

It will call refresh() every time you resolve it, or it will call refresh() before each FileObject method call (via OnCallRefreshFileObject decorator) or never automatically.

refresh() most of the time sets the FileObject state to detached, so it is read freshly when the next action checks with attach().

It is mostly related to metadata like attributes and children, I don’t think there is any filesystem provider who actually caches content.

The FilesCache is actually responsible for caching instances of the FileObject inbetween resolveFile() calls. So you if happen to resolve or navigate to the same file, you will also get the same java object instance (unless you use the NullFilesCache or the LRUFilesCache cache expired some entries).

Leave a Reply

Your email address will not be published. Required fields are marked *