public class TransactionalCache extends java.lang.Object implements org.alfresco.repo.cache.SimpleCache, TransactionListener, org.springframework.beans.factory.InitializingBean
It uses the Ehcache Cache for it's per-transaction caches as these provide automatic size limitations, etc.
Instances of this class do not require a transaction. They will work directly with the shared cache when no transaction is present. There is virtually no overhead when running out-of-transaction.
The first phase of the commit ensures that any values written to the cache in the current transaction are not already superceded by values in the shared cache. In this case, the transaction is failed for concurrency reasons and will have to retry. The second phase occurs post-commit. We are sure that the transaction committed correctly, but things may have changed in the cache between the commit and post-commit. If this is the case, then the offending values are merely removed from the shared cache.
When the cache is cleared, a flag is set on the transaction.
The shared cache, instead of being cleared itself, is just ignored for the remainder
of the tranasaction. At the end of the transaction, if the flag is set, the
shared transaction is cleared before updates are added back to it.
Because there is a limited amount of space available to the in-transaction caches, when either of these becomes full, the cleared flag is set. This ensures that the shared cache will not have stale data in the event of the transaction-local caches dropping items. It is therefore important to size the transactional caches correctly.
| Modifier and Type | Class and Description |
|---|---|
static class |
TransactionalCache.CacheRegionKey |
| Constructor and Description |
|---|
TransactionalCache()
Public constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
afterCommit()
Merge the transactional caches into the shared cache
|
void |
afterPropertiesSet()
Ensures that all properties have been set
|
void |
afterRollback()
Transfers cache removals or clears.
|
void |
beforeCommit(boolean readOnly)
Merge the transactional caches into the shared cache
|
void |
beforeCompletion()
NO-OP
|
void |
clear()
Clears out all the caches.
|
boolean |
contains(java.io.Serializable key)
Checks the transactional removed and updated caches before checking the shared cache.
|
boolean |
equals(java.lang.Object obj) |
void |
flush()
NO-OP
|
java.lang.Object |
get(java.io.Serializable keyIn)
Checks the per-transaction caches for the object before going to the shared cache.
|
java.util.Collection |
getKeys()
The keys returned are a union of the set of keys in the current transaction and
those in the backing cache.
|
int |
hashCode() |
void |
put(java.io.Serializable keyIn,
java.lang.Object value)
Goes direct to the shared cache in the absence of a transaction.
|
void |
remove(java.io.Serializable keyIn)
Goes direct to the shared cache in the absence of a transaction.
|
void |
setAllowEqualsChecks(boolean allowEqualsChecks)
Allow equality checking of values before they are written to the shared cache on
commit.
|
void |
setDisableSharedCache(boolean disableSharedCache)
Set whether values must be written through to the shared cache or not
|
void |
setDisableSharedCacheReadForTransaction(boolean noSharedCacheRead)
Transaction-long setting to force all the share cache to be bypassed for the current transaction.
|
void |
setMaxCacheSize(int maxCacheSize)
Set the maximum number of elements to store in the update and remove caches.
|
void |
setMutable(boolean isMutable) |
void |
setName(java.lang.String name)
Set the name that identifies this cache from other instances.
|
void |
setSharedCache(org.alfresco.repo.cache.SimpleCache sharedCache)
Set the shared cache to use during transaction synchronization or when no transaction
is present.
|
void |
setTenantAware(boolean isTenantAware) |
java.lang.String |
toString() |
public java.lang.String toString()
toString in class java.lang.ObjectTransactionalCache.setName(String)public boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic void setSharedCache(org.alfresco.repo.cache.SimpleCache sharedCache)
sharedCache - underlying cache shared by transactionspublic void setDisableSharedCache(boolean disableSharedCache)
disableSharedCache - true to prevent values from being written to
the shared cachepublic void setMutable(boolean isMutable)
isMutable - true if the data stored in the cache is modifiablepublic void setAllowEqualsChecks(boolean allowEqualsChecks)
allowEqualsChecks - true if value comparisons can be made between values
stored in the transactional cache and those stored in the
shared cachepublic void setMaxCacheSize(int maxCacheSize)
The removed list will overflow to disk in order to ensure that deletions are not lost.
maxCacheSize - maximum number of items to be held in-transactionpublic void setName(java.lang.String name)
public void setTenantAware(boolean isTenantAware)
public void afterPropertiesSet()
throws java.lang.Exception
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBeanjava.lang.Exceptionpublic void setDisableSharedCacheReadForTransaction(boolean noSharedCacheRead)
null shared cache,
but only lasts for the transaction.
Use this when a read transaction must see consistent and current data i.e. go to the database.
While this is active, write operations will also not be committed to the shared cache.noSharedCacheRead - true to avoid reading from the shared cache for the transactionpublic boolean contains(java.io.Serializable key)
contains in interface org.alfresco.repo.cache.SimpleCachepublic java.util.Collection getKeys()
getKeys in interface org.alfresco.repo.cache.SimpleCachepublic java.lang.Object get(java.io.Serializable keyIn)
get in interface org.alfresco.repo.cache.SimpleCachepublic void put(java.io.Serializable keyIn,
java.lang.Object value)
Where a transaction is present, a cache of updated items is lazily added to the thread and the Object put onto that.
put in interface org.alfresco.repo.cache.SimpleCachepublic void remove(java.io.Serializable keyIn)
Where a transaction is present, a cache of removed items is lazily added to the thread and the Object put onto that.
remove in interface org.alfresco.repo.cache.SimpleCachepublic void clear()
clear in interface org.alfresco.repo.cache.SimpleCachepublic void flush()
flush in interface TransactionListenerpublic void beforeCompletion()
beforeCompletion in interface TransactionListenerpublic void beforeCommit(boolean readOnly)
beforeCommit in interface TransactionListenerreadOnly - true if the transaction is read-onlypublic void afterCommit()
afterCommit in interface TransactionListenerpublic void afterRollback()
afterRollback in interface TransactionListenerCopyright © 2005 - 2010 Alfresco Software, Inc. All Rights Reserved.