com.marringtons.file
Class FileCache

java.lang.Object
  extended bycom.marringtons.file.FileCache

public class FileCache
extends Object

This class caches a defined number of disk files for reading to reduce disk access for files that are accessed repetively. It looks in the properties file for a key "file.read.cache.size" for this number. Given a file name it uses FileReader to look for and find the file. Once a file is read once it is kept in the cache and retrieved from memory when needed. When the cache fills files the least recently used files are removed. For a single-user application a small cache size of 10 would be adequate. For an Internet or Intranet application the cache size should be at least 10 per expected user. Use larger cache sizes for larger file groups.

 byte[] buffer = FileCache.fetch(fileName);
 if (buffer == null) Application.log("Can't file file " + fileName);
 

Author:
Paul Marrington

Field Summary
static boolean disable
          set to true to disable caching.
 
Constructor Summary
FileCache(int size)
          Create a new file cache with a fixed size specified in code.
FileCache(String size)
          Create a new file cache with a fixed size from a properties file.
FileCache(String size, int defaultSize)
          Create a new file cache with a fixed size from a properties file and a default value if the property does not exist.
 
Method Summary
 void close()
          Close cache when finished with it.
 byte[] fetch(String fileName)
          Fetch a file either from the cache or from disk.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

disable

public static boolean disable
set to true to disable caching. Only use in test code when changing HTML and/or JavaScript

Constructor Detail

FileCache

public FileCache(int size)
Create a new file cache with a fixed size specified in code.

Parameters:
size - maximum number of entries before LRU entries are purged.

FileCache

public FileCache(String size)
Create a new file cache with a fixed size from a properties file.

Parameters:
size - properties key for maximum number of entries before LRU entries are purged.

FileCache

public FileCache(String size,
                 int defaultSize)
Create a new file cache with a fixed size from a properties file and a default value if the property does not exist.

Parameters:
size - properties key for maximum number of entries before LRU entries are purged.
defaultSize - size to use if there is none defined in property
Method Detail

fetch

public byte[] fetch(String fileName)
             throws IOException
Fetch a file either from the cache or from disk. FileReader is used to find and read the file.

Parameters:
fileName - name of file to retrieve from disk or cache
Returns:
byte[] contents of the file.
Throws:
IOException

close

public void close()
Close cache when finished with it. Only needed to stop Cache complaining during finalize().



Copyright © 2005 Paul Marrington http://library.marringtons.com