Package org.alfresco.util.cache
Class AbstractAsynchronouslyRefreshedCache<T>
- java.lang.Object
-
- org.alfresco.util.cache.AbstractAsynchronouslyRefreshedCache<T>
-
- All Implemented Interfaces:
Callable<Void>,AsynchronouslyRefreshedCache<T>,RefreshableCache<T>,RefreshableCacheListener,TransactionListener,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.InitializingBean
public abstract class AbstractAsynchronouslyRefreshedCache<T> extends Object implements AsynchronouslyRefreshedCache<T>, RefreshableCacheListener, Callable<Void>, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, TransactionListener
The base implementation for an asynchronously refreshed cache. Currently supports one value or a cache per key (such as tenant.) Implementors just need to provide buildCache(String key/tennnantId)- Since:
- 4.1.3
- Author:
- Andy, mrogers MER 17/04/2014 Refactored to core and generalised tennancy
-
-
Constructor Summary
Constructors Constructor Description AbstractAsynchronouslyRefreshedCache()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidafterCommit()Invoked after transaction commit.voidafterPropertiesSet()voidafterRollback()Invoked after transaction rollback.voidbeforeCommit(boolean readOnly)Called before a transaction is committed.voidbeforeCompletion()Invoked before transaction commit/rollback.voidbroadcastEvent(RefreshableCacheEvent event)protected abstract TbuildCache(String key)Build the cache entry for the specific key.Voidcall()voidforceInChangesForThisUncommittedTransaction(String key)Use the current thread to build and put a new version of the cache entry before returning.Tget(String key)Get the cache.StringgetCacheId()Get the cache idvoidinit()booleanisUpToDate(String key)Determine if the cache is up to datevoidonRefreshableCacheEvent(RefreshableCacheEvent refreshableCacheEvent)Callback made when a cache refresh occursvoidrefresh(String key)Refresh the cache asynchronously.voidregister(RefreshableCacheListener listener)Register to be informed when the cache is updated in the background.voidsetBeanName(String name)voidsetRegistry(AsynchronouslyRefreshedCacheRegistry registry)voidsetThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor)StringtoString()protected voidwaitForBuild(org.alfresco.util.cache.AbstractAsynchronouslyRefreshedCache.Refresh refresh)
-
-
-
Field Detail
-
liveLock
protected final ReentrantReadWriteLock liveLock
-
-
Method Detail
-
register
public void register(RefreshableCacheListener listener)
Description copied from interface:RefreshableCacheRegister to be informed when the cache is updated in the background. Note: it is up to the implementation to provide any transactional wrapping. Transactional wrapping is not required to invalidate a shared cache entry directly via a transactional cache- Specified by:
registerin interfaceRefreshableCache<T>- Parameters:
listener- RefreshableCacheListener
-
setThreadPoolExecutor
public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor)
- Parameters:
threadPoolExecutor- the threadPoolExecutor to set
-
setRegistry
public void setRegistry(AsynchronouslyRefreshedCacheRegistry registry)
- Parameters:
registry- the registry to set
-
init
public void init()
-
get
public T get(String key)
Description copied from interface:RefreshableCacheGet the cache. If there is no cache value this call will block. If the underlying cache is being refreshed, the old cache value will be returned until the refresh is complete.- Specified by:
getin interfaceRefreshableCache<T>- Returns:
- T
-
forceInChangesForThisUncommittedTransaction
public void forceInChangesForThisUncommittedTransaction(String key)
Use the current thread to build and put a new version of the cache entry before returning.- Parameters:
key- the cache key
-
waitForBuild
protected void waitForBuild(org.alfresco.util.cache.AbstractAsynchronouslyRefreshedCache.Refresh refresh)
-
refresh
public void refresh(String key)
Description copied from interface:RefreshableCacheRefresh the cache asynchronously.- Specified by:
refreshin interfaceRefreshableCache<T>
-
onRefreshableCacheEvent
public void onRefreshableCacheEvent(RefreshableCacheEvent refreshableCacheEvent)
Description copied from interface:RefreshableCacheListenerCallback made when a cache refresh occurs- Specified by:
onRefreshableCacheEventin interfaceRefreshableCacheListener- Parameters:
refreshableCacheEvent- the cache event
-
isUpToDate
public boolean isUpToDate(String key)
Description copied from interface:AsynchronouslyRefreshedCacheDetermine if the cache is up to date- Specified by:
isUpToDatein interfaceAsynchronouslyRefreshedCache<T>- Parameters:
key- tennant id- Returns:
- true if the cache is not currently refreshing itself
-
setBeanName
public void setBeanName(String name)
- Specified by:
setBeanNamein interfaceorg.springframework.beans.factory.BeanNameAware
-
getCacheId
public String getCacheId()
Description copied from interface:AsynchronouslyRefreshedCacheGet the cache id- Specified by:
getCacheIdin interfaceAsynchronouslyRefreshedCache<T>- Specified by:
getCacheIdin interfaceRefreshableCacheListener- Returns:
- the cache ID
-
buildCache
protected abstract T buildCache(String key)
Build the cache entry for the specific key. This method is called in a thread-safe manner i.e. it is only ever called by a single thread.- Parameters:
key-- Returns:
- new Cache instance
-
afterPropertiesSet
public void afterPropertiesSet() throws Exception- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception
-
broadcastEvent
public void broadcastEvent(RefreshableCacheEvent event)
-
beforeCommit
public void beforeCommit(boolean readOnly)
Description copied from interface:TransactionListenerCalled before a transaction is committed.All transaction resources are still available.
- Specified by:
beforeCommitin interfaceTransactionListener- Parameters:
readOnly- true if the transaction is read-only
-
beforeCompletion
public void beforeCompletion()
Description copied from interface:TransactionListenerInvoked before transaction commit/rollback. Will be called afterTransactionListener.beforeCommit(boolean)even ifTransactionListener.beforeCommit(boolean)failed.All transaction resources are still available.
- Specified by:
beforeCompletionin interfaceTransactionListener
-
afterCommit
public void afterCommit()
Description copied from interface:TransactionListenerInvoked after transaction commit.Any exceptions generated here will only be logged and will have no effect on the state of the transaction.
Although all transaction resources are still available, this method should be used only for cleaning up resources after a commit has occured.
- Specified by:
afterCommitin interfaceTransactionListener
-
afterRollback
public void afterRollback()
Description copied from interface:TransactionListenerInvoked after transaction rollback.Any exceptions generated here will only be logged and will have no effect on the state of the transaction.
Although all transaction resources are still available, this method should be used only for cleaning up resources after a rollback has occured.
- Specified by:
afterRollbackin interfaceTransactionListener
-
-