Caching
You might want to always write temporary files to the cache directory, in order to manage them in a central place. To do so, call
For long running appplication, you might want to do this periodically in a background timer thread, as in the following example. In 4.0, the cache policy for GRIB indexes is set seperately from generic DiskCache, in order to give you seperate control: Note that you still control whether to alway use a cache directory, and where that is located with DiskCache methods: In multi-threaded situations such as a server, you need to make sure that grib indexing is thread-safe. One way to do this is to generate the indexes ahead of time, then tell the library to not write the index, but only use files that already have an index: See GRIB decoder for details on generating a GRIB index externally. See GribGridServiceProvider javadoc for more details. NetcdfFile objects are cached in memory for performance. When acquired, the object is locked so another thread cannot use. When closed, the lock is removed. When the cache is full, older objects are removed from the cache, and all resources released. Note that typically a java.io.RandomAccessFile object, holding an OS file handle, is open while its in the cache. You must make sure that your cache size is not so large such that you run out of file handles due to NetcdfFile object caching. Most aggregations do not hold more than one file handle open, no matter how many files are in the aggregation. The exception to that is a Union aggregation, which holds each of the files in the union open for the duration of the NetcdfFile object. Holding a file handle open also creates a read lock on some operating systems, which will prevent the file from being opened in write mode. To enable caching, you must first call where minElementsInMemory are the number of objects to keep in the cache when cleaning up, maxElementsInMemory triggers a cleanup if the cache size goes over it, and period specifies the time in seconds to do periodic cleanups. After enabling, you can disable with: However, you cant reenable after disabling. Setting minElementsInMemory to zero will remove all files not currently in use every period seconds. Normally the cleanup is done is a background thread to not interferre with your application, and the maximum elements is approximate. When resources such as file handles must be carefully managed, you can set a hard limit with this call: so that as soon as the number of NetcdfFile objects exceeds hardLimit , a cleanup is done immediately in the calling thread.
You may want to limit the amount of space the disk cache uses (unless you always have data in writeable directories, so that the disk cache is never used). To scour the cache, call DiskCache.cleanCache(). There are several variations of the cleanup:
1) Calendar c = Calendar.getInstance(); // contains current startup time
c.add( Calendar.MINUTE, 30); // add 30 minutes to current time // run task every 60 minutes, starting 30 minutes from now
2) java.util.Timer timer = new Timer();
timer.scheduleAtFixedRate( new CacheScourTask(), c.getTime(), (long) 1000 * 60 * 60 );
3) private class CacheScourTask extends java.util.TimerTask {
public void run() {
StringBuffer sbuff = new StringBuffer();
4) DiskCache.cleanCache(100 * 1000 * 1000, sbuff); // 100 Mbytes
sbuff.append("----------------------\n");
5) log.info(sbuff.toString());
}
}
...
// upon exiting
6) timer.cancel();
GRIB indexing
GribGridServiceProvider.setIndexAlwaysInCache( true); // always use the cache for grib index
ucar.nc2.util.DiskCache.setRootDirectory(String cacheDir) GribGridServiceProvider.setIndexExtendMode( IndexExtendMode.none); // never write an index
GribGridServiceProvider.setIndexSyncMode( IndexExtendMode.none); // never sync the index
Object Caching
NetcdfFileCache
NetcdfDataset.initNetcdfFileCache(int minElementsInMemory, int maxElementsInMemory, int period);
NetcdfDataset.disableNetcdfFileCache();
NetcdfDataset.initNetcdfFileCache(int minElementsInMemory, int maxElementsInMemory, int hardLimit, int period);
Collection Caching Directory Scans and Metadata
ControllerCaching
DiskCache2 cacheDir = new DiskCache2(".unidata/ehcache", true, -1, -1);
ControllerCaching cacheManager = thredds.filesystem.ControllerCaching.makeTestController(cacheDir.getRootDirectory());
thredds.inventory.DatasetCollectionMFiles.setController(cacheManager);
FMRC Caching
This document is maintained by John Caron and was last updated Oct 2010