public interface ObjectCache<K,V>
try
... finally
block.
To use as a reader:
To overwrite:key = "EPSG:4326"; CoordinateReferenceSystem crs = cache.get(key);
To reserve the entry while figuring out what to write:cache.put(key, crs);
To use as a proper cache:try { cache.writeLock(key); // may block if another writer is working on this code. value = cache.peek(key); if (value == null) { // another writer got here first } else { value = figuringOutWhatToWrite(....); cache.put(key, value); } } finally { cache.writeUnLock(key); }
CylindricalCS cs = (CylindricalCS) cache.get(key); if (cs == null) { try { cache.writeLock(key); cs = (CylindricalCS) cache.test(key); if (cs == null) { cs = csAuthority.createCylindricalCS(code); cache.put(key, cs); } } finally { cache.writeUnLock(key); } } return cs;
https://jsr-107-interest.dev.java.net/javadoc/javax/cache/package-summary.html
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all entries from this cache.
|
V |
get(K key)
Returns an object from the pool for the specified code.
|
Set<K> |
getKeys()
Returns a set of all the keys currently contained within the ObjectCache.
|
V |
peek(K key)
Use the write lock to test the value for the provided key.
|
void |
put(K key,
V object)
Puts an element into the cache.
|
void |
remove(K key)
Removes a given key from the cache.
|
void |
writeLock(K key)
Acquire a write lock on the indicated key.
|
void |
writeUnLock(K key)
Release write lock on the indicated key.
|
void clear()
V get(K key)
referent
is returned.key
- The key whose associated value is to be returned.V peek(K key)
This method is used by a writer to test if someone (ie another writer) has provided the value for us (while we were blocked waiting for them).
null
void put(K key, V object)
You may simply use this method - it is threadsafe:
cache.put("4326", crs);You may also consider reserving the entry while you work on the answer:
try { cache.writeLock( "fred" ); ...find fred cache.put( "fred", fred ); } finally { cache.writeUnLock(); }
key
- the authority code.object
- The referencing object to add in the pool.void writeLock(K key)
void writeUnLock(K key)
Set<K> getKeys()
This is a static copy of the keys in the cache at the point in time when the function is called.
void remove(K key)
Copyright © 1996–2023 Geotools. All rights reserved.