com.caucho.vfs
Class CachePath

java.lang.Object
  |
  +--com.caucho.vfs.Path
        |
        +--com.caucho.vfs.FilesystemPath
              |
              +--com.caucho.vfs.CachePath

public class CachePath
extends FilesystemPath

A cache wrapper over a path. All files are automatically cached. Once the cache is created, you use the file just like any other Path:

 Path root = new CachePath(Vfs.lookup("test/cacheable"));

 ReadStream is = root.lookup("test.file").openRead();
 ...
 is.close();
 

The cache size is governed by two limits: memory size and number of entries. The size of the disk is considered infinite, and it limited only by the number of entries.

The cache entries are stored in an LRU with a capacity of the number of entries. If loading a new file pushes the memory size over the limit, the old files are dropped from the memory cache.


Fields inherited from class com.caucho.vfs.FilesystemPath
bindRoot, pathname, root, separatorChar, userPath
 
Fields inherited from class com.caucho.vfs.Path
L, schemeMap
 
Constructor Summary
protected CachePath(FilesystemPath root, java.lang.String userPath, java.lang.String path)
           
  CachePath(Path root, int entries, long capacity)
          Creates a new cache.
 
Method Summary
 boolean canRead()
          Tests if the file can be read.
 boolean canWrite()
          Tests if the file can be written.
 void clear()
          Clear the cache.
 boolean equals(java.lang.Object b)
           
 boolean exists()
          Returns true if the underlying file exists.
protected  Path fsWalk(java.lang.String userPath, java.util.Map attributes, java.lang.String path)
          Looks up the cache entry for a given path.
 long getLastModified()
          Returns the last modified time of the file.
 long getLength()
          Returns the length of the file in bytes.
 java.lang.String getScheme()
          The scheme for the path is cache:
 int hashCode()
          Returns the hash code, which is based on the underlying file.
 boolean isDirectory()
          Tests if the path refers to a directory.
 boolean isFile()
          Tests if the path refers to a file.
 java.lang.String[] list()
           
 boolean mkdir()
          Creates the directory named by this path.
 boolean mkdirs()
          Creates the directory named by this path and any parent directories.
 StreamImpl openAppendImpl()
           
 StreamImpl openReadImpl()
          Opens a read stream to the path.
 StreamImpl openReadWriteImpl()
           
 StreamImpl openWriteImpl()
           
 boolean remove()
          Removes the file or directory named by this path.
 boolean renameTo(Path path)
          Renames the file or directory to the name given by the path.
 void setLastModified(long time)
           
 void setRemoveOnRelease(boolean remove)
          If set, files bumped from the cache are removed from the underlying filesystem.
 void writeToStream(java.io.OutputStream os)
          Write the contents of the cache item to the output stream.
 
Methods inherited from class com.caucho.vfs.FilesystemPath
bind, copy, createRoot, getFullPath, getParent, getPath, getTail, getURL, getUserPath, normalizePath, normalizePath, schemeWalk, setUserPath, toString
 
Methods inherited from class com.caucho.vfs.Path
createNewFile, createRoot, createTempFile, getAttribute, getAttributeNames, getContentType, getHost, getNativePath, getObject, getPort, getQuery, getValue, isObject, iterator, lookup, lookup, lookupNative, lookupNative, openAppend, openRead, openReadWrite, openWrite, removeAttribute, renameTo, scanScheme, setAttribute, setObject, setValue, unbind
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CachePath

protected CachePath(FilesystemPath root,
                    java.lang.String userPath,
                    java.lang.String path)
Parameters:
path - canonical path

CachePath

public CachePath(Path root,
                 int entries,
                 long capacity)
Creates a new cache.
Parameters:
root - underlying backing store.
entries - the number of entries in the cache.
capacity - total size in bytes allowed in the memory cache.
Method Detail

setRemoveOnRelease

public void setRemoveOnRelease(boolean remove)
If set, files bumped from the cache are removed from the underlying filesystem.
Parameters:
remove - if true, remove the underlying file on a release.

fsWalk

protected Path fsWalk(java.lang.String userPath,
                      java.util.Map attributes,
                      java.lang.String path)
Looks up the cache entry for a given path.
Overrides:
fsWalk in class FilesystemPath
Parameters:
userPath - the string given by the user in lookup()
attributes - the user's initialization attributes
path - the canonical path name
Returns:
a cache path entry

getScheme

public java.lang.String getScheme()
The scheme for the path is cache:
Overrides:
getScheme in class Path

clear

public void clear()
Clear the cache.

exists

public boolean exists()
Returns true if the underlying file exists.
Overrides:
exists in class Path

isDirectory

public boolean isDirectory()
Description copied from class: Path
Tests if the path refers to a directory.
Overrides:
isDirectory in class Path

isFile

public boolean isFile()
Description copied from class: Path
Tests if the path refers to a file.
Overrides:
isFile in class Path

getLength

public long getLength()
Description copied from class: Path
Returns the length of the file in bytes.
Overrides:
getLength in class Path
Following copied from class: com.caucho.vfs.Path
Returns:
0 for non-files

getLastModified

public long getLastModified()
Description copied from class: Path
Returns the last modified time of the file. According to the jdk, this may not correspond to the system time.
Overrides:
getLastModified in class Path
Following copied from class: com.caucho.vfs.Path
Returns:
0 for non-files.

setLastModified

public void setLastModified(long time)
Overrides:
setLastModified in class Path

canRead

public boolean canRead()
Description copied from class: Path
Tests if the file can be read.
Overrides:
canRead in class Path

canWrite

public boolean canWrite()
Description copied from class: Path
Tests if the file can be written.
Overrides:
canWrite in class Path

list

public java.lang.String[] list()
                        throws java.io.IOException
Overrides:
list in class Path
Following copied from class: com.caucho.vfs.Path
Returns:
The contents of this directory or null if the path does not refer to a directory.

mkdir

public boolean mkdir()
              throws java.io.IOException
Description copied from class: Path
Creates the directory named by this path.
Overrides:
mkdir in class Path
Following copied from class: com.caucho.vfs.Path
Returns:
true if successful.

mkdirs

public boolean mkdirs()
               throws java.io.IOException
Description copied from class: Path
Creates the directory named by this path and any parent directories.
Overrides:
mkdirs in class Path
Following copied from class: com.caucho.vfs.Path
Returns:
true if successful.

remove

public boolean remove()
               throws java.io.IOException
Description copied from class: Path
Removes the file or directory named by this path.
Overrides:
remove in class Path
Following copied from class: com.caucho.vfs.Path
Returns:
true if successful.

renameTo

public boolean renameTo(Path path)
                 throws java.io.IOException
Description copied from class: Path
Renames the file or directory to the name given by the path.
Overrides:
renameTo in class Path
Following copied from class: com.caucho.vfs.Path
Returns:
true if successful

writeToStream

public void writeToStream(java.io.OutputStream os)
                   throws java.io.IOException
Write the contents of the cache item to the output stream.
Overrides:
writeToStream in class Path
Parameters:
os - destination output stream.

openReadImpl

public StreamImpl openReadImpl()
                        throws java.io.IOException
Opens a read stream to the path. When the file is in the memory cache, the read stream returns the memory. Otherwise it reads from the backing file.
Overrides:
openReadImpl in class Path

openWriteImpl

public StreamImpl openWriteImpl()
                         throws java.io.IOException
Overrides:
openWriteImpl in class Path

openAppendImpl

public StreamImpl openAppendImpl()
                          throws java.io.IOException
Overrides:
openAppendImpl in class Path

openReadWriteImpl

public StreamImpl openReadWriteImpl()
                             throws java.io.IOException
Overrides:
openReadWriteImpl in class Path

hashCode

public int hashCode()
Returns the hash code, which is based on the underlying file.
Overrides:
hashCode in class FilesystemPath

equals

public boolean equals(java.lang.Object b)
Overrides:
equals in class FilesystemPath