|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object tribble.io.DiskCacheManager
public class DiskCacheManager
Disk file cache manager.
Manages a set of files in a local disk directory as a cache of document files.
Usage
To create a cache that stores files in a local disk directory, create a disk cache manager object, and establish the local directory and index file it should use:
try { DiskCacheManager cache; File dir; dir = ...; cache = new DiskCacheManager(); cache.open(dir, "mycache.ind"); } catch (IOException ex) { ... error ... }
New document files can be added to the directory cache:
try { File fname; String docId; docId = ...; fname = cache.createFile(id); } catch (IOException ex) { ... error ... }
The createFile(java.lang.String)
method creates a new file within the cache directory
and returns its name. The document-ID specified must be unique within the
cache, i.e., no other document file within the cache can have the same
document-ID. (A IOException
is thrown if the document-ID is
not unique.) The returned filename is guaranteed to be unique within the
cache directory. From that point on, there is a one-to-one correspondence
between the document-ID and the cached filename.
By default, the created filenames have a name that is prefixed with "temp" and suffixed with "tmp". However, these can be changed by client code:
cache.setFilePrefix("mine"); // Or whatever cache.setFileSuffix("doc"); // Or whateverData can be written to and read from the cached filename by client code. The file need not remain open (or be opened at all, for that matter) while the cache manager is active.
Once the cached filename is no longer needed, it can be removed from the cached directory:
try { cache.removeFile(id); } catch (IOException ex) { ... error ... }Once this is done, there is no longer any association between the document-ID and any filename in the cache directory.
Notes
A control file is maintained in the local cache directory, containing the names of all of the files that currently reside in the cache. This file is updated any time a document file is added to or removed from the cache.
The control file is read whenever the cache directory is opened by a document cache manager object, in order to re-establish the contents of the cache directory.
The default name of the control file is "cache.ind", but this can be specified when calling the
open(java.io.File, java.lang.String)
method.To allow multiple cache directory managers to use the same disk directory, a lock file is used to insure mutually exclusive access to the directory.
Field Summary | |
---|---|
static java.lang.String |
CONTROL_FILE_VERS
Cache directory control file format version number. |
static java.lang.String |
DFL_CONTROL_FILENAME
Default cache directory control filename. |
static int |
DFL_EXPIRY_DAYS
Default number of days until cached files expire. |
static java.lang.String |
DFL_PREFIX
Default cache filename prefix. |
static java.lang.String |
DFL_SUFFIX
Default cache filename suffix. |
(package private) static char |
ENTRY_SEP
Cache control file entry field separator. |
protected java.io.File |
m_dir
Local cache directory name. |
protected int |
m_expiryDays
Days past the last access date that document files expire. |
protected java.util.HashMap |
m_files
Files currently residing in the local directory cache. |
protected java.io.File |
m_indexFname
Local cache directory index (control) file. |
protected java.io.File |
m_lockFname
Local cache directory lock (mutex) file. |
protected java.lang.String |
m_pref
Cache filename prefix. |
protected java.lang.String |
m_suff
Cache filename suffix. |
(package private) static java.lang.String |
REV
Revision information. |
Constructor Summary | |
---|---|
DiskCacheManager()
Default constructor. |
Method Summary | |
---|---|
void |
close()
Close this cache directory manager. |
java.io.File |
createFile(java.lang.String docId)
Create a new cached filename in the cache directory for a given document-ID. |
protected void |
finalize()
Finalization. |
protected DiskCacheFile |
findFile(java.lang.String docId)
Locate the cached filename for a given document-ID within the cache index. |
java.io.File |
getFile(java.lang.String docId)
Retrieve the cached filename for a given document-ID within the cache directory. |
protected void |
loadEntries()
Read the index table for this cache directory manager from the cache directory. |
protected void |
lock()
Acquire an exclusive lock on the index file of this cache directory. |
void |
open(java.io.File dir,
java.lang.String index)
Open the disk cache directory and initialize this cache manager. |
protected static java.lang.String |
readEntryLine(java.io.BufferedReader in)
Read a text line from the cache directory index (control) file. |
void |
removeAllExpiredFiles()
Delete all expired document files from this cache directory. |
void |
removeAllFiles()
Delete all document files from this cache directory. |
protected void |
removeExpiredEntries()
Remove all the expired document files from the cache directory. |
void |
removeFile(java.lang.String docId)
Delete a given document file from the cache directory. |
void |
setExpiryDays(int nDays)
Establish the minimum lifetime of document files that reside in this cache directory. |
void |
setFilePrefix(java.lang.String pre)
Set the filename prefix for document files in this cache directory. |
void |
setFileSuffix(java.lang.String suf)
Set the filename suffix for document files in this cache directory. |
protected void |
storeEntries()
Write the index table for this cache directory manager to the cache directory. |
protected void |
unlock()
Release an exclusive lock on the index file of this cache directory. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
static final java.lang.String REV
public static final java.lang.String DFL_CONTROL_FILENAME
public static final java.lang.String CONTROL_FILE_VERS
public static final java.lang.String DFL_PREFIX
public static final java.lang.String DFL_SUFFIX
public static final int DFL_EXPIRY_DAYS
static final char ENTRY_SEP
protected java.io.File m_dir
protected java.io.File m_indexFname
protected java.io.File m_lockFname
protected java.lang.String m_pref
protected java.lang.String m_suff
protected int m_expiryDays
protected java.util.HashMap m_files
This is a hash table keyed by document-IDs, which are unique to
each document file contained in the local cache directory. The value of
each hash entry is a DiskCacheFile
object that contains the name
of the file in the local cache, as well as other information, for the
document file.
Constructor Detail |
---|
public DiskCacheManager()
Method Detail |
---|
public void open(java.io.File dir, java.lang.String index) throws java.io.IOException
open
in interface FileCacheManagerI
dir
- A directory name to be used as the local file cache.index
- A filename residing within the local cache directory to be used as a index
(control) file. This can be null or empty (""), in which case
a default index filename is used.
java.io.IOException
- Thrown if access to the directory is denied or some other error occurs.close()
public void close() throws java.io.IOException
close
in interface FileCacheManagerI
java.io.IOException
- Thrown if access to the directory is denied or some other error occurs.open(java.io.File, java.lang.String)
public void setFilePrefix(java.lang.String pre) throws java.io.IOException
pre
- A filename prefix. This should generally be a fairly short string, such
as three characters.
java.io.IOException
- Thrown if pre is malformed.createFile(java.lang.String)
public void setFileSuffix(java.lang.String suf) throws java.io.IOException
suf
- A filename suffix. This should generally be a fairly short string, such
as three characters, plus a leading dot ('.').
java.io.IOException
- Thrown if suf is malformed.createFile(java.lang.String)
public void setExpiryDays(int nDays)
nDays
- The minimum number of days beyond their last access date that document
files are allowed to reside in the cache directory before being
automatically deleted.
If nDays is zero or negative, cached files will be deleted when
method close()
is called for this cache directory.
By default, cached files will exist for a minimum of
DFL_EXPIRY_DAYS
days.
removeAllExpiredFiles()
public java.io.File createFile(java.lang.String docId) throws java.io.IOException
createFile
in interface FileCacheManagerI
docId
- A document-ID to create. This name must be unique within this cache.
This name may contain any characters except newlines ('\n'),
although it is recommended that it be composed of only printable
characters.
java.io.IOException
- Thrown if document-ID docId already exists in the cache, or if
the cache directory could not be accessed, or if some other error
occurs.getFile(java.lang.String)
,
removeFile(java.lang.String)
public java.io.File getFile(java.lang.String docId) throws java.io.IOException
This method updates the last access time for the cached file.
getFile
in interface FileCacheManagerI
docId
- A document-ID to locate.
java.io.IOException
- Thrown if the document file does not exist, or if the cache directory
could not be accessed, or if some other error occurs.createFile(java.lang.String)
,
removeFile(java.lang.String)
public void removeFile(java.lang.String docId) throws java.io.IOException
removeFile
in interface FileCacheManagerI
docId
- The document-ID of a document file to delete from the cache directory.
java.io.IOException
- Thrown if the document file does not exist, or if the cache directory
could not be accessed, or if some other error occurs.removeAllFiles()
,
removeAllExpiredFiles()
public void removeAllFiles() throws java.io.IOException
removeAllFiles
in interface FileCacheManagerI
java.io.IOException
- Thrown if the cache directory could not be accessed, or if some other
error occurs.removeFile(java.lang.String)
,
removeAllExpiredFiles()
public void removeAllExpiredFiles() throws java.io.IOException
java.io.IOException
- Thrown if the cache directory could not be accessed, or if some other
error occurs.setExpiryDays(int)
,
removeAllFiles()
protected static java.lang.String readEntryLine(java.io.BufferedReader in) throws java.io.IOException
Comment lines (which have a '#' in the leftmost column) are ignored.
java.io.IOException
- Thrown if a read error occurs, or if some other error occurs.storeEntries()
protected void finalize()
finalize
in class java.lang.Object
close()
protected DiskCacheFile findFile(java.lang.String docId) throws java.io.IOException
docId
- A document-ID to locate.
java.io.IOException
- Thrown if document-ID docId does not exist within this cache.getFile(java.lang.String)
protected void loadEntries() throws java.io.IOException
java.io.IOException
- Thrown if a read error occurs, or if some other error occurs.storeEntries()
protected void storeEntries() throws java.io.IOException
java.io.IOException
- Thrown if a write error occurs, or if some other error occurs.loadEntries()
protected void removeExpiredEntries() throws java.io.IOException
java.io.IOException
- +INCOMPLETEremoveAllExpiredFiles()
,
DiskCacheFile.hasExpired(java.util.Date)
protected void lock() throws java.io.IOException
java.io.IOException
- Thrown if an I/O error occurs.protected void unlock() throws java.io.IOException
java.io.IOException
- Thrown if an I/O error occurs.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |