Package org.alfresco.util.cache
Class AbstractAsynchronouslyRefreshedCache<T>
- java.lang.Object
-
- org.alfresco.util.cache.AbstractAsynchronouslyRefreshedCache<T>
-
- All Implemented Interfaces:
java.util.concurrent.Callable<java.lang.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 java.lang.Object implements AsynchronouslyRefreshedCache<T>, RefreshableCacheListener, java.util.concurrent.Callable<java.lang.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 void
afterCommit()
Invoked after transaction commit.void
afterPropertiesSet()
void
afterRollback()
Invoked after transaction rollback.void
beforeCommit(boolean readOnly)
Called before a transaction is committed.void
beforeCompletion()
Invoked before transaction commit/rollback.void
broadcastEvent(RefreshableCacheEvent event)
protected abstract T
buildCache(java.lang.String key)
Build the cache entry for the specific key.java.lang.Void
call()
void
forceInChangesForThisUncommittedTransaction(java.lang.String key)
Use the current thread to build and put a new version of the cache entry before returning.T
get(java.lang.String key)
Get the cache.java.lang.String
getCacheId()
Get the cache idvoid
init()
boolean
isUpToDate(java.lang.String key)
Determine if the cache is up to datevoid
onRefreshableCacheEvent(RefreshableCacheEvent refreshableCacheEvent)
Callback made when a cache refresh occursvoid
refresh(java.lang.String key)
Refresh the cache asynchronously.void
register(RefreshableCacheListener listener)
Register to be informed when the cache is updated in the background.void
setBeanName(java.lang.String name)
void
setRegistry(AsynchronouslyRefreshedCacheRegistry registry)
void
setThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor threadPoolExecutor)
java.lang.String
toString()
protected void
waitForBuild(org.alfresco.util.cache.AbstractAsynchronouslyRefreshedCache.Refresh refresh)
-
-
-
Field Detail
-
liveLock
protected final java.util.concurrent.locks.ReentrantReadWriteLock liveLock
-
live
protected java.util.HashMap<java.lang.String,T> live
-
-
Method Detail
-
register
public void register(RefreshableCacheListener listener)
Description copied from interface:RefreshableCache
Register 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:
register
in interfaceRefreshableCache<T>
- Parameters:
listener
- RefreshableCacheListener
-
setThreadPoolExecutor
public void setThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor threadPoolExecutor)
- Parameters:
threadPoolExecutor
- the threadPoolExecutor to set
-
setRegistry
public void setRegistry(AsynchronouslyRefreshedCacheRegistry registry)
- Parameters:
registry
- the registry to set
-
init
public void init()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
get
public T get(java.lang.String key)
Description copied from interface:RefreshableCache
Get 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:
get
in interfaceRefreshableCache<T>
- Returns:
- T
-
forceInChangesForThisUncommittedTransaction
public void forceInChangesForThisUncommittedTransaction(java.lang.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(java.lang.String key)
Description copied from interface:RefreshableCache
Refresh the cache asynchronously.- Specified by:
refresh
in interfaceRefreshableCache<T>
-
onRefreshableCacheEvent
public void onRefreshableCacheEvent(RefreshableCacheEvent refreshableCacheEvent)
Description copied from interface:RefreshableCacheListener
Callback made when a cache refresh occurs- Specified by:
onRefreshableCacheEvent
in interfaceRefreshableCacheListener
- Parameters:
refreshableCacheEvent
- the cache event
-
isUpToDate
public boolean isUpToDate(java.lang.String key)
Description copied from interface:AsynchronouslyRefreshedCache
Determine if the cache is up to date- Specified by:
isUpToDate
in interfaceAsynchronouslyRefreshedCache<T>
- Parameters:
key
- tennant id- Returns:
- true if the cache is not currently refreshing itself
-
call
public java.lang.Void call()
- Specified by:
call
in interfacejava.util.concurrent.Callable<T>
-
setBeanName
public void setBeanName(java.lang.String name)
- Specified by:
setBeanName
in interfaceorg.springframework.beans.factory.BeanNameAware
-
getCacheId
public java.lang.String getCacheId()
Description copied from interface:AsynchronouslyRefreshedCache
Get the cache id- Specified by:
getCacheId
in interfaceAsynchronouslyRefreshedCache<T>
- Specified by:
getCacheId
in interfaceRefreshableCacheListener
- Returns:
- the cache ID
-
buildCache
protected abstract T buildCache(java.lang.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 java.lang.Exception
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
java.lang.Exception
-
broadcastEvent
public void broadcastEvent(RefreshableCacheEvent event)
-
beforeCommit
public void beforeCommit(boolean readOnly)
Description copied from interface:TransactionListener
Called before a transaction is committed.All transaction resources are still available.
- Specified by:
beforeCommit
in interfaceTransactionListener
- Parameters:
readOnly
- true if the transaction is read-only
-
beforeCompletion
public void beforeCompletion()
Description copied from interface:TransactionListener
Invoked before transaction commit/rollback. Will be called afterTransactionListener.beforeCommit(boolean)
even ifTransactionListener.beforeCommit(boolean)
failed.All transaction resources are still available.
- Specified by:
beforeCompletion
in interfaceTransactionListener
-
afterCommit
public void afterCommit()
Description copied from interface:TransactionListener
Invoked 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:
afterCommit
in interfaceTransactionListener
-
afterRollback
public void afterRollback()
Description copied from interface:TransactionListener
Invoked 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:
afterRollback
in interfaceTransactionListener
-
-