org.alfresco.repo.cache
Class TransactionalCache

java.lang.Object
  extended by org.alfresco.repo.cache.TransactionalCache
All Implemented Interfaces:
org.alfresco.repo.cache.SimpleCache, TransactionListener, org.springframework.beans.factory.InitializingBean

public class TransactionalCache
extends java.lang.Object
implements org.alfresco.repo.cache.SimpleCache, TransactionListener, org.springframework.beans.factory.InitializingBean

A 2-level cache that mainains both a transaction-local cache and wraps a non-transactional (shared) cache.

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.


Constructor Summary
TransactionalCache()
          Public constructor.
 
Method Summary
 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 key)
          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 key, java.lang.Object value)
          Goes direct to the shared cache in the absence of a transaction.
 void remove(java.io.Serializable key)
          Goes direct to the shared cache in the absence of a transaction.
 void setDisableSharedCache(boolean disableSharedCache)
          Set whether values must be written through to the shared cache or not
 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.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TransactionalCache

public TransactionalCache()
Public constructor.

Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
TransactionalCache.setName(String)

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

setSharedCache

public void setSharedCache(org.alfresco.repo.cache.SimpleCache 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

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 -

setName

public void setName(java.lang.String name)
Set the name that identifies this cache from other instances. This is optional.

Parameters:
name -

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Ensures that all properties have been set

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.Exception

contains

public boolean contains(java.io.Serializable key)
Checks the transactional removed and updated caches before checking the shared cache.

Specified by:
contains in interface org.alfresco.repo.cache.SimpleCache

getKeys

public 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.

Specified by:
getKeys in interface org.alfresco.repo.cache.SimpleCache

get

public java.lang.Object get(java.io.Serializable key)
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:
get in interface org.alfresco.repo.cache.SimpleCache

put

public void put(java.io.Serializable key,
                java.lang.Object 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.

Specified by:
put in interface org.alfresco.repo.cache.SimpleCache

remove

public void remove(java.io.Serializable key)
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:
remove in interface org.alfresco.repo.cache.SimpleCache

clear

public void clear()
Clears out all the caches.

Specified by:
clear in interface org.alfresco.repo.cache.SimpleCache

flush

public void flush()
NO-OP

Specified by:
flush in interface TransactionListener

beforeCompletion

public void beforeCompletion()
NO-OP

Specified by:
beforeCompletion in interface TransactionListener

beforeCommit

public void beforeCommit(boolean readOnly)
Merge the transactional caches into the shared cache

Specified by:
beforeCommit in interface TransactionListener
Parameters:
readOnly - true if the transaction is read-only

afterCommit

public void afterCommit()
Merge the transactional caches into the shared cache

Specified by:
afterCommit in interface 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:
afterRollback in interface TransactionListener


Copyright © 2005 - 2010 Alfresco Software, Inc. All Rights Reserved.