Package org.craftercms.core.cache.impl
Class CacheImpl
java.lang.Object
org.craftercms.core.cache.impl.CacheImpl
- All Implemented Interfaces:
Cache
Cache that implements common functionality, such as logging and
expiration/refresh check every tick, and uses and underlying CacheStoreAdapter to store the items.- Author:
- Sumer Jabri, Alfonso Vásquez
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CacheRefresherUsed to refresh a list of items.protected CacheStoreAdapterAdapter for the cache data structure.protected AtomicIntegerHolds the current number of ticks. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds 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 voidChecks if the givenCacheItemhas expired or needs to be refreshed.Retrieves an item from a scope.Returns a list of the keys of the items present in the specified scope.Returns the list of scopes this cache manages.intReturns the quantity of items present in the specified scope.getStatistics(String scope) Returns the statistics of the specified scope in the cache.booleanReturns true if there's and item with the specified key in the specified scope.booleanReturns true if the specified scope exists in the cache.voidPuts 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.booleanRemoves 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 Details
-
ticks
Holds the current number of ticks. -
cacheStoreAdapter
Adapter for the cache data structure. -
cacheRefresher
Used to refresh a list of items.
-
-
Constructor Details
-
CacheImpl
public CacheImpl()Default constructor. SetstimestampGeneratorto.IncrementalTimestampGenerator.
-
-
Method Details
-
setCacheStoreAdapter
Sets the underlyingCacheStoreAdapter- Parameters:
cacheStoreAdapter- the adapter for the cache data structure
-
setCacheRefresher
Sets theCacheRefresher, used to refresh the items of the cache. -
hasScope
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
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
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
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
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
Clears the contents of the entire cache.- Specified by:
clearAllin interfaceCache- Throws:
InternalCacheEngineException- if an error occurred in the underlying cache implementation
-
clearScope
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
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
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
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
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
-