Package org.craftercms.core.cache.impl
Class CacheImpl
- java.lang.Object
-
- org.craftercms.core.cache.impl.CacheImpl
-
- All Implemented Interfaces:
Cache
public class CacheImpl extends Object implements Cache
Cachethat implements common functionality, such as logging and expiration/refresh check every tick, and uses and underlyingCacheStoreAdapterto store the items.- Author:
- Sumer Jabri, Alfonso Vásquez
-
-
Field Summary
Fields Modifier and Type Field Description protected CacheRefreshercacheRefresherUsed to refresh a list of items.protected CacheStoreAdaptercacheStoreAdapterAdapter for the cache data structure.protected AtomicIntegerticksHolds the current number of ticks.
-
Constructor Summary
Constructors Constructor Description CacheImpl()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddScope(String scope, int maxItemsInMemory)Adds a new scope to the cache.protected booleancheckForExpiration(CacheItem item)Checks if the givenCacheItemhas expired.protected booleancheckForRefresh(CacheItem item, List<CacheItem> itemsToRefresh)Checks if a givenCacheItemneeds to be refreshed.voidclearAll()Clears the contents of the entire cache.voidclearScope(String scope)Clears the contents of the specified scope in the cache.protected voiddoChecks(CacheItem item, List<CacheItem> itemsToRefresh)Checks if the givenCacheItemhas expired or needs to be refreshed.CacheItemget(String scope, Object key)Retrieves an item from a scope.Collection<Object>getKeys(String scope)Returns a list of the keys of the items present in the specified scope.Collection<String>getScopes()Returns the list of scopes this cache manages.intgetSize(String scope)Returns the quantity of items present in the specified scope.CacheStatisticsgetStatistics(String scope)Returns the statistics of the specified scope in the cache.booleanhasKey(String scope, Object key)Returns true if there's and item with the specified key in the specified scope.booleanhasScope(String scope)Returns true if the specified scope exists in the cache.voidput(String scope, Object key, Object value)Puts an item in a scope.voidput(String scope, Object key, Object value, long expireAfter, long refreshFrequency, CacheLoader loader, Object... loaderParams)Puts and item in a scope.booleanremove(String scope, Object key)Removes an item from a scope.voidremoveScope(String scope)Removes an existing scope and all the items that are in it.voidsetCacheRefresher(CacheRefresher cacheRefresher)Sets theCacheRefresher, used to refresh the items of the cache.voidsetCacheStoreAdapter(CacheStoreAdapter cacheStoreAdapter)Sets the underlyingCacheStoreAdaptervoidtick()Called when a tick occurs.
-
-
-
Field Detail
-
ticks
protected AtomicInteger ticks
Holds the current number of ticks.
-
cacheStoreAdapter
protected CacheStoreAdapter cacheStoreAdapter
Adapter for the cache data structure.
-
cacheRefresher
protected CacheRefresher cacheRefresher
Used to refresh a list of items.
-
-
Constructor Detail
-
CacheImpl
public CacheImpl()
Default constructor. SetstimestampGeneratorto.IncrementalTimestampGenerator.
-
-
Method Detail
-
setCacheStoreAdapter
public void setCacheStoreAdapter(CacheStoreAdapter cacheStoreAdapter)
Sets the underlyingCacheStoreAdapter- Parameters:
cacheStoreAdapter- the adapter for the cache data structure
-
setCacheRefresher
public void setCacheRefresher(CacheRefresher cacheRefresher)
Sets theCacheRefresher, used to refresh the items of the cache.
-
hasScope
public boolean hasScope(String scope) throws InternalCacheEngineException
Returns true if the specified scope exists in the cache.- Specified by:
hasScopein interfaceCache- Parameters:
scope- the name of the scope- Returns:
- true if the specified scope exists in the cache
- Throws:
InternalCacheEngineException- if an error occurred in the underlying cache implementation
-
getScopes
public Collection<String> getScopes() throws InternalCacheEngineException
Returns the list of scopes this cache manages.- Specified by:
getScopesin interfaceCache- Returns:
- the list of available scopes in the cache
- Throws:
InternalCacheEngineException- if an error occurred in the underlying cache implementation
-
addScope
public void addScope(String scope, int maxItemsInMemory) throws InternalCacheEngineException
Adds a new scope to the cache.- Specified by:
addScopein interfaceCache- Parameters:
scope- the name of the scopemaxItemsInMemory- the maximum number of items in memory, before they are evicted- Throws:
InternalCacheEngineException- if an error occurred in the underlying cache implementation
-
removeScope
public void removeScope(String scope) throws InvalidScopeException, InternalCacheEngineException
Removes an existing scope and all the items that are in it.- Specified by:
removeScopein interfaceCache- Parameters:
scope- scope to remove- Throws:
InvalidScopeException- if the specified scope isn't a registered oneInternalCacheEngineException- if an error occurred in the underlying cache implementation
-
getSize
public int getSize(String scope) throws InvalidScopeException, InternalCacheEngineException
Returns the quantity of items present in the specified scope.- Specified by:
getSizein interfaceCache- Returns:
- the size of the scope (quantity of items)
- Throws:
InvalidScopeException- if the specified scope isn't a registered oneInternalCacheEngineException- if an error occurred in the underlying cache implementation
-
getKeys
public Collection<Object> getKeys(String scope) throws InvalidScopeException, InternalCacheEngineException
Returns a list of the keys of the items present in the specified scope.- Specified by:
getKeysin interfaceCache- Returns:
- a list of the keys of the available items in the scope
- Throws:
InvalidScopeException- if the specified scope isn't a registered oneInternalCacheEngineException- if an error occurred in the underlying cache implementation
-
hasKey
public boolean hasKey(String scope, Object key) throws InvalidScopeException, InternalCacheEngineException
Returns true if there's and item with the specified key in the specified scope.- Specified by:
hasKeyin interfaceCache- Returns:
- true if the key exists in the scope, false otherwise
- Throws:
InvalidScopeException- if the specified scope isn't a registered oneInternalCacheEngineException- if an error occurred in the underlying cache implementation
-
get
public CacheItem get(String scope, Object key) throws InvalidScopeException, InternalCacheEngineException
Retrieves an item from a scope.- Specified by:
getin interfaceCache- Parameters:
scope- scope to get the item fromkey- unique key for the item within this scope- Returns:
- the requested item if found, null otherwise
- Throws:
InvalidScopeException- if the specified scope isn't a registered oneInternalCacheEngineException- if an error occurred in the underlying cache implementation
-
put
public void put(String scope, Object key, Object value) throws InvalidScopeException, InternalCacheEngineException
Puts an item in a scope.- Specified by:
putin interfaceCache- Parameters:
scope- scope to add the item tokey- unique key for the item within this scopevalue- value to store in the cache- Throws:
InvalidScopeException- if the specified scope isn't a registered oneInternalCacheEngineException- if an error occurred in the underlying cache implementation
-
put
public void put(String scope, Object key, Object value, long expireAfter, long refreshFrequency, CacheLoader loader, Object... loaderParams) throws InvalidScopeException, InternalCacheEngineException
Puts and item in a scope.- Specified by:
putin interfaceCache- Parameters:
scope- scope to add the item tokey- unique key for the item within this scopevalue- value to store in the cacheexpireAfter- the amount of time (in ticks, where the tick time span is defined at runtime) before the item can be removed from cache. UseCacheItem.NEVER_EXPIREto indicate that there's no expiration time.refreshFrequency- the amount of time (in ticks, where the tick time span is defined at runtime) before items are reloaded into the cache. UseCacheItem.NEVER_REFRESHto indicate that there's no need for the item to be refreshed. Note that when refreshing, there's almost always an old value for the item in the cache before the new value is loaded into the cache. Theloaderparam is also required in order for the item to be refreshed.loader- the cache loader used to load a new value when the item needs to be refreshed. If not specified and therefreshFrequencyis notCacheItem.NEVER_REFRESH, the cache will use a default one.loaderParams- additional parameters the loader could need- Throws:
InvalidScopeException- if the specified scope isn't a registered oneInternalCacheEngineException- if an error occurred in the underlying cache implementation
-
remove
public boolean remove(String scope, Object key) throws InvalidScopeException, InternalCacheEngineException
Removes an item from a scope.- Specified by:
removein interfaceCache- Parameters:
scope- scope to remove the item fromkey- unique key for the item within this scope- Returns:
- true if the removal was successful, false otherwise
- Throws:
InvalidScopeException- if the specified scope isn't a registered oneInternalCacheEngineException- if an error occurred in the underlying cache implementation
-
clearAll
public void clearAll() throws InternalCacheEngineExceptionClears the contents of the entire cache.- Specified by:
clearAllin interfaceCache- Throws:
InternalCacheEngineException- if an error occurred in the underlying cache implementation
-
clearScope
public void clearScope(String scope) throws InvalidScopeException, InternalCacheEngineException
Clears the contents of the specified scope in the cache.- Specified by:
clearScopein interfaceCache- Parameters:
scope- scope to clear- Throws:
InvalidScopeException- if the specified scope isn't a registered oneInternalCacheEngineException- if an error occurred in the underlying cache implementation
-
getStatistics
public CacheStatistics getStatistics(String scope)
Returns the statistics of the specified scope in the cache.- Specified by:
getStatisticsin interfaceCache- Parameters:
scope- scope to inspect- Returns:
- the statistics
-
tick
public void tick()
Called when a tick occurs. A tick is a logical unit of time. It basically symbolizes the time span between calls to this method (i.e. 15 mins) by a job scheduler like Quartz. Checks if some of theCacheItems in the cache have expired or need to be refreshed. If the item has expired, it is removed from the cache. If it needs to be refreshed, it is added to a list of items that need to be refreshed that is later passed to theCacheRefresher.
-
doChecks
protected void doChecks(CacheItem item, List<CacheItem> itemsToRefresh)
Checks if the givenCacheItemhas expired or needs to be refreshed.- Parameters:
item-itemsToRefresh- the list of items where to put the specified item if it needs to be refreshed
-
checkForExpiration
protected boolean checkForExpiration(CacheItem item) throws Exception
Checks if the givenCacheItemhas expired. If so, it is removed from its scope in theCacheStoreAdapter.- Returns:
- true if the item has expired and was removed from the scope, false otherwise
- Throws:
Exception
-
checkForRefresh
protected boolean checkForRefresh(CacheItem item, List<CacheItem> itemsToRefresh)
Checks if a givenCacheItemneeds to be refreshed. If so, then it is added to the listen of items to refresh.- Parameters:
item-itemsToRefresh- the list of items where to put the specified item if it needs to be refreshed- Returns:
- true if the item will be refreshed, false otherwise
-
-