public class CacheImpl extends Object implements Cache
Cache that implements common functionality, such as logging and
expiration/refresh check every tick, and uses and underlying CacheStoreAdapter to store the items.| Modifier and Type | Field and Description |
|---|---|
protected CacheRefresher |
cacheRefresher
Used to refresh a list of items.
|
protected CacheStoreAdapter |
cacheStoreAdapter
Adapter for the cache data structure.
|
protected AtomicInteger |
ticks
Holds the current number of ticks.
|
| Constructor and Description |
|---|
CacheImpl()
Default constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addScope(String scope,
int maxItemsInMemory)
Adds a new scope to the cache.
|
protected boolean |
checkForExpiration(CacheItem item)
Checks if the given
CacheItem has expired. |
protected boolean |
checkForRefresh(CacheItem item,
List<CacheItem> itemsToRefresh)
Checks if a given
CacheItem needs to be refreshed. |
void |
clearAll()
Clears the contents of the entire cache.
|
void |
clearScope(String scope)
Clears the contents of the specified scope in the cache.
|
protected void |
doChecks(CacheItem item,
List<CacheItem> itemsToRefresh)
Checks if the given
CacheItem has expired or needs to be refreshed. |
CacheItem |
get(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.
|
int |
getSize(String scope)
Returns the quantity of items present in the specified scope.
|
CacheStatistics |
getStatistics(String scope)
Returns the statistics of the specified scope in the cache.
|
boolean |
hasKey(String scope,
Object key)
Returns true if there's and item with the specified key in the specified scope.
|
boolean |
hasScope(String scope)
Returns true if the specified scope exists in the cache.
|
void |
put(String scope,
Object key,
Object value)
Puts an item in a scope.
|
void |
put(String scope,
Object key,
Object value,
long expireAfter,
long refreshFrequency,
CacheLoader loader,
Object... loaderParams)
Puts and item in a scope.
|
boolean |
remove(String scope,
Object key)
Removes an item from a scope.
|
void |
removeScope(String scope)
Removes an existing scope and all the items that are in it.
|
void |
setCacheRefresher(CacheRefresher cacheRefresher)
Sets the
CacheRefresher, used to refresh the items of the cache. |
void |
setCacheStoreAdapter(CacheStoreAdapter cacheStoreAdapter)
Sets the underlying
CacheStoreAdapter |
void |
tick()
Called when a tick occurs.
|
protected AtomicInteger ticks
protected CacheStoreAdapter cacheStoreAdapter
protected CacheRefresher cacheRefresher
public CacheImpl()
timestampGenerator to .IncrementalTimestampGenerator.public void setCacheStoreAdapter(CacheStoreAdapter cacheStoreAdapter)
CacheStoreAdaptercacheStoreAdapter - the adapter for the cache data structurepublic void setCacheRefresher(CacheRefresher cacheRefresher)
CacheRefresher, used to refresh the items of the cache.public boolean hasScope(String scope) throws InternalCacheEngineException
hasScope in interface Cachescope - the name of the scopeInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic Collection<String> getScopes() throws InternalCacheEngineException
getScopes in interface CacheInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic void addScope(String scope, int maxItemsInMemory) throws InternalCacheEngineException
addScope in interface Cachescope - the name of the scopemaxItemsInMemory - the maximum number of items in memory, before they are evictedInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic void removeScope(String scope) throws InvalidScopeException, InternalCacheEngineException
removeScope in interface Cachescope - scope to removeInvalidScopeException - if the specified scope isn't a registered oneInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic int getSize(String scope) throws InvalidScopeException, InternalCacheEngineException
getSize in interface CacheInvalidScopeException - if the specified scope isn't a registered oneInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic Collection<Object> getKeys(String scope) throws InvalidScopeException, InternalCacheEngineException
getKeys in interface CacheInvalidScopeException - if the specified scope isn't a registered oneInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic boolean hasKey(String scope, Object key) throws InvalidScopeException, InternalCacheEngineException
hasKey in interface CacheInvalidScopeException - if the specified scope isn't a registered oneInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic CacheItem get(String scope, Object key) throws InvalidScopeException, InternalCacheEngineException
get in interface Cachescope - scope to get the item fromkey - unique key for the item within this scopeInvalidScopeException - if the specified scope isn't a registered oneInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic void put(String scope, Object key, Object value) throws InvalidScopeException, InternalCacheEngineException
put in interface Cachescope - scope to add the item tokey - unique key for the item within this scopevalue - value to store in the cacheInvalidScopeException - if the specified scope isn't a registered oneInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic void put(String scope, Object key, Object value, long expireAfter, long refreshFrequency, CacheLoader loader, Object... loaderParams) throws InvalidScopeException, InternalCacheEngineException
put in interface Cachescope - 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. Use CacheItem.NEVER_EXPIRE to 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. Use CacheItem.NEVER_REFRESH to 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. The loader param 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
the refreshFrequency is not CacheItem.NEVER_REFRESH,
the cache will use a default one.loaderParams - additional parameters the loader could needInvalidScopeException - if the specified scope isn't a registered oneInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic boolean remove(String scope, Object key) throws InvalidScopeException, InternalCacheEngineException
remove in interface Cachescope - scope to remove the item fromkey - unique key for the item within this scopeInvalidScopeException - if the specified scope isn't a registered oneInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic void clearAll()
throws InternalCacheEngineException
clearAll in interface CacheInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic void clearScope(String scope) throws InvalidScopeException, InternalCacheEngineException
clearScope in interface Cachescope - scope to clearInvalidScopeException - if the specified scope isn't a registered oneInternalCacheEngineException - if an error occurred in the underlying cache implementationpublic CacheStatistics getStatistics(String scope)
getStatistics in interface Cachescope - scope to inspectpublic void tick()
CacheItems 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 the CacheRefresher.protected void doChecks(CacheItem item, List<CacheItem> itemsToRefresh)
CacheItem has expired or needs to be refreshed.item - itemsToRefresh - the list of items where to put the specified item if it needs to be refreshedprotected boolean checkForExpiration(CacheItem item) throws Exception
CacheItem has expired. If so, it is removed from its scope in the
CacheStoreAdapter.Exceptionprotected boolean checkForRefresh(CacheItem item, List<CacheItem> itemsToRefresh)
CacheItem needs to be refreshed. If so, then it is added to the listen of items to
refresh.item - itemsToRefresh - the list of items where to put the specified item if it needs to be refreshedCopyright © 2018 CrafterCMS. All rights reserved.