org.axonframework.cache
Class WeakReferenceCache

java.lang.Object
  extended by org.axonframework.cache.WeakReferenceCache
All Implemented Interfaces:
Cache

public class WeakReferenceCache
extends Object
implements Cache

Cache implementation that keeps values in the cache until the garbage collector has removed them. Unlike the WeakHashMap, which uses weak references on the keys, this Cache uses weak references on the values.

Values are Weakly referenced, which means they are not eligible for removal as long as any other references to the value exist.

Items expire once the garbage collector has removed them. Some time after they have been removed, the entry listeners are being notified thereof. Note that notification are emitted when the cache is being accessed (either for reading or writing). If the cache is not being accessed for a longer period of time, it may occur that listeners are not notified.

Since:
2.2.1
Author:
Allard Buijze

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.axonframework.cache.Cache
Cache.EntryListener, Cache.EntryListenerAdapter
 
Constructor Summary
WeakReferenceCache()
           
 
Method Summary
<K> boolean
containsKey(K key)
          Indicates whether there is an item stored under given key.
<K,V> V
get(K key)
          Returns an item from the cache, or null if no item was stored under that key
<K,V> void
put(K key, V value)
          Stores the given value in the cache, under given key.
<K,V> boolean
putIfAbsent(K key, V value)
          Stores the given value in the cache, under given key, if no element is yet available under that key.
 void registerCacheEntryListener(Cache.EntryListener entryListener)
          Registers the given cacheEntryListener to listen for Cache changes.
<K> boolean
remove(K key)
          Removes the entry stored under given key.
 void unregisterCacheEntryListener(Cache.EntryListener entryListener)
          Unregisters the previously registered cacheEntryListener.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WeakReferenceCache

public WeakReferenceCache()
Method Detail

registerCacheEntryListener

public void registerCacheEntryListener(Cache.EntryListener entryListener)
Description copied from interface: Cache
Registers the given cacheEntryListener to listen for Cache changes.

Specified by:
registerCacheEntryListener in interface Cache
Parameters:
entryListener - The listener to register

unregisterCacheEntryListener

public void unregisterCacheEntryListener(Cache.EntryListener entryListener)
Description copied from interface: Cache
Unregisters the previously registered cacheEntryListener.

Specified by:
unregisterCacheEntryListener in interface Cache
Parameters:
entryListener - The listener to unregister

get

public <K,V> V get(K key)
Description copied from interface: Cache
Returns an item from the cache, or null if no item was stored under that key

Specified by:
get in interface Cache
Type Parameters:
K - The type of key used
V - The type of value stored
Parameters:
key - The key under which the item was cached
Returns:
the item stored under the given key

put

public <K,V> void put(K key,
                      V value)
Description copied from interface: Cache
Stores the given value in the cache, under given key. If an item already exists, it is updated with the new value.

Specified by:
put in interface Cache
Type Parameters:
K - The type of key used
V - The type of value stored
Parameters:
key - The key under which to store the item
value - The item to cache

putIfAbsent

public <K,V> boolean putIfAbsent(K key,
                                 V value)
Description copied from interface: Cache
Stores the given value in the cache, under given key, if no element is yet available under that key. This operation is performed atomically.

Specified by:
putIfAbsent in interface Cache
Type Parameters:
K - The type of key used
V - The type of value stored
Parameters:
key - The key under which to store the item
value - The item to cache
Returns:
true if no value was previously assigned to the key, false otherwise.

remove

public <K> boolean remove(K key)
Description copied from interface: Cache
Removes the entry stored under given key. If no such entry exists, nothing happens.

Specified by:
remove in interface Cache
Type Parameters:
K - The type of key used
Parameters:
key - The key under which the item was stored
Returns:
true if a value was previously assigned to the key and has been removed, false otherwise.

containsKey

public <K> boolean containsKey(K key)
Description copied from interface: Cache
Indicates whether there is an item stored under given key.

Specified by:
containsKey in interface Cache
Type Parameters:
K - The type of key
Parameters:
key - The key to check
Returns:
true if an item is available under that key, false otherwise.


Copyright © 2010-2016. All Rights Reserved.