Class TransactionalCache<K extends Serializable,V>
- All Implemented Interfaces:
LockingCache<K,,V> SimpleCache<K,,V> TransactionListener,org.alfresco.util.transaction.TransactionListener,org.springframework.beans.factory.InitializingBean
It uses the shared SimpleCache for it's per-transaction caches as these can 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 superseded 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.
- Author:
- Derek Hulley
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic final classA wrapper object to carry object values, but forcing a straight equality check based on a random integer only. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidMerge the transactional caches into the shared cachevoidEnsures that all properties have been setvoidTransfers cache removals or clears.voidbeforeCommit(boolean readOnly) Merge the transactional caches into the shared cachevoidNO-OPvoidclear()Clears out all the caches.booleanChecks the transactional removed and updated caches before checking the shared cache.booleanvoidflush()NO-OPChecks the per-transaction caches for the object before going to the shared cache.booleangetKeys()The keys returned are a union of the set of keys in the current transaction and those in the backing cache.static <KEY extends Serializable,VAL>
VALgetSharedCacheValue(SimpleCache<KEY, TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key) static <KEY extends Serializable,VAL>
VALgetSharedCacheValue(SimpleCache<KEY, TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key, TransactionStats stats) Fetches a value from the shared cache.inthashCode()booleanisValueLocked(K keyIn) voidvoidGoes direct to the shared cache in the absence of a transaction.static <KEY extends Serializable,VAL>
voidputSharedCacheValue(SimpleCache<KEY, TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key, VAL value, TransactionStats stats) Values written to the backing cache need proper wrapping and unwrappingvoidGoes direct to the shared cache in the absence of a transaction.voidsetAllowEqualsChecks(boolean allowEqualsChecks) Allow equality checking of values before they are written to the shared cache on commit.voidsetCacheStats(CacheStatistics cacheStats) voidsetCacheStatsEnabled(boolean cacheStatsEnabled) voidsetDisableSharedCache(boolean disableSharedCache) Set whether values must be written through to the shared cache or notvoidsetDisableSharedCacheReadForTransaction(boolean noSharedCacheRead) Transaction-long setting to force all the share cache to be bypassed for the current transaction.voidsetMaxCacheSize(int maxCacheSize) Set the maximum number of elements to store in the update and remove caches.voidsetMutable(boolean isMutable) voidSet the name that identifies this cache from other instances.voidsetSharedCache(SimpleCache<Serializable, TransactionalCache.ValueHolder<V>> sharedCache) Set the shared cache to use during transaction synchronization or when no transaction is present.voidsetTenantAware(boolean isTenantAware) toString()voidunlockValue(K keyIn) Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.alfresco.util.transaction.TransactionListener
getCustomOrder
-
Constructor Details
-
TransactionalCache
public TransactionalCache()Public constructor.
-
-
Method Details
-
toString
-
equals
-
hashCode
public int hashCode() -
setMutable
public void setMutable(boolean isMutable) - Parameters:
isMutable- true if the data stored in the cache is modifiable
-
setAllowEqualsChecks
public void setAllowEqualsChecks(boolean allowEqualsChecks) Allow equality checking of values before they are written to the shared cache on commit. This allows some caches to bypass unnecessary cache updates when the values remain unchanged. Typically, this setting should be applied only to mutable caches and only where the values being stored have a fast and reliable equality check.- Parameters:
allowEqualsChecks- true if value comparisons can be made between values stored in the transactional cache and those stored in the shared cache
-
setMaxCacheSize
public void setMaxCacheSize(int maxCacheSize) Set the maximum number of elements to store in the update and remove caches. The maximum number of elements stored in the transaction will be twice the value given.The removed list will overflow to disk in order to ensure that deletions are not lost.
- Parameters:
maxCacheSize- maximum number of items to be held in-transaction
-
setName
Set the name that identifies this cache from other instances. -
setTenantAware
public void setTenantAware(boolean isTenantAware) -
setCacheStats
-
setCacheStatsEnabled
public void setCacheStatsEnabled(boolean cacheStatsEnabled) -
afterPropertiesSet
Ensures that all properties have been set- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception
-
contains
Checks the transactional removed and updated caches before checking the shared cache.- Specified by:
containsin interfaceSimpleCache<K extends Serializable,V>
-
getKeys
The keys returned are a union of the set of keys in the current transaction and those in the backing cache.- Specified by:
getKeysin interfaceSimpleCache<K extends Serializable,V>
-
isValueLocked
- Specified by:
isValueLockedin interfaceLockingCache<K extends Serializable,V>
-
lockValue
- Specified by:
lockValuein interfaceLockingCache<K extends Serializable,V>
-
unlockValue
- Specified by:
unlockValuein interfaceLockingCache<K extends Serializable,V>
-
get
Checks the per-transaction caches for the object before going to the shared cache. If the thread is not in a transaction, then the shared cache is accessed directly.- Specified by:
getin interfaceSimpleCache<K extends Serializable,V>
-
put
Goes direct to the shared cache in the absence of a transaction.Where a transaction is present, a cache of updated items is lazily added to the thread and the Object put onto that.
- Specified by:
putin interfaceSimpleCache<K extends Serializable,V>
-
remove
Goes direct to the shared cache in the absence of a transaction.Where a transaction is present, a cache of removed items is lazily added to the thread and the Object put onto that.
- Specified by:
removein interfaceSimpleCache<K extends Serializable,V>
-
clear
public void clear()Clears out all the caches.- Specified by:
clearin interfaceSimpleCache<K extends Serializable,V>
-
flush
public void flush()NO-OP- Specified by:
flushin interfaceTransactionListener
-
beforeCompletion
public void beforeCompletion()NO-OP- Specified by:
beforeCompletionin interfaceTransactionListener- Specified by:
beforeCompletionin interfaceorg.alfresco.util.transaction.TransactionListener
-
beforeCommit
public void beforeCommit(boolean readOnly) Merge the transactional caches into the shared cache- Specified by:
beforeCommitin interfaceTransactionListener- Specified by:
beforeCommitin interfaceorg.alfresco.util.transaction.TransactionListener- Parameters:
readOnly- true if the transaction is read-only
-
afterCommit
public void afterCommit()Merge the transactional caches into the shared cache- Specified by:
afterCommitin interfaceTransactionListener- Specified by:
afterCommitin interfaceorg.alfresco.util.transaction.TransactionListener
-
afterRollback
public void afterRollback()Transfers cache removals or clears. This allows explicit cache cleanup to be propagated to the shared cache even in the event of rollback - useful if the cause of a problem is the shared cache value.- Specified by:
afterRollbackin interfaceTransactionListener- Specified by:
afterRollbackin interfaceorg.alfresco.util.transaction.TransactionListener
-