Package uk.ac.starlink.ttools.plot2.data
Class DiskCache
java.lang.Object
uk.ac.starlink.ttools.plot2.data.DiskCache
Represents a persistent cache based on named files in a given
directory of a filesystem.
This class does not provide all the required facilities for
cache management, but it provides some methods which will be
useful during such management.
- Since:
- 16 Jan 2020
- Author:
- Mark Taylor
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Records that a file has been added to the cache.static String
formatByteSize
(long nbyte) Formats a string representing a number of bytes for human consumption.static long
getDefaultCacheLimit
(File dir) Returns a cacheLimit value that is suitable for general usage.getDir()
Returns the directory associated with this cache.static File
Returns the default system scratch directory.Returns a string summarising total cache files written to date.static String
Returns a default-length hash string corresponding to a given string.static String
Returns a configurable-length hash string corresponding to a given string.void
Logging utility function.static boolean
Creates a scratch directory and any missing parents, assigning write privileges for all or owner-only.void
ready()
Ensures that this cache's directory is ready for use.void
tidy()
Removes files from this cache's directory if it contains more data than the configured cache limit.static File
toCacheDir
(File baseDir, String label) Constructs a cache directory path given a base directory and a purpose-specific label.void
Updates the lastModified timestamp for a given file.static File
toWorkFilename
(File file) Produces a working filename to be used as temporary workspace when assembling a given destination filename.
-
Field Details
-
CACHE_DIRNAME
- See Also:
-
README_NAME
- See Also:
-
-
Constructor Details
-
DiskCache
Constructor.- Parameters:
dir
- directory into which cache files will be writtenlimit
- cache size indicator; if positive, it's the maximum cache size in bytes; if negative, it's the amount of space on the disk that cache usage tries to keep free; if zero, it's something adaptive
-
-
Method Details
-
getDir
Returns the directory associated with this cache.- Returns:
- cache directory
-
fileAdded
Records that a file has been added to the cache. This record is used to report persistently added files at shutdown. No check is made that the file in question actually was added.- Parameters:
f
- file added
-
ready
Ensures that this cache's directory is ready for use. Should be called before the cache is used.- Throws:
IOException
-
log
Logging utility function. This just passes a message to the logging system, using the logging level associated with this instance.- Parameters:
txt
- message to log
-
tidy
public void tidy()Removes files from this cache's directory if it contains more data than the configured cache limit. -
getWriteSummary
Returns a string summarising total cache files written to date.- Returns:
- summary text
-
touch
Updates the lastModified timestamp for a given file.- Parameters:
file
- file to touch
-
hashText
Returns a default-length hash string corresponding to a given string. This doesn't have to be cryptographically secure, but accidental collisions are to be avoided.- Parameters:
txt
- text to hash- Returns:
- fixed-length hash string
-
hashText
Returns a configurable-length hash string corresponding to a given string. This doesn't have to be cryptographically secure, but accidental collisions are to be avoided.- Parameters:
txt
- text to hashnchar
- number of characters in output (currently up to 32)- Returns:
- hash string
-
toWorkFilename
Produces a working filename to be used as temporary workspace when assembling a given destination filename.We could just use one of the
File.createTempFile
methods, but attempt to come up with a name that is (a) likely to be unique and (b) bears some resemlance to the requested destination file, for ease of debugging etc.- Parameters:
file
- destination file- Returns:
- workspace file
-
formatByteSize
Formats a string representing a number of bytes for human consumption.- Parameters:
nbyte
- byte count- Returns:
- storage size string
-
getSystemTmpDir
Returns the default system scratch directory. This is supplied from thejava.io.tmpdir
system property.- Returns:
- scratch directory
-
toCacheDir
Constructs a cache directory path given a base directory and a purpose-specific label.- Parameters:
baseDir
- base directory; if null, java.io.tmpdir is usedlabel
- purpose-specific label (used for subdirectory name)- Returns:
- directory to which cache files can be written
-
mkdirs
Creates a scratch directory and any missing parents, assigning write privileges for all or owner-only. This is likeFile.mkdirs()
, but it allows the option of assigning global write privileges to any created directories.- Parameters:
dir
- target directoryownerOnly
- true to make created directories writable by ownwer only, false to make them writable by all- Returns:
- true if creation succeeded
-
getDefaultCacheLimit
Returns a cacheLimit value that is suitable for general usage. The return value is negative, meaning that it corresponds to requiring a certain number of bytes free on the filesystem.- Parameters:
dir
- cache directory
-