Class CompositeCache<K,V>
- All Implemented Interfaces:
ICache<K,
,V> ICacheType
,IRequireScheduler
This is the core of a JCS region. Hence, this simple class is the core of JCS.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.commons.jcs3.engine.behavior.ICacheType
ICacheType.CacheType
-
Field Summary
Fields inherited from interface org.apache.commons.jcs3.engine.behavior.ICache
NAME_COMPONENT_DELIMITER
-
Constructor Summary
ConstructorsConstructorDescriptionCompositeCache
(ICompositeCacheAttributes cattr, IElementAttributes attr) Constructor for the Cache object -
Method Summary
Modifier and TypeMethodDescriptionvoid
dispose()
Flushes all cache items from memory to auxiliary caches and close the auxiliary caches.void
dispose
(boolean fromRemote) Invoked only by CacheManager.protected void
doExpires
(ICacheElement<K, V> element) Gets an item from the cache.protected ICacheElement<K,
V> Look in memory, then disk, remote, or laterally for this item.List<AuxiliaryCache<K,
V>> Get the list of auxiliary caches for this region.AuxiliaryCache<K,
V>[] Deprecated.Use List methodGets the ICompositeCacheAttributes attribute of the Cache object.Gets the cacheName attribute of the Cache object.Gets the cacheType attribute of the Cache object.Gets the default element attribute of the Cache object This returns a copy.getElementAttributes
(K key) Gets the elementAttributes attribute of the Cache object.long
Number of times a requested item was found in and auxiliary cache.long
Number of times a requested item was found in the memory cache.Returns the key matcher used by get matching.Get a set of the keys for all elements in the cachegetKeySet
(boolean localOnly) Get a set of the keys for all elements in the cacheMap<K,
ICacheElement<K, V>> getMatching
(String pattern) Build a map of all the matching elements in all of the auxiliaries and memory.protected Map<K,
ICacheElement<K, V>> getMatching
(String pattern, boolean localOnly) Build a map of all the matching elements in all of the auxiliaries and memory.protected Map<K,
ICacheElement<K, V>> getMatchingFromMemory
(String pattern) Gets the key array from the memcache.Access to the memory cache for instrumentation.long
Number of times a requested element was found but was expired.long
Number of times a requested element was not found.Map<K,
ICacheElement<K, V>> getMultiple
(Set<K> keys) Gets multiple items from the cache based on the given set of keys.protected Map<K,
ICacheElement<K, V>> getMultiple
(Set<K> keys, boolean localOnly) Look in memory, then disk, remote, or laterally for these items.int
getSize()
Gets the size attribute of the Cache object.This returns data gathered for this region and all the auxiliaries it currently uses.getStats()
Gets stats for debugging.Gets the status attribute of the Cache object.long
void
handleElementEvent
(ICacheElement<K, V> element, ElementEventType eventType) If there are event handlers for the item, then create an event and queue it up.boolean
isExpired
(ICacheElement<K, V> element) Determine if the element is expired based on the values of the element attributesboolean
isExpired
(ICacheElement<K, V> element, long timestamp, ElementEventType eventMaxlife, ElementEventType eventIdle) Check if the element is expired based on the values of the element attributesDo not try to go remote or laterally for this get.Map<K,
ICacheElement<K, V>> localGetMatching
(String pattern) Build a map of all the matching elements in all of the auxiliaries and memory.Map<K,
ICacheElement<K, V>> localGetMultiple
(Set<K> keys) Gets multiple items from the cache based on the given set of keys.boolean
localRemove
(K key) Do not propagate removeall laterally or remotely.void
Will not pass the remove message remotely.void
localUpdate
(ICacheElement<K, V> ce) Standard update method.boolean
Removes an item from the cache.protected boolean
fromRemote: If a remove call was made on a cache with both, then the remote should have been called.void
Clears the region.protected void
removeAll
(boolean localOnly) Removes all cached items.void
save()
Calling save cause the entire contents of the memory cache to be flushed to all auxiliaries.void
setAuxCaches
(List<AuxiliaryCache<K, V>> auxCaches) This sets the list of auxiliary caches for this region.void
setAuxCaches
(AuxiliaryCache<K, V>[] auxCaches) Deprecated.Use List methodvoid
Sets the ICompositeCacheAttributes attribute of the Cache object.void
Injector for cache managervoid
Sets the default element attribute of the Cache object.void
Injector for Element event queuevoid
setKeyMatcher
(IKeyMatcher<K> keyMatcher) Sets the key matcher used by get matching.void
setScheduledExecutorService
(ScheduledExecutorService scheduledExecutor) Inject an instance of a central ScheduledExecutorServicevoid
spoolToDisk
(ICacheElement<K, V> ce) Writes the specified element to any disk auxiliaries.toString()
This returns the stats.void
update
(ICacheElement<K, V> ce) Standard update method.protected void
update
(ICacheElement<K, V> cacheElement, boolean localOnly) Put an item into the cache.protected void
updateAuxiliaries
(ICacheElement<K, V> cacheElement, boolean localOnly) This method is responsible for updating the auxiliaries if they are present.
-
Constructor Details
-
CompositeCache
Constructor for the Cache object- Parameters:
cattr
- The cache attributeattr
- The default element attributes
-
-
Method Details
-
setElementEventQueue
Injector for Element event queue- Parameters:
queue
-
-
setCompositeCacheManager
Injector for cache manager- Parameters:
manager
-
-
setScheduledExecutorService
Description copied from interface:IRequireScheduler
Inject an instance of a central ScheduledExecutorService- Specified by:
setScheduledExecutorService
in interfaceIRequireScheduler
- See Also:
-
setAuxCaches
This sets the list of auxiliary caches for this region. It filters out null caches- Parameters:
auxCaches
-- Since:
- 3.1
-
setAuxCaches
Deprecated.Use List methodThis sets the list of auxiliary caches for this region.- Parameters:
auxCaches
-
-
getAuxCacheList
Get the list of auxiliary caches for this region.- Returns:
- a list of auxiliary caches, may be empty, never null
- Since:
- 3.1
-
getAuxCaches
Deprecated.Use List methodGet the list of auxiliary caches for this region.- Returns:
- an array of auxiliary caches, may be empty, never null
-
update
Standard update method.- Specified by:
update
in interfaceICache<K,
V> - Parameters:
ce
-- Throws:
IOException
-
localUpdate
Standard update method.- Parameters:
ce
-- Throws:
IOException
-
update
Put an item into the cache. If it is localOnly, then do no notify remote or lateral auxiliaries.- Parameters:
cacheElement
- the ICacheElement<K, V>localOnly
- Whether the operation should be restricted to local auxiliaries.- Throws:
IOException
-
updateAuxiliaries
protected void updateAuxiliaries(ICacheElement<K, V> cacheElement, boolean localOnly) throws IOExceptionThis method is responsible for updating the auxiliaries if they are present. If it is local only, any lateral and remote auxiliaries will not be updated.Before updating an auxiliary it checks to see if the element attributes permit the operation.
Disk auxiliaries are only updated if the disk cache is not merely used as a swap. If the disk cache is merely a swap, then items will only go to disk when they overflow from memory.
This is called by update(cacheElement, localOnly) after it updates the memory cache.
This is protected to make it testable.
- Parameters:
cacheElement
-localOnly
-- Throws:
IOException
-
spoolToDisk
Writes the specified element to any disk auxiliaries. Might want to rename this "overflow" in case the hub wants to do something else.If JCS is not configured to use the disk as a swap, that is if the CompositeCacheAttribute diskUsagePattern is not SWAP_ONLY, then the item will not be spooled.
- Parameters:
ce
- The CacheElement
-
get
Gets an item from the cache. -
localGet
Do not try to go remote or laterally for this get.- Parameters:
key
-- Returns:
- ICacheElement
-
get
Look in memory, then disk, remote, or laterally for this item. The order is dependent on the order in the cache.ccf file.Do not try to go remote or laterally for this get if it is localOnly. Otherwise try to go remote or lateral if such an auxiliary is configured for this region.
- Parameters:
key
-localOnly
-- Returns:
- ICacheElement
-
doExpires
-
getMultiple
Gets multiple items from the cache based on the given set of keys.- Specified by:
getMultiple
in interfaceICache<K,
V> - Parameters:
keys
-- Returns:
- a map of K key to ICacheElement<K, V> element, or an empty map if there is no data in cache for any of these keys
-
localGetMultiple
Gets multiple items from the cache based on the given set of keys. Do not try to go remote or laterally for this data.- Parameters:
keys
-- Returns:
- a map of K key to ICacheElement<K, V> element, or an empty map if there is no data in cache for any of these keys
-
getMultiple
Look in memory, then disk, remote, or laterally for these items. The order is dependent on the order in the cache.ccf file. Keep looking in each cache location until either the element is found, or the method runs out of places to look.Do not try to go remote or laterally for this get if it is localOnly. Otherwise try to go remote or lateral if such an auxiliary is configured for this region.
- Parameters:
keys
-localOnly
-- Returns:
- ICacheElement
-
getMatching
Build a map of all the matching elements in all of the auxiliaries and memory.- Specified by:
getMatching
in interfaceICache<K,
V> - Parameters:
pattern
-- Returns:
- a map of K key to ICacheElement<K, V> element, or an empty map if there is no data in cache for any matching keys
-
localGetMatching
Build a map of all the matching elements in all of the auxiliaries and memory. Do not try to go remote or laterally for this data.- Parameters:
pattern
-- Returns:
- a map of K key to ICacheElement<K, V> element, or an empty map if there is no data in cache for any matching keys
-
getMatching
Build a map of all the matching elements in all of the auxiliaries and memory. Items in memory will replace from the auxiliaries in the returned map. The auxiliaries are accessed in opposite order. It's assumed that those closer to home are better.Do not try to go remote or laterally for this get if it is localOnly. Otherwise try to go remote or lateral if such an auxiliary is configured for this region.
- Parameters:
pattern
-localOnly
-- Returns:
- a map of K key to ICacheElement<K, V> element, or an empty map if there is no data in cache for any matching keys
-
getMatchingFromMemory
Gets the key array from the memcache. Builds a set of matches. Calls getMultiple with the set. Returns a map: key -> result.- Parameters:
pattern
-- Returns:
- a map of K key to ICacheElement<K, V> element, or an empty map if there is no data in cache for any matching keys
- Throws:
IOException
-
getKeySet
Get a set of the keys for all elements in the cache- Returns:
- A set of the key type
-
getKeySet
Get a set of the keys for all elements in the cache- Parameters:
localOnly
- true if only memory keys are requested- Returns:
- A set of the key type
-
remove
Removes an item from the cache. -
localRemove
Do not propagate removeall laterally or remotely.- Parameters:
key
-- Returns:
- true if the item was already in the cache.
-
remove
fromRemote: If a remove call was made on a cache with both, then the remote should have been called. If it wasn't then the remote is down. we'll assume it is down for all. If it did come from the remote then the cache is remotely configured and lateral removal is unnecessary. If it came laterally then lateral removal is unnecessary. Does this assume that there is only one lateral and remote for the cache? Not really, the initial removal should take care of the problem if the source cache was similarly configured. Otherwise the remote cache, if it had no laterals, would remove all the elements from remotely configured caches, but if those caches had some other weird laterals that were not remotely configured, only laterally propagated then they would go out of synch. The same could happen for multiple remotes. If this looks necessary we will need to build in an identifier to specify the source of a removal.- Parameters:
key
-localOnly
-- Returns:
- true if the item was in the cache, else false
-
removeAll
Clears the region. This command will be sent to all auxiliaries. Some auxiliaries, such as the JDBC disk cache, can be configured to not honor removeAll requests.- Specified by:
removeAll
in interfaceICache<K,
V> - Throws:
IOException
- See Also:
-
localRemoveAll
Will not pass the remove message remotely.- Throws:
IOException
-
removeAll
Removes all cached items.- Parameters:
localOnly
- must pass in false to get remote and lateral aux's updated. This prevents looping.- Throws:
IOException
-
dispose
Flushes all cache items from memory to auxiliary caches and close the auxiliary caches. -
dispose
Invoked only by CacheManager. This method disposes of the auxiliaries one by one. For the disk cache, the items in memory are freed, meaning that they will be sent through the overflow channel to disk. After the auxiliaries are disposed, the memory cache is disposed.- Parameters:
fromRemote
-
-
save
Calling save cause the entire contents of the memory cache to be flushed to all auxiliaries. Though this put is extremely fast, this could bog the cache and should be avoided. The dispose method should call a version of this. Good for testing. -
getSize
Gets the size attribute of the Cache object. This return the number of elements, not the byte size. -
getCacheType
Gets the cacheType attribute of the Cache object.- Specified by:
getCacheType
in interfaceICacheType
- Returns:
- The cacheType value
-
getStatus
Gets the status attribute of the Cache object. -
getStats
Gets stats for debugging. -
getStatistics
This returns data gathered for this region and all the auxiliaries it currently uses.- Returns:
- Statistics and Info on the Region.
-
getCacheName
Gets the cacheName attribute of the Cache object. This is also known as the region name.- Specified by:
getCacheName
in interfaceICache<K,
V> - Returns:
- The cacheName value
-
getElementAttributes
Gets the default element attribute of the Cache object This returns a copy. It does not return a reference to the attributes.- Returns:
- The attributes value
-
setElementAttributes
Sets the default element attribute of the Cache object.- Parameters:
attr
-
-
getCacheAttributes
Gets the ICompositeCacheAttributes attribute of the Cache object.- Returns:
- The ICompositeCacheAttributes value
-
setCacheAttributes
Sets the ICompositeCacheAttributes attribute of the Cache object.- Parameters:
cattr
- The new ICompositeCacheAttributes value
-
getElementAttributes
Gets the elementAttributes attribute of the Cache object.- Parameters:
key
-- Returns:
- The elementAttributes value
- Throws:
CacheException
IOException
-
isExpired
Determine if the element is expired based on the values of the element attributes- Parameters:
element
- the element- Returns:
- true if the element is expired
-
isExpired
public boolean isExpired(ICacheElement<K, V> element, long timestamp, ElementEventType eventMaxlife, ElementEventType eventIdle) Check if the element is expired based on the values of the element attributes- Parameters:
element
- the elementtimestamp
- the timestamp to compare toeventMaxlife
- the event to fire in case the max life time is exceededeventIdle
- the event to fire in case the idle time is exceeded- Returns:
- true if the element is expired
-
handleElementEvent
If there are event handlers for the item, then create an event and queue it up.This does not call handle directly; instead the handler and the event are put into a queue. This prevents the event handling from blocking normal cache operations.
- Parameters:
element
- the itemeventType
- the event type
-
getMemoryCache
Access to the memory cache for instrumentation.- Returns:
- the MemoryCache implementation
-
getHitCountRam
Number of times a requested item was found in the memory cache.- Returns:
- number of hits in memory
-
getHitCountAux
Number of times a requested item was found in and auxiliary cache.- Returns:
- number of auxiliary hits.
-
getMissCountNotFound
Number of times a requested element was not found.- Returns:
- number of misses.
-
getMissCountExpired
Number of times a requested element was found but was expired.- Returns:
- number of found but expired gets.
-
getUpdateCount
- Returns:
- Returns the updateCount.
-
setKeyMatcher
Sets the key matcher used by get matching.- Specified by:
setKeyMatcher
in interfaceICache<K,
V> - Parameters:
keyMatcher
-
-
getKeyMatcher
Returns the key matcher used by get matching.- Returns:
- keyMatcher
-
toString
This returns the stats.
-