public class TransactionalCache<K extends Serializable,V> extends Object implements org.alfresco.repo.cache.LockingCache<K,V>, 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.
| Modifier and Type | Class and Description |
|---|---|
static class |
TransactionalCache.CacheRegionKey |
static class |
TransactionalCache.ValueHolder<V2>
A wrapper object to carry object values, but forcing a straight equality check
based on a random integer only.
|
| 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(K key)
Checks the transactional removed and updated caches before checking the shared cache.
|
boolean |
equals(Object obj) |
void |
flush()
NO-OP
|
V |
get(K keyIn)
Checks the per-transaction caches for the object before going to the shared cache.
|
boolean |
getDisableSharedCacheReadForTransaction() |
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 Serializable,VAL> |
getSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache,
KEY key) |
static <KEY extends Serializable,VAL> |
getSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache,
KEY key,
TransactionStats stats)
Fetches a value from the shared cache.
|
int |
hashCode() |
boolean |
isValueLocked(K keyIn) |
void |
lockValue(K keyIn) |
void |
put(K keyIn,
V value)
Goes direct to the shared cache in the absence of a transaction.
|
static <KEY extends Serializable,VAL> |
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
|
void |
remove(K 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 |
setCacheStats(CacheStatistics cacheStats) |
void |
setCacheStatsEnabled(boolean cacheStatsEnabled) |
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(String name)
Set the name that identifies this cache from other instances.
|
void |
setSharedCache(org.alfresco.repo.cache.SimpleCache<Serializable,TransactionalCache.ValueHolder<V>> sharedCache)
Set the shared cache to use during transaction synchronization or when no transaction
is present.
|
void |
setTenantAware(boolean isTenantAware) |
String |
toString() |
void |
unlockValue(K keyIn) |
public String toString()
toString in class ObjectsetName(String)public void setSharedCache(org.alfresco.repo.cache.SimpleCache<Serializable,TransactionalCache.ValueHolder<V>> 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(String name)
public void setTenantAware(boolean isTenantAware)
public void setCacheStats(CacheStatistics cacheStats)
public void setCacheStatsEnabled(boolean cacheStatsEnabled)
public void afterPropertiesSet()
throws Exception
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBeanExceptionpublic boolean getDisableSharedCacheReadForTransaction()
public 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(K key)
contains in interface org.alfresco.repo.cache.SimpleCache<K extends Serializable,V>public Collection<K> getKeys()
getKeys in interface org.alfresco.repo.cache.SimpleCache<K extends Serializable,V>public static <KEY extends Serializable,VAL> VAL getSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key)
public static <KEY extends Serializable,VAL> VAL getSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key, TransactionStats stats)
If a TransactionStats instance is passed in, then cache access stats are tracked, otherwise - if null is passed in then stats are not tracked.
key - the keypublic static <KEY extends Serializable,VAL> void putSharedCacheValue(org.alfresco.repo.cache.SimpleCache<KEY,TransactionalCache.ValueHolder<VAL>> sharedCache, KEY key, VAL value, TransactionStats stats)
sharedCache - the cache to operate onkey - the keyvalue - the value to wrappublic boolean isValueLocked(K keyIn)
isValueLocked in interface org.alfresco.repo.cache.LockingCache<K extends Serializable,V>public void lockValue(K keyIn)
lockValue in interface org.alfresco.repo.cache.LockingCache<K extends Serializable,V>public void unlockValue(K keyIn)
unlockValue in interface org.alfresco.repo.cache.LockingCache<K extends Serializable,V>public V get(K keyIn)
get in interface org.alfresco.repo.cache.SimpleCache<K extends Serializable,V>public void put(K keyIn, V 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.SimpleCache<K extends Serializable,V>public void remove(K 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.SimpleCache<K extends Serializable,V>public void clear()
clear in interface org.alfresco.repo.cache.SimpleCache<K extends Serializable,V>public void flush()
public void beforeCompletion()
beforeCompletion in interface org.alfresco.util.transaction.TransactionListenerpublic void beforeCommit(boolean readOnly)
beforeCommit in interface org.alfresco.util.transaction.TransactionListenerreadOnly - true if the transaction is read-onlypublic void afterCommit()
afterCommit in interface org.alfresco.util.transaction.TransactionListenerpublic void afterRollback()
afterRollback in interface org.alfresco.util.transaction.TransactionListenerCopyright © 2005–2018 Alfresco Software. All rights reserved.