|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface ObjectCache
A cache for arbitrary objects. Cache implementations are thread-safe and support concurrency.
A cache entry can be locked when an object is in process of being created, but the locking /
unlocking must be protected in a 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
Method Summary | |
---|---|
void |
clear()
Removes all entries from this cache. |
java.lang.Object |
get(java.lang.Object key)
Returns an object from the pool for the specified code. |
java.util.Set<java.lang.Object> |
getKeys()
Returns a set of all the keys currently contained within the ObjectCache. |
java.lang.Object |
peek(java.lang.Object key)
Use the write lock to test the value for the provided key. |
void |
put(java.lang.Object key,
java.lang.Object object)
Puts an element into the cache. |
void |
remove(java.lang.Object key)
Removes a given key from the cache. |
void |
writeLock(java.lang.Object key)
Acquire a write lock on the indicated key. |
void |
writeUnLock(java.lang.Object key)
Release write lock on the indicated key. |
Method Detail |
---|
void clear()
java.lang.Object get(java.lang.Object key)
referent
is returned.
key
- The key whose associated value is to be returned.java.lang.Object peek(java.lang.Object 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).
key
-
null
void put(java.lang.Object key, java.lang.Object 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(java.lang.Object key)
key
- void writeUnLock(java.lang.Object key)
key
- java.util.Set<java.lang.Object> getKeys()
This is a static copy of the keys in the cache at the point in time when the function is called.
void remove(java.lang.Object key)
key
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |