org.axonframework.saga.repository
Class SagaCache

java.lang.Object
  extended by org.axonframework.saga.repository.SagaCache

public class SagaCache
extends Object

Wrapper around a map of String to Saga that keeps Weak References to the saga instances. As long as any references to a Saga exist, this SagaCache will return that instance when it is looked up using its identifier.

When all references to a Saga are cleared, the garbage collector may clear any unreferenced saga instances from this Cache.

When a reference from this Cache is removed, the entry itself may still survive in the cache (albeit empty). To remove any empty entries, use the purge() method. Empty entries are also cleared when accessed (cache misses).

Note that the primary purpose of this cache is not to improve performance, but to prevent multiple instances of the same conceptual saga (i.e. having the same identifier) from being active in the JVM.

Since:
0.7
Author:
Allard Buijze

Constructor Summary
SagaCache()
          Initializes an empty cache.
 
Method Summary
 Saga get(String sagaIdentifier)
          Retrieves the Saga instance with the given sagaIdentifier, or null if none was found.
 boolean isEmpty()
          Indicates whether or not this cache is empty.
 void purge()
          Clears any entries whose saga instances have been cleaned up by the garbage collector.
 Saga put(Saga saga)
          Puts the given saga in this cache, if no saga with the same identifier already exists.
 int size()
          Returns an approximation of the number of items in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SagaCache

public SagaCache()
Initializes an empty cache.

Method Detail

get

public Saga get(String sagaIdentifier)
Retrieves the Saga instance with the given sagaIdentifier, or null if none was found.

Parameters:
sagaIdentifier - The identifier of the saga to return
Returns:
the Saga with given identifier, or null if none was found.

put

public Saga put(Saga saga)
Puts the given saga in this cache, if no saga with the same identifier already exists. The return value provides a reference to the cached saga instance. This may either be the same as saga (in case the given saga was successfully stored in the cache), or another instance.

Callers of put should always use the returned reference for further processing and regard the given saga as an unwanted duplicate.

Parameters:
saga - The saga instance to store in the cache
Returns:
The cached instance of the saga

purge

public void purge()
Clears any entries whose saga instances have been cleaned up by the garbage collector. Purged entries will no longer count against the size() of the cache.


size

public int size()
Returns an approximation of the number of items in the cache. The returned count includes empty entries (i.e. entries pointing to sagas that have been garbage collected)

Returns:
the number of entries in the cache
See Also:
purge()

isEmpty

public boolean isEmpty()
Indicates whether or not this cache is empty. The cache is considered not empty even if it only contains entries to sagas that have been garbage collected.

Returns:
true if the cache is completely empty, false otherwise.
See Also:
purge()


Copyright © 2011. All Rights Reserved.