ucar.unidata.io
Class FileCache

java.lang.Object
  extended by ucar.unidata.io.FileCache

public class FileCache
extends java.lang.Object

Keep cache of open RandomAccessFile, for performance. Used by TDS to optimize serving netCDF files over HTTP.

  RandomAccessFile raf = null;
  try {
    RandomAccessFile raf = FileCache.acquire(location, cancelTask);
    ...
  } finally {
    FileCache.release( raf)
  }
 

Library ships with cache disabled. If you want to use, call init() and make sure you call exit() when exiting program. All methods are thread safe. Cleanup is done automatically in a background thread, using LRU.

Author:
jcaron

Nested Class Summary
static class FileCache.CacheElement
          This tracks the elements in the cache.
 
Constructor Summary
FileCache()
           
 
Method Summary
static RandomAccessFile acquire(java.lang.String location)
           
static RandomAccessFile acquireCacheOnly(java.lang.String location)
          Try to find a file in the cache.
static void clearCache(boolean force)
           
static void disable()
          Disable use of the cache.
static void exit()
          You must call exit() to shut down the background timer in order to get a clean process shutdpwn.
static java.util.List getCache()
          Get the files in the cache.
static void init()
          Default 10 minimum, 20 maximum files, cleanup every 20 minutes
static void init(int minElementsInMemory, int maxElementsInMemory, long period)
          Initialize the cache.
static void release(RandomAccessFile raf)
          Release the file.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileCache

public FileCache()
Method Detail

init

public static void init()
Default 10 minimum, 20 maximum files, cleanup every 20 minutes


init

public static void init(int minElementsInMemory,
                        int maxElementsInMemory,
                        long period)
Initialize the cache. If you dont, default values will be used. Call disable if you dont want caching.

Parameters:
minElementsInMemory - keep this number in the cache
maxElementsInMemory - trigger a cleanup if it goes over this number.
period - (secs) do periodic cleanups every this number of seconds.

disable

public static void disable()
Disable use of the cache. Call init() to start again. Generally call this before any possible use.


exit

public static void exit()
You must call exit() to shut down the background timer in order to get a clean process shutdpwn.


acquireCacheOnly

public static RandomAccessFile acquireCacheOnly(java.lang.String location)
Try to find a file in the cache.

Parameters:
location - file location is used as the key.
Returns:
file if its in the cache, null otherwise.

acquire

public static RandomAccessFile acquire(java.lang.String location)
                                throws java.io.IOException
Throws:
java.io.IOException

release

public static void release(RandomAccessFile raf)
                    throws java.io.IOException
Release the file. This unlocks it, updates its lastAccessed date.

Parameters:
raf - release this file.
Throws:
java.io.IOException - if file not in cache.

getCache

public static java.util.List getCache()
Get the files in the cache. For debugging/status only, do not change!

Returns:
List of FileCache.CacheElement

clearCache

public static void clearCache(boolean force)