Java Reference
In-Depth Information
public boolean cached = false; D Instructional field, true for cache hits
protected ShowBoardCommand boardCommand = null;
private static java.util.Hashtable boardCache = null;
E This is the cache.
Note static.
F Initialize is
lighter than
usual.
public void initialize ()
throws
IOException,
DataException {
G Synchronization
controls access.
synchronized(boardCache) {
if (boardCache == null) {
boardCache = new Hashtable();
}
}
}
H Invalidate
handles changed
data.
public void invalidate(String key) {
synchronized(boardCache) {
getBoardCache().remove(key);
}
}
I Execute will try
cache first.
public void execute ()
throws
IOException,
DataException {
synchronized(boardCache) { J Synchronized to protect static cache
try {
// Cached is an instructional flag to show whether a
// value was fetched from the cache.
cached = true;
Hashtable cache = getBoardCache();
if (cache == null) {
throw new Exception();
}
boardCommand = (ShowBoardCommand) cache.get(getBoard());
if (boardCommand == null) {
boardCommand = new ShowBoardCommand();
boardCommand.setBoard(getBoard());
boardCommand.initialize();
boardCommand.execute();
cache.put(getBoard(), boardCommand);
fire a command. 1)
Cache missed, so
cached = false;
}
} catch (Throwable theException) {
theException.printStackTrace();
}
}
1!
Most getters
pass through.
public String getAuthor(int index)
throws
IndexOutOfBoundsException,
Search WWH ::




Custom Search