public class DiskCache2
extends java.lang.Object
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:
Modifier and Type | Class and Description |
---|---|
static class |
DiskCache2.CachePathPolicy |
Constructor and Description |
---|
DiskCache2(java.lang.String root,
boolean reletiveToHome,
int persistMinutes,
int scourEveryMinutes)
Create a cache on disk.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
canWrite(java.io.File f)
Returns
true if we can write to the file. |
void |
cleanCache(java.io.File dir,
java.lang.StringBuilder sbuff,
boolean isRoot)
Remove any files or directories whose last modified time greater than persistMinutes
|
java.io.File |
createUniqueFile(java.lang.String prefix,
java.lang.String suffix)
Reserve 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.
|
java.io.File |
getCacheFile(java.lang.String fileLocation)
Get a File in the cache, corresponding to the fileLocation.
|
static DiskCache2 |
getDefault()
Default DiskCache2 strategy: use $user_home/.unidata/cache/, no scouring, alwaysUseCache = false
Mimics default DiskCache static class
|
java.io.File |
getExistingFileOrCache(java.lang.String fileLocation)
Looking for an existing file, in cache or no
|
java.io.File |
getFile(java.lang.String fileLocation)
Get the named File.
|
static DiskCache2 |
getNoop() |
java.lang.String |
getRootDirectory()
Get the cache root directory.
|
void |
setAlwaysUseCache(boolean alwaysUseCache)
If true, always put the file in the cache.
|
void |
setCachePathPolicy(DiskCache2.CachePathPolicy cachePathPolicy,
java.lang.String cachePathPolicyParam)
Set the cache path policy
|
void |
setCachePathPolicy(int cachePathPolicy,
java.lang.String cachePathPolicyParam)
Deprecated.
use setCachePathPolicy(CachePathPolicy cachePathPolicy, String cachePathPolicyParam)
|
void |
setLogger(org.slf4j.Logger cacheLog)
Optionally set a logger.
|
void |
setNeverUseCache(boolean neverUseCache)
If true, never put the file in the cache.
|
void |
setPolicy(DiskCache2.CachePathPolicy cachePathPolicy)
Set the cache path policy
|
void |
setPolicy(int cachePathPolicy)
Deprecated.
use setCachePathPolicy(CachePathPolicy cachePathPolicy)
|
void |
setPolicy(java.lang.String policy) |
void |
setRootDirectory(java.lang.String cacheDir) |
void |
showCache(java.io.PrintStream pw)
Show cache contents.
|
java.lang.String |
toString() |
public DiskCache2(java.lang.String root, boolean reletiveToHome, int persistMinutes, int scourEveryMinutes)
root
- the root directory of the cache. Must be writeable.reletiveToHome
- if the root directory is relative to the cache home directory.persistMinutes
- a file is deleted if its last modified time is greater than persistMinutesscourEveryMinutes
- how often to run the scour process. If <= 0, don't scour.public static DiskCache2 getDefault()
public static DiskCache2 getNoop()
public void exit()
public void setLogger(org.slf4j.Logger cacheLog)
cacheLog
- use this loggerpublic void setRootDirectory(java.lang.String cacheDir)
public java.lang.String getRootDirectory()
public java.io.File getCacheFile(java.lang.String fileLocation)
fileLocation
- logical file locationpublic java.io.File getFile(java.lang.String fileLocation)
fileLocation
- logical file locationpublic static boolean canWrite(java.io.File f)
true
if we can write to the file.f
- a file. It may be a regular file or a directory. It may even be non-existent, in which case the
writability of the file's parent dir is tested.true
if we can write to the file.public java.io.File getExistingFileOrCache(java.lang.String fileLocation)
fileLocation
- the original namepublic java.io.File createUniqueFile(java.lang.String prefix, java.lang.String suffix)
prefix
- The prefix string to be used in generating the file's
name; must be at least three characters longsuffix
- The suffix string to be used in generating the file's
name; may be null
, in which case the
suffix ".tmp"
will be usedpublic void setPolicy(DiskCache2.CachePathPolicy cachePathPolicy)
cachePathPolicy
- one of:
OneDirectory (default) : replace "/" with "-", so all files are in one directory.
NestedDirectory: cache files are in nested directories under the root.
NestedTruncate: eliminate leading directoriespublic void setCachePathPolicy(DiskCache2.CachePathPolicy cachePathPolicy, java.lang.String cachePathPolicyParam)
cachePathPolicy
- one of:
OneDirectory (default) : replace "/" with "-", so all files are in one directory.
NestedDirectory: cache files are in nested directories under the root.
NestedTruncate: eliminate leading directoriescachePathPolicyParam
- for NestedTruncate, eliminate this stringpublic void setCachePathPolicy(int cachePathPolicy, java.lang.String cachePathPolicyParam)
public void setPolicy(int cachePathPolicy)
public void setPolicy(java.lang.String policy)
public void setAlwaysUseCache(boolean alwaysUseCache)
alwaysUseCache
- If true, always put the file in the cachepublic void setNeverUseCache(boolean neverUseCache)
neverUseCache
- If true, never put the file in the cachepublic void showCache(java.io.PrintStream pw)
pw
- write to this PrintStream.public void cleanCache(java.io.File dir, java.lang.StringBuilder sbuff, boolean isRoot)
dir
- clean starting heresbuff
- status messages here, may be nullisRoot
- delete empty directories, bit not root directorypublic java.lang.String toString()
toString
in class java.lang.Object