public class DiskCache
extends java.lang.Object
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:
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);
Modifier and Type | Field and Description |
---|---|
static boolean |
simulateUnwritableDir
debug only
|
Constructor and Description |
---|
DiskCache() |
Modifier and Type | Method and Description |
---|---|
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) |
public static void setRootDirectory(java.lang.String cacheDir)
cacheDir
- the cache directorypublic static void makeRootDirectory()
public static java.lang.String getRootDirectory()
public static void setCachePolicy(boolean alwaysInCache)
alwaysInCache
- make this the default policypublic static java.io.File getFileStandardPolicy(java.lang.String fileLocation)
fileLocation
- normal file locationpublic static java.io.File getFile(java.lang.String fileLocation, boolean alwaysInCache)
fileLocation
- normal file locationalwaysInCache
- true if you want to look only in the cachepublic static java.io.File getCacheFile(java.lang.String fileLocation)
fileLocation
- normal file locationpublic static void showCache(java.io.PrintStream pw)
public static void cleanCache(java.util.Date cutoff, java.lang.StringBuilder sbuff)
cutoff
- earliest date to allowsbuff
- write results here, null is ok.public static void cleanCache(long maxBytes, java.lang.StringBuilder sbuff)
maxBytes
- max number of bytes in cache.sbuff
- write results here, null is ok.public static void cleanCache(long maxBytes, java.util.Comparator<java.io.File> fileComparator, java.lang.StringBuilder sbuff)
maxBytes
- max number of bytes in cache.fileComparator
- sort files first with thissbuff
- write results here, null is ok.public static void main(java.lang.String[] args) throws java.io.IOException
java.io.IOException