Class TransactionalCache<K extends java.io.Serializable,V>
- java.lang.Object
-
- org.alfresco.repo.cache.TransactionalCache<K,V>
-
- All Implemented Interfaces:
org.alfresco.repo.cache.LockingCache<K,V>,org.alfresco.repo.cache.SimpleCache<K,V>,TransactionListener,org.alfresco.util.transaction.TransactionListener,org.springframework.beans.factory.InitializingBean
public class TransactionalCache<K extends java.io.Serializable,V> extends java.lang.Object implements org.alfresco.repo.cache.LockingCache<K,V>, TransactionListener, org.springframework.beans.factory.InitializingBean
A 2-level cache that maintains both a transaction-local cache and wraps a non-transactional (shared) cache.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 Classes Modifier and Type Class Description static classTransactionalCache.CacheRegionKeystatic classTransactionalCache.ValueHolder<V2>A wrapper object to carry object values, but forcing a straight equality check based on a random integer only.
-
Constructor Summary
Constructors Constructor Description TransactionalCache()Public constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterCommit()Merge the transactional caches into the shared cachevoidafterPropertiesSet()Ensures that all properties have been setvoidafterRollback()Transfers cache removals or clears.voidbeforeCommit(boolean readOnly)Merge the transactional caches into the shared cachevoidbeforeCompletion()NO-OPvoidclear()Clears out all the caches.booleancontains(K key)Checks the transactional removed and updated caches before checking the shared cache.booleanequals(java.lang.Object obj)voidflush()NO-OPVget(K keyIn)Checks the per-transaction caches for the object before going to the shared cache.booleangetDisableSharedCacheReadForTransaction()java.util.Collection<K>getKeys()The keys returned are a union of the set of keys in the current transaction and those in the backing cache.static <KEY extends java.io.Serializable,VAL>
VALgetSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key)static <KEY extends java.io.Serializable,VAL>
VALgetSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key, TransactionStats stats)Fetches a value from the shared cache.inthashCode()booleanisValueLocked(K keyIn)voidlockValue(K keyIn)voidput(K keyIn, V value)Goes direct to the shared cache in the absence of a transaction.static <KEY extends java.io.Serializable,VAL>
voidputSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key, VAL value, TransactionStats stats)Values written to the backing cache need proper wrapping and unwrappingvoidremove(K keyIn)Goes 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)voidsetName(java.lang.String name)Set the name that identifies this cache from other instances.voidsetSharedCache(org.alfresco.repo.cache.SimpleCache<java.io.Serializable,TransactionalCache.ValueHolder<V>> sharedCache)Set the shared cache to use during transaction synchronization or when no transaction is present.voidsetTenantAware(boolean isTenantAware)java.lang.StringtoString()voidunlockValue(K keyIn)
-
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- See Also:
setName(String)
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
setSharedCache
public void setSharedCache(org.alfresco.repo.cache.SimpleCache<java.io.Serializable,TransactionalCache.ValueHolder<V>> sharedCache)
Set the shared cache to use during transaction synchronization or when no transaction is present.- Parameters:
sharedCache- underlying cache shared by transactions
-
setDisableSharedCache
public void setDisableSharedCache(boolean disableSharedCache)
Set whether values must be written through to the shared cache or not- Parameters:
disableSharedCache- true to prevent values from being written to the shared cache
-
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
public void setName(java.lang.String name)
Set the name that identifies this cache from other instances.
-
setTenantAware
public void setTenantAware(boolean isTenantAware)
-
setCacheStats
public void setCacheStats(CacheStatistics cacheStats)
-
setCacheStatsEnabled
public void setCacheStatsEnabled(boolean cacheStatsEnabled)
-
afterPropertiesSet
public void afterPropertiesSet() throws java.lang.ExceptionEnsures that all properties have been set- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
java.lang.Exception
-
getDisableSharedCacheReadForTransaction
public boolean getDisableSharedCacheReadForTransaction()
-
setDisableSharedCacheReadForTransaction
public void setDisableSharedCacheReadForTransaction(boolean noSharedCacheRead)
Transaction-long setting to force all the share cache to be bypassed for the current transaction. This setting is like having anullshared 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.- Parameters:
noSharedCacheRead- true to avoid reading from the shared cache for the transaction
-
contains
public boolean contains(K key)
Checks the transactional removed and updated caches before checking the shared cache.
-
getKeys
public java.util.Collection<K> getKeys()
The keys returned are a union of the set of keys in the current transaction and those in the backing cache.
-
getSharedCacheValue
public static <KEY extends java.io.Serializable,VAL> VAL getSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key)
-
getSharedCacheValue
public static <KEY extends java.io.Serializable,VAL> VAL getSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key, TransactionStats stats)
Fetches a value from the shared cache. If values were wrapped, then they will be unwrapped before being returned. If code requires direct access to the wrapper object as well, then this call should not be used.If a TransactionStats instance is passed in, then cache access stats are tracked, otherwise - if null is passed in then stats are not tracked.
- Parameters:
key- the key- Returns:
- Returns the value or null
-
putSharedCacheValue
public static <KEY extends java.io.Serializable,VAL> void putSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key, VAL value, TransactionStats stats)
Values written to the backing cache need proper wrapping and unwrapping- Parameters:
sharedCache- the cache to operate onkey- the keyvalue- the value to wrap- Since:
- 4.2.3
-
isValueLocked
public boolean isValueLocked(K keyIn)
-
lockValue
public void lockValue(K keyIn)
-
unlockValue
public void unlockValue(K keyIn)
-
get
public V get(K keyIn)
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.
-
put
public void put(K keyIn, V value)
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.
-
remove
public void remove(K keyIn)
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.
-
clear
public void clear()
Clears out all the caches.
-
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
-
-