ucar.nc2.util
Class DiskCache2

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

public class DiskCache2
extends Object

Manages a place on disk to persistently cache files, which are deleted when the last modified date exceeds a certain time. This starts up a thread to periodically scour itself; be sure to call exit() to terminate the thread.

Each DiskCache has a "root directory", which may be set as an absolute path, or reletive to the DiskCache "home directory". The root directory must be writeable. The DiskCache home directory is set in the following order:

  1. through the system property "nj22.cachePersistRoot" if it exists
  2. through the system property "user.home" if it exists
  3. through the system property "user.dir" if it exists
  4. to the current working directory

Author:
jcaron

Field Summary
static int CACHEPATH_POLICY_NESTED_DIRECTORY
           
static int CACHEPATH_POLICY_NESTED_TRUNCATE
           
static int CACHEPATH_POLICY_ONE_DIRECTORY
           
 
Constructor Summary
DiskCache2(String root, boolean reletiveToHome, int persistMinutes, int scourEveryMinutes)
          Create a cache on disk.
 
Method Summary
 void cleanCache(File dir, StringBuilder sbuff, boolean isRoot)
          Remove any files or directories whose last modified time greater than persistMinutes
 File createUniqueFile(String prefix, String suffix)
          Create a new, uniquely named file in the root directory.
 void exit()
          Be sure to call this when your application exits, otherwise the scour thread may not die.
 File getCacheFile(String fileLocation)
          Get a File in the cache, corresponding to the fileLocation.
 String getRootDirectory()
          Get the cache root directory.
static void main(String[] args)
          debug
 void setCachePathPolicy(int cachePathPolicy, String cachePathPolicyParam)
          Set the cache path policy
 void setLogger(org.slf4j.Logger cacheLog)
          Optionally set a logger.
 void setPolicy(int cachePathPolicy)
          Set the cache path policy
 void setRootDirectory(String cacheDir)
           
 void showCache(PrintStream pw)
          Show cache contents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CACHEPATH_POLICY_ONE_DIRECTORY

public static int CACHEPATH_POLICY_ONE_DIRECTORY

CACHEPATH_POLICY_NESTED_DIRECTORY

public static int CACHEPATH_POLICY_NESTED_DIRECTORY

CACHEPATH_POLICY_NESTED_TRUNCATE

public static int CACHEPATH_POLICY_NESTED_TRUNCATE
Constructor Detail

DiskCache2

public DiskCache2(String root,
                  boolean reletiveToHome,
                  int persistMinutes,
                  int scourEveryMinutes)
Create a cache on disk.

Parameters:
root - the root directory of the cache. Must be writeable.
reletiveToHome - if the root directory is reletive to the cache home directory.
persistMinutes - a file is deleted if its last modified time is greater than persistMinutes
scourEveryMinutes - how often to run the scour process. If <= 0, dont scour.
Method Detail

exit

public void exit()
Be sure to call this when your application exits, otherwise the scour thread may not die.


setLogger

public void setLogger(org.slf4j.Logger cacheLog)
Optionally set a logger. Each time a scour is done, a messsage is written to it.

Parameters:
cacheLog - use this logger

setRootDirectory

public void setRootDirectory(String cacheDir)

getRootDirectory

public String getRootDirectory()
Get the cache root directory.

Returns:
the cache root directory.

getCacheFile

public File getCacheFile(String fileLocation)
Get a File in the cache, corresponding to the fileLocation. File may or may not exist. If fileLocation has "/" in it, and cachePathPolicy == CACHEPATH_POLICY_NESTED_DIRECTORY, the nested directories will be created.

Parameters:
fileLocation - logical file location
Returns:
physical File in the cache.

createUniqueFile

public File createUniqueFile(String prefix,
                             String suffix)
Create a new, uniquely named file in the root directory. Mimics File.createTempFile()

Parameters:
prefix - The prefix string to be used in generating the file's name; must be at least three characters long
suffix - The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used
Returns:
uniquely named file

setCachePathPolicy

public void setCachePathPolicy(int cachePathPolicy,
                               String cachePathPolicyParam)
Set the cache path policy

Parameters:
cachePathPolicy - one of: CACHEPATH_POLICY_ONE_DIRECTORY (default) : replace "/" with "-", so all files are in one directory. CACHEPATH_POLICY_NESTED_DIRECTORY: cache files are in nested directories under the root. CACHEPATH_POLICY_NESTED_TRUNCATE: eliminate leading directories
cachePathPolicyParam - for CACHEPATH_POLICY_NESTED_TRUNCATE, eliminate this string

setPolicy

public void setPolicy(int cachePathPolicy)
Set the cache path policy

Parameters:
cachePathPolicy - one of CACHEPATH_POLICY__XXXX

showCache

public void showCache(PrintStream pw)
Show cache contents.

Parameters:
pw - write to this PrintStream.

cleanCache

public void cleanCache(File dir,
                       StringBuilder sbuff,
                       boolean isRoot)
Remove any files or directories whose last modified time greater than persistMinutes

Parameters:
dir - clean starting here
sbuff - status messages here, may be null
isRoot - delete empty directories, bit not root directory

main

public static void main(String[] args)
                 throws IOException
debug

Throws:
IOException


Copyright © 1999-2011 UCAR/Unidata. All Rights Reserved.