Package org.wso2.carbon.caching.impl
Class CacheImpl<K,V>
java.lang.Object
org.wso2.carbon.caching.impl.CacheImpl<K,V>
- All Implemented Interfaces:
Iterable<Cache.Entry<K,,V>> Cache<K,,V> CacheLifecycle
TODO: class description
TODO: Cache statistics
-
Nested Class Summary
Nested classes/interfaces inherited from interface javax.cache.Cache
Cache.Entry<K,V>, Cache.EntryProcessor<K, V>, Cache.MutableEntry<K, V> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(K key) Returns true if this cache contains a mapping for the specified key.booleanvoidvoidGets an entry from the cache.getAll()The getAll method will return, from the cache, aMapof the objects associated with the Collection of keys in argument "keys".Associates the specified value with the specified key in this cache, returning an existing value if one existed.getAndRemove(K key) Atomically removes the entry for a key only if currently mapped to a given value.getAndReplace(K key, V value) Atomically replaces the entry for a key only if currently mapped to some value.Gets the CacheManager managing this cache.Returns a CacheConfiguration.getMBean()Get the MBean for this cache.getName()Return the name of the cache.Returns theCacheStatisticsMXBean associated with the cache.Returns the cache status.inthashCode()invokeEntryProcessor(K key, Cache.EntryProcessor<K, V> entryProcessor) Passes the cache entry associated with K to be passed to the entry processor.iterator()The ordering of the entries is undefinedkeys()The load method provides a means to "pre-load" the cache.The loadAll method provides a means to "pre-load" objects into the cache.voidAssociates the specified value with the specified key in this cache If the cache previously contained a mapping for the key, the old value is replaced by the specified value.voidCopies all of the mappings from the specified map to this cache.booleanputIfAbsent(K key, V value) Atomically associates the specified key with the given value if it is not already associated with a value.voidputIfNoDuplicate(K key, V value) Associates the specified value with the specified key in this cache If the cache previously contained a mapping for the key, the old value is replaced by the specified value if the value is not equal to the value already existing in the cache.voidAssociates the specified value with the specified key in this cache only if the cache does not already contain a value for the key.booleanregisterCacheEntryListener(CacheEntryListener<? super K, ? super V> cacheEntryListener) Adds a listener to the notification service.booleanRemoves the mapping for a key from this cache if it is present.booleanAtomically removes the mapping for a key only if currently mapped to the given value.voidRemoves all of the mappings from this cache.voidRemoves entries for the specified keys.voidThis method is added to only remove the cache locally.booleanremoveLocal(Object key) This method is added to only remove the cache locally.booleanAtomically replaces the entry for a key only if currently mapped to some value.booleanAtomically replaces the entry for a key only if currently mapped to a given value.voidsetCacheConfiguration(CacheConfigurationImpl cacheConfiguration) voidsetCapacity(long capacity) voidsetEvictionAlgorithm(EvictionAlgorithm evictionAlgorithm) voidstart()Notifies providers to start themselves.voidstop()Providers may be doing all sorts of exotic things and need to be able to clean up on stop.voidDeprecated.This method is highly inefficient.booleanunregisterCacheEntryListener(CacheEntryListener<?, ?> cacheEntryListener) Removes a call back listener.<T> TReturn an object of the specified type to allow access to the provider-specific API.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
CacheImpl
-
-
Method Details
-
get
Description copied from interface:CacheGets an entry from the cache. If the cache is configured read-through, and get would return null because the entry is missing from the cache, the Cache'sCacheLoaderis called which will attempt to load the entry.Effects:
- Expiry - updates
Access Time. - Read-Through - will use the
CacheLoaderif enabled and key not present in cache
- Expiry - updates
-
getAll
Description copied from interface:CacheThe getAll method will return, from the cache, aMapof the objects associated with the Collection of keys in argument "keys". If the cache is configured read-through, and a get would return null because an entry is missing from the cache, the Cache'sCacheLoaderis called which will attempt to load the entry. This is done for each key in the collection for which this is the case. If an entry cannot be loaded for a given key, the key will not be present in the returned Map. -
syncCaches
public void syncCaches()Deprecated.This method is highly inefficient. Do not use. -
getAll
-
containsKey
Description copied from interface:CacheReturns true if this cache contains a mapping for the specified key. More formally, returns true if and only if this cache contains a mapping for a key k such that key.equals(k). (There can be at most one such mapping.)- Specified by:
containsKeyin interfaceCache<K,V> - Parameters:
key- key whose presence in this cache is to be tested.- Returns:
- true if this map contains a mapping for the specified key
- See Also:
-
load
Description copied from interface:CacheThe load method provides a means to "pre-load" the cache. This method will, asynchronously, load the specified object into the cache using the associatedCacheLoaderfor the given key. If the object already exists in the cache, no action is taken and null is returned. If no loader is associated with the cache no object will be loaded into the cache and null is returned. If a problem is encountered during the retrieving or loading of the object, an exception must be propagated onFuture.get()as aExecutionException -
loadAll
Description copied from interface:CacheThe loadAll method provides a means to "pre-load" objects into the cache. This method will, asynchronously, load the specified objects into the cache using the associated cache loader for the given keys. If the an object already exists in the cache, no action is taken. If no loader is associated with the object, no object will be loaded into the cache. If a problem is encountered during the retrieving or loading of the objects, an exception (to be defined) should be logged. If a problem is encountered during the retrieving or loading of the object, an exception must be propagated onFuture.get()as aExecutionException -
getStatistics
Description copied from interface:CacheReturns theCacheStatisticsMXBean associated with the cache. May return null if the cache does not support statistics gathering.- Specified by:
getStatisticsin interfaceCache<K,V> - Returns:
- the CacheStatisticsMBean
-
put
Description copied from interface:CacheAssociates the specified value with the specified key in this cache If the cache previously contained a mapping for the key, the old value is replaced by the specified value. (A cache c is said to contain a mapping for a key k if and only ifc.containsKey(k)would return true.) In contrast to the corresponding Map operation, does not return the previous value. -
putOnRead
Description copied from interface:CacheAssociates the specified value with the specified key in this cache only if the cache does not already contain a value for the key. This method is intended for use when populating the cache as part of read operations. In clustered deployments, concurrent read operations may retrieve the same value from the underlying data store. Using this method prevents those concurrent reads from overwriting an existing cached value and triggering unnecessary cache-entry update notifications across the cluster. If an entry for the specified key already exists, the cache is left unchanged. In contrast toCache.put(Object, Object), this method does not replace existing values. -
putIfNoDuplicate
Description copied from interface:CacheAssociates the specified value with the specified key in this cache If the cache previously contained a mapping for the key, the old value is replaced by the specified value if the value is not equal to the value already existing in the cache. (A cache c is said to contain a mapping for a key k if and only ifc.containsKey(k)would return true.) In contrast to the corresponding Map operation, does not return the previous value.- Specified by:
putIfNoDuplicatein interfaceCache<K,V> - Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- See Also:
-
getAndPut
Description copied from interface:CacheAssociates the specified value with the specified key in this cache, returning an existing value if one existed. If the cache previously contained a mapping for the key, the old value is replaced by the specified value. (A cache c is said to contain a mapping for a key k if and only ifc.containsKey(k)would return true.) The the previous value is returned, or null if there was no value associated with the key previously. -
putAll
Description copied from interface:CacheCopies all of the mappings from the specified map to this cache. The effect of this call is equivalent to that of callingput(k, v)on this cache once for each mapping from key k to value v in the specified map. The order in which the individual puts will occur is undefined. The behavior of this operation is undefined if the specified cache or map is modified while the operation is in progress. -
putIfAbsent
Description copied from interface:CacheAtomically associates the specified key with the given value if it is not already associated with a value. This is equivalent toif (!cache.containsKey(key)) {} cache.put(key, value); return true; } else { return false; }except that the action is performed atomically. In contrast to the corresponding ConcurrentMap operation, does not return the previous value.- Specified by:
putIfAbsentin interfaceCache<K,V> - Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- Returns:
- true if a value was set.
- See Also:
-
remove
Description copied from interface:CacheRemoves the mapping for a key from this cache if it is present. More formally, if this cache contains a mapping from key k to value v such that(key==null ? k==null : key.equals(k)), that mapping is removed. (The cache can contain at most one such mapping.)Returns true if this cache previously associated the key, or false if the cache contained no mapping for the key.
The cache will not contain a mapping for the specified key once the call returns.
-
removeLocal
This method is added to only remove the cache locally. This is required sinceremove(Object)method notifies the other nodes in a cluster in addition to removing the local cache. -
remove
Description copied from interface:CacheAtomically removes the mapping for a key only if currently mapped to the given value. This is equivalent toif (cache.containsKey(key) && cache.get(key).equals(oldValue)) { cache.remove(key); return true; } else { return false; }except that the action is performed atomically. -
getAndRemove
Description copied from interface:CacheAtomically removes the entry for a key only if currently mapped to a given value. This is equivalent toif (cache.containsKey(key)) { V oldValue = cache.get(key); cache.remove(key); return oldValue; } else { return null; }except that the action is performed atomically.- Specified by:
getAndRemovein interfaceCache<K,V> - Parameters:
key- key with which the specified value is associated- Returns:
- the value if one existed or null if no mapping existed for this key
- See Also:
-
replace
Description copied from interface:CacheAtomically replaces the entry for a key only if currently mapped to a given value. This is equivalent toif (cache.containsKey(key) && cache.get(key).equals(oldValue)) { cache.put(key, newValue); return true; } else { return false; }except that the action is performed atomically. -
replace
Description copied from interface:CacheAtomically replaces the entry for a key only if currently mapped to some value. This is equivalent toif (cache.containsKey(key)) { cache.put(key, value); return true; } else { return false; }except that the action is performed atomically. In contrast to the corresponding ConcurrentMap operation, does not return the previous value. -
getAndReplace
Description copied from interface:CacheAtomically replaces the entry for a key only if currently mapped to some value. This is equivalent toif (cache.containsKey(key)) { V value = cache.get(key, value); cache.put(key, value); return value; } else { return null; }except that the action is performed atomically.- Specified by:
getAndReplacein interfaceCache<K,V> - Parameters:
key- key with which the specified value is associatedvalue- value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or null if there was no mapping for the key.
- See Also:
-
removeAll
Description copied from interface:CacheRemoves entries for the specified keys. The order in which the individual removes will occur is undefined. -
removeAll
public void removeAll()Description copied from interface:CacheRemoves all of the mappings from this cache. The order in which the individual removes will occur is undefined. This is potentially an expensive operation as listeners are invoked. Use #clear() to avoid this. -
removeAllLocal
public void removeAllLocal()This method is added to only remove the cache locally. This is required sinceremoveAll()method notifies the other nodes in a cluster in addition to removing the local cache. -
getConfiguration
Description copied from interface:CacheReturns a CacheConfiguration. When status isStatus.STARTEDan implementation must respect the following:- Statistics must be mutable when status is
Status.STARTED(CacheConfiguration.setStatisticsEnabled(boolean))
InvalidConfigurationExceptionmust be thrown on an attempt to mutate the configuration.- Specified by:
getConfigurationin interfaceCache<K,V> - Returns:
- the
CacheConfigurationof this cache
- Statistics must be mutable when status is
-
registerCacheEntryListener
public boolean registerCacheEntryListener(CacheEntryListener<? super K, ? super V> cacheEntryListener) Description copied from interface:CacheAdds a listener to the notification service.- Specified by:
registerCacheEntryListenerin interfaceCache<K,V> - Parameters:
cacheEntryListener- The listener to add. Listeners fire synchronously in the execution path, and after the causing event. if a listener throws an exception it will be wrapped in a CacheException and propagated back to the caller.- Returns:
- true if the listener is being added and was not already added
-
unregisterCacheEntryListener
Description copied from interface:CacheRemoves a call back listener.- Specified by:
unregisterCacheEntryListenerin interfaceCache<K,V> - Parameters:
cacheEntryListener- the listener to remove- Returns:
- true if the listener was present
-
invokeEntryProcessor
Description copied from interface:CachePasses the cache entry associated with K to be passed to the entry processor. All operations performed by the processor will be done atomically i.e. all The processor will perform the operations against- Specified by:
invokeEntryProcessorin interfaceCache<K,V> - Parameters:
key- the key to the entryentryProcessor- the processor which will process the entry- Returns:
- an object
- See Also:
-
getName
Description copied from interface:CacheReturn the name of the cache. -
getCacheManager
Description copied from interface:CacheGets the CacheManager managing this cache. A cache can be in only one CacheManager.- Specified by:
getCacheManagerin interfaceCache<K,V> - Returns:
- the manager
-
unwrap
Description copied from interface:CacheReturn an object of the specified type to allow access to the provider-specific API. If the provider's implementation does not support the specified class, theIllegalArgumentExceptionis thrown. -
keys
-
iterator
Description copied from interface:CacheThe ordering of the entries is undefined -
getMBean
Description copied from interface:CacheGet the MBean for this cache. -
start
public void start()Description copied from interface:CacheLifecycleNotifies providers to start themselves. This method is called during the resource's start method after it has changed its status to alive. Cache operations are legal in this method. At the completion of this method invocationCacheLifecycle.getStatus()must returnStatus.STARTED.- Specified by:
startin interfaceCacheLifecycle
-
stop
public void stop()Description copied from interface:CacheLifecycleProviders may be doing all sorts of exotic things and need to be able to clean up on stop. Cache operations are illegal after this method is called. AIllegalStateExceptionwill be Resources will change status toStatus.STOPPEDwhen this method completes. Stop must free any JVM resources used.- Specified by:
stopin interfaceCacheLifecycle
-
getStatus
Description copied from interface:CacheLifecycleReturns the cache status. This method blocks while the state is changing- Specified by:
getStatusin interfaceCacheLifecycle- Returns:
- one of
Status
-
expire
-
evict
-
setCacheConfiguration
-
setCapacity
public void setCapacity(long capacity) -
setEvictionAlgorithm
-
equals
-
hashCode
public int hashCode()
-