ucar.nc2.util
Class DiskCache

java.lang.Object
  extended by ucar.nc2.util.DiskCache

public class DiskCache
extends java.lang.Object

This is a general purpose utility for determining a place to write files and cache them, eg for uncompressing files. This class does not scour itself.

The cdm library sometimes needs to write files, eg to uncompress them, or for grib index files, etc. The first choice is to write these files in the same directory that the original file lives in. However, that directory may not be writeable, so we need to find a place to write them to. We also want to use the file if it already exists.

A writeable cache "root directory" is set:

  1. explicitly by setRootDirectory()
  2. through the system property "nj22.cache" if it exists
  3. by appending "/nj22/cache" to the system property "user.home" if it exists
  4. by appending "/nj22/cache" to the system property "user.dir" if it exists
  5. use "./nj22/cache" if none of the above

Scenario 1: want to uncompress a file; check to see if already have done so, otherwise get a File that can be written to.

 // see if already uncompressed
 File uncompressedFile = FileCache.getFile( uncompressedFilename, false);
 if (!uncompressedFile.exists()) {
   // nope, uncompress it
   UncompressInputStream.uncompress( uriString, uncompressedFile);
 }
 doSomething( uncompressedFile);
 

Scenario 2: want to write a derived file always in the cache.

 File derivedFile = FileCache.getCacheFile( derivedFilename);
 if (!derivedFile.exists()) {
   createDerivedFile( derivedFile);
 }
 doSomething( derivedFile);
 

Scenario 3: same as scenario 1, but use the default Cache policy:

 File wf = FileCache.getFileStandardPolicy( uncompressedFilename);
 if (!wf.exists()) {
   writeToFile( wf);
   wf.close();
 }
 doSomething( wf);
 

Author:
jcaron

Field Summary
static boolean simulateUnwritableDir
          debug only
 
Constructor Summary
DiskCache()
           
 
Method Summary
static void cleanCache(java.util.Date cutoff, java.lang.StringBuilder sbuff)
          Remove all files with date < cutoff.
static void cleanCache(long maxBytes, java.util.Comparator<java.io.File> fileComparator, java.lang.StringBuilder sbuff)
          Remove files if needed to make cache have less than maxBytes bytes file sizes.
static void cleanCache(long maxBytes, java.lang.StringBuilder sbuff)
          Remove files if needed to make cache have less than maxBytes bytes file sizes.
static java.io.File getCacheFile(java.lang.String fileLocation)
          Get a file in the cache.
static java.io.File getFile(java.lang.String fileLocation, boolean alwaysInCache)
          Get a File if it exists.
static java.io.File getFileStandardPolicy(java.lang.String fileLocation)
          Get a File if it exists.
static java.lang.String getRootDirectory()
          Get the name of the root directory
static void main(java.lang.String[] args)
           
static void makeRootDirectory()
          Make sure that the current root directory exists.
static void setCachePolicy(boolean alwaysInCache)
          Set the standard policy used in getWriteableFileStandardPolicy().
static void setRootDirectory(java.lang.String cacheDir)
          Set the cache root directory.
static void showCache(java.io.PrintStream pw)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

simulateUnwritableDir

public static boolean simulateUnwritableDir
debug only

Constructor Detail

DiskCache

public DiskCache()
Method Detail

setRootDirectory

public static void setRootDirectory(java.lang.String cacheDir)
Set the cache root directory. Create it if it doesnt exist.

Parameters:
cacheDir - the cache directory

makeRootDirectory

public static void makeRootDirectory()
Make sure that the current root directory exists.


getRootDirectory

public static java.lang.String getRootDirectory()
Get the name of the root directory

Returns:
name of the root directory

setCachePolicy

public static void setCachePolicy(boolean alwaysInCache)
Set the standard policy used in getWriteableFileStandardPolicy(). Default is to try to create the file in the same directory as the original. Setting alwaysInCache to true means to always create it in the cache directory.

Parameters:
alwaysInCache - make this the default policy

getFileStandardPolicy

public static java.io.File getFileStandardPolicy(java.lang.String fileLocation)
Get a File if it exists. If not, get a File that can be written to. Use the standard policy to decide where to place it.

Things are a bit compilicated, because in order to guarentee a file in an arbitrary location can be written to, we have to try to open it as a FileOutputStream. If we do, we dont want to open it twice, so we return a WriteableFile that contains an opened FileOutputStream. If it already exists, or we get it from cache, we dont need to open it.

In any case, you must call WriteableFile.close() to make sure its closed.

Parameters:
fileLocation - normal file location
Returns:
WriteableFile holding the writeable File and a possibly opened FileOutputStream (append false)

getFile

public static java.io.File getFile(java.lang.String fileLocation,
                                   boolean alwaysInCache)
Get a File if it exists. If not, get a File that can be written to. If alwaysInCache, look only in the cache, otherwise, look in the "normal" location first, then in the cache.

Parameters:
fileLocation - normal file location
alwaysInCache - true if you want to look only in the cache
Returns:
a File that either exists or is writeable.

getCacheFile

public static java.io.File getCacheFile(java.lang.String fileLocation)
Get a file in the cache. File may or may not exist. We assume its always writeable. If it does exist, set its LastModifiedDate to current time.

Parameters:
fileLocation - normal file location
Returns:
equivilent File in the cache.

showCache

public static void showCache(java.io.PrintStream pw)

cleanCache

public static void cleanCache(java.util.Date cutoff,
                              java.lang.StringBuilder sbuff)
Remove all files with date < cutoff.

Parameters:
cutoff - earliest date to allow
sbuff - write results here, null is ok.

cleanCache

public static void cleanCache(long maxBytes,
                              java.lang.StringBuilder sbuff)
Remove files if needed to make cache have less than maxBytes bytes file sizes. This will remove oldest files first.

Parameters:
maxBytes - max number of bytes in cache.
sbuff - write results here, null is ok.

cleanCache

public static void cleanCache(long maxBytes,
                              java.util.Comparator<java.io.File> fileComparator,
                              java.lang.StringBuilder sbuff)
Remove files if needed to make cache have less than maxBytes bytes file sizes. This will remove files in sort order defined by fileComparator. The first files in the sort order are kept, until the max bytes is exceeded, then they are deleted.

Parameters:
maxBytes - max number of bytes in cache.
fileComparator - sort files first with this
sbuff - write results here, null is ok.

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Throws:
java.io.IOException