@ThreadSafe public class FileCache extends Object implements FileCacheIF
NetcdfDataset.initNetcdfFileCache(...); // on application startup ... NetcdfFile ncfile = null; try { ncfile = NetcdfDataset.acquireFile(location, cancelTask); ... } finally { if (ncfile != null) ncfile.close(); } ... NetcdfDataset.shutdown(); // when terminating the applicationAll methods are thread safe. Cleanup is done automatically in a background thread, using LRU algorithm.
Modifier and Type | Field and Description |
---|---|
protected ConcurrentHashMap<Object,ucar.nc2.util.cache.FileCache.CacheElement> |
cache |
protected static org.slf4j.Logger |
cacheLog |
protected AtomicInteger |
cleanups |
protected ConcurrentHashMap<FileCacheable,ucar.nc2.util.cache.FileCache.CacheElement.CacheFile> |
files |
protected int |
hardLimit |
protected AtomicBoolean |
hasScheduled |
protected AtomicInteger |
hits |
protected static org.slf4j.Logger |
log |
protected int |
minElements |
protected AtomicInteger |
miss |
protected String |
name |
protected int |
period |
protected int |
softLimit |
protected ConcurrentHashMap<Object,ucar.nc2.util.cache.FileCache.Tracker> |
track |
protected boolean |
trackAll |
Constructor and Description |
---|
FileCache(int minElementsInMemory,
int maxElementsInMemory,
int period)
Constructor.
|
FileCache(int minElementsInMemory,
int softLimit,
int hardLimit,
int period)
Constructor.
|
FileCache(String name,
int minElementsInMemory,
int softLimit,
int hardLimit,
int period)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
FileCacheable |
acquire(FileFactory factory,
Object hashKey,
String location,
int buffer_size,
CancelTask cancelTask,
Object spiObject)
Acquire a FileCacheable from the cache, and lock it so no one else can use it.
|
FileCacheable |
acquire(FileFactory factory,
String location) |
FileCacheable |
acquire(FileFactory factory,
String location,
CancelTask cancelTask)
Acquire a FileCacheable, and lock it so no one else can use it.
|
void |
clearCache(boolean force)
Remove all cache entries.
|
void |
disable()
Disable the cache, and force release all files.
|
void |
eject(Object hashKey)
Remove all instances of object from the cache
|
void |
enable()
Enable the cache, with the current set of parameters.
|
String |
getInfo(FileCacheable ncfile) |
boolean |
release(FileCacheable ncfile)
Release the file.
|
void |
resetTracking() |
List<String> |
showCache() |
void |
showCache(Formatter format)
Show individual cache entries, add to formatter.
|
void |
showStats(Formatter format)
Add stat report (hits, misses, etc) to formatter.
|
void |
showTracking(Formatter format) |
static void |
shutdown()
You must call shutdown() to shut down the background threads in order to get a clean process shutdown.
|
protected static final org.slf4j.Logger log
protected static final org.slf4j.Logger cacheLog
protected String name
protected final int softLimit
protected final int minElements
protected final int hardLimit
protected final int period
protected final AtomicBoolean hasScheduled
protected final ConcurrentHashMap<Object,ucar.nc2.util.cache.FileCache.CacheElement> cache
protected final ConcurrentHashMap<FileCacheable,ucar.nc2.util.cache.FileCache.CacheElement.CacheFile> files
protected final AtomicInteger cleanups
protected final AtomicInteger hits
protected final AtomicInteger miss
protected ConcurrentHashMap<Object,ucar.nc2.util.cache.FileCache.Tracker> track
protected boolean trackAll
public FileCache(int minElementsInMemory, int maxElementsInMemory, int period)
minElementsInMemory
- keep this number in the cachemaxElementsInMemory
- trigger a cleanup if it goes over this number.period
- (secs) do periodic cleanups every this number of seconds.public FileCache(int minElementsInMemory, int softLimit, int hardLimit, int period)
minElementsInMemory
- keep this number in the cachesoftLimit
- trigger a cleanup if it goes over this number.hardLimit
- if > 0, never allow more than this many elements. This causes a cleanup to be done in the calling thread.period
- if > 0, do periodic cleanups every this number of seconds.public FileCache(String name, int minElementsInMemory, int softLimit, int hardLimit, int period)
name
- of file cacheminElementsInMemory
- keep this number in the cachesoftLimit
- trigger a cleanup if it goes over this number.hardLimit
- if > 0, never allow more than this many elements. This causes a cleanup to be done in the calling thread.period
- if > 0, do periodic cleanups every this number of seconds.public static void shutdown()
public void disable()
disable
in interface FileCacheIF
public void enable()
enable
in interface FileCacheIF
public FileCacheable acquire(FileFactory factory, String location, CancelTask cancelTask) throws IOException
factory
- use this factory to open the file; may not be nulllocation
- file location, also used as the cache name, will be passed to the NetcdfFileFactorycancelTask
- user can cancel, ok to be null.IOException
- on errorpublic FileCacheable acquire(FileFactory factory, String location) throws IOException
acquire
in interface FileCacheIF
IOException
public FileCacheable acquire(FileFactory factory, Object hashKey, String location, int buffer_size, CancelTask cancelTask, Object spiObject) throws IOException
acquire
in interface FileCacheIF
factory
- use this factory to open the file if not in the cache; may not be nullhashKey
- unique key for this file. If null, the location will be usedlocation
- file location, may also used as the cache name, will be passed to the NetcdfFileFactorybuffer_size
- RandomAccessFile buffer size, if <= 0, use default sizecancelTask
- user can cancel, ok to be null.spiObject
- passed to the factory if object needs to be recreatedIOException
- on errorpublic void eject(Object hashKey)
eject
in interface FileCacheIF
hashKey
- the objectpublic boolean release(FileCacheable ncfile) throws IOException
release
in interface FileCacheIF
ncfile
- release this file.IOException
public String getInfo(FileCacheable ncfile) throws IOException
IOException
public void clearCache(boolean force)
clearCache
in interface FileCacheIF
force
- if true, remove them even if they are currently locked.public void showCache(Formatter format)
showCache
in interface FileCacheIF
format
- add to thispublic List<String> showCache()
showCache
in interface FileCacheIF
public void showStats(Formatter format)
showStats
in interface FileCacheIF
format
- add to thispublic void showTracking(Formatter format)
showTracking
in interface FileCacheIF
public void resetTracking()
resetTracking
in interface FileCacheIF
Copyright © 1999–2017 UCAR/Unidata. All rights reserved.