public class CompositeCache<K,V> extends Object implements ICache<K,V>, IRequireScheduler
This is the core of a JCS region. Hence, this simple class is the core of JCS.
ICacheType.CacheType
Constructor and Description |
---|
CompositeCache(ICompositeCacheAttributes cattr,
IElementAttributes attr)
Constructor for the Cache object
|
Modifier and Type | Method and Description |
---|---|
void |
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) |
ICacheElement<K,V> |
get(K key)
Gets an item from the cache.
|
protected ICacheElement<K,V> |
get(K key,
boolean localOnly)
Look in memory, then disk, remote, or laterally for this item.
|
AuxiliaryCache<K,V>[] |
getAuxCaches()
Get the list of auxiliary caches for this region.
|
ICompositeCacheAttributes |
getCacheAttributes()
Gets the ICompositeCacheAttributes attribute of the Cache object.
|
String |
getCacheName()
Gets the cacheName attribute of the Cache object.
|
ICacheType.CacheType |
getCacheType()
Gets the cacheType attribute of the Cache object.
|
IElementAttributes |
getElementAttributes()
Gets the default element attribute of the Cache object This returns a copy.
|
IElementAttributes |
getElementAttributes(K key)
Gets the elementAttributes attribute of the Cache object.
|
int |
getHitCountAux()
Number of times a requested item was found in and auxiliary cache.
|
int |
getHitCountRam()
Number of times a requested item was found in the memory cache.
|
IKeyMatcher<K> |
getKeyMatcher()
Returns the key matcher used by get matching.
|
Set<K> |
getKeySet()
Get a set of the keys for all elements in the cache
|
Set<K> |
getKeySet(boolean localOnly)
Get a set of the keys for all elements in the cache
|
Map<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.
|
IMemoryCache<K,V> |
getMemoryCache()
Access to the memory cache for instrumentation.
|
int |
getMissCountExpired()
Number of times a requested element was found but was expired.
|
int |
getMissCountNotFound()
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.
|
ICacheStats |
getStatistics()
This returns data gathered for this region and all the auxiliaries it currently uses.
|
String |
getStats()
Gets stats for debugging.
|
CacheStatus |
getStatus()
Gets the status attribute of the Cache object.
|
int |
getUpdateCount() |
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 attributes
|
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
|
ICacheElement<K,V> |
localGet(K key)
Do 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 |
localRemoveAll()
Will not pass the remove message remotely.
|
void |
localUpdate(ICacheElement<K,V> ce)
Standard update method.
|
boolean |
remove(K key)
Removes an item from the cache.
|
protected boolean |
remove(K key,
boolean localOnly)
fromRemote: If a remove call was made on a cache with both, then the remote should have been
called.
|
void |
removeAll()
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(AuxiliaryCache<K,V>[] auxCaches)
This sets the list of auxiliary caches for this region.
|
void |
setCacheAttributes(ICompositeCacheAttributes cattr)
Sets the ICompositeCacheAttributes attribute of the Cache object.
|
void |
setCompositeCacheManager(CompositeCacheManager manager)
Injector for cache manager
|
void |
setElementAttributes(IElementAttributes attr)
Sets the default element attribute of the Cache object.
|
void |
setElementEventQueue(IElementEventQueue queue)
Injector for Element event queue
|
void |
setKeyMatcher(IKeyMatcher<K> keyMatcher)
Sets the key matcher used by get matching.
|
void |
setScheduledExecutorService(ScheduledExecutorService scheduledExecutor)
Inject an instance of a central ScheduledExecutorService
|
void |
spoolToDisk(ICacheElement<K,V> ce)
Writes the specified element to any disk auxiliaries.
|
String |
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.
|
public CompositeCache(ICompositeCacheAttributes cattr, IElementAttributes attr)
cattr
- The cache attributeattr
- The default element attributespublic void setElementEventQueue(IElementEventQueue queue)
queue
- public void setCompositeCacheManager(CompositeCacheManager manager)
manager
- public void setScheduledExecutorService(ScheduledExecutorService scheduledExecutor)
IRequireScheduler
setScheduledExecutorService
in interface IRequireScheduler
IRequireScheduler.setScheduledExecutorService(java.util.concurrent.ScheduledExecutorService)
public void setAuxCaches(AuxiliaryCache<K,V>[] auxCaches)
auxCaches
- public AuxiliaryCache<K,V>[] getAuxCaches()
public void update(ICacheElement<K,V> ce) throws IOException
update
in interface ICache<K,V>
ce
- IOException
public void localUpdate(ICacheElement<K,V> ce) throws IOException
ce
- IOException
protected void update(ICacheElement<K,V> cacheElement, boolean localOnly) throws IOException
cacheElement
- the ICacheElement<K, V>localOnly
- Whether the operation should be restricted to local auxiliaries.IOException
protected void updateAuxiliaries(ICacheElement<K,V> cacheElement, boolean localOnly) throws IOException
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.
cacheElement
- localOnly
- IOException
public void spoolToDisk(ICacheElement<K,V> ce)
If JCS is not configured to use the disk as a swap, that is if the the CompositeCacheAttribute diskUsagePattern is not SWAP_ONLY, then the item will not be spooled.
ce
- The CacheElementpublic ICacheElement<K,V> get(K key)
get
in interface ICache<K,V>
key
- ICache.get(Object)
public ICacheElement<K,V> localGet(K key)
key
- protected ICacheElement<K,V> get(K key, boolean localOnly)
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.
key
- localOnly
- protected void doExpires(ICacheElement<K,V> element)
public Map<K,ICacheElement<K,V>> getMultiple(Set<K> keys)
getMultiple
in interface ICache<K,V>
keys
- public Map<K,ICacheElement<K,V>> localGetMultiple(Set<K> keys)
keys
- protected Map<K,ICacheElement<K,V>> getMultiple(Set<K> keys, boolean localOnly)
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.
keys
- localOnly
- public Map<K,ICacheElement<K,V>> getMatching(String pattern)
getMatching
in interface ICache<K,V>
pattern
- public Map<K,ICacheElement<K,V>> localGetMatching(String pattern)
pattern
- protected Map<K,ICacheElement<K,V>> getMatching(String pattern, boolean localOnly)
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.
pattern
- localOnly
- protected Map<K,ICacheElement<K,V>> getMatchingFromMemory(String pattern) throws IOException
pattern
- IOException
public Set<K> getKeySet()
public Set<K> getKeySet(boolean localOnly)
localOnly
- true if only memory keys are requestedpublic boolean remove(K key)
remove
in interface ICache<K,V>
key
- ICache.remove(Object)
public boolean localRemove(K key)
key
- protected boolean remove(K key, boolean localOnly)
key
- localOnly
- public void removeAll() throws IOException
removeAll
in interface ICache<K,V>
IOException
ICache.removeAll()
public void localRemoveAll() throws IOException
IOException
protected void removeAll(boolean localOnly) throws IOException
localOnly
- must pass in false to get remote and lateral aux's updated. This prevents
looping.IOException
public void dispose()
public void dispose(boolean fromRemote)
fromRemote
- public void save()
public int getSize()
public ICacheType.CacheType getCacheType()
getCacheType
in interface ICacheType
public CacheStatus getStatus()
public ICacheStats getStatistics()
public String getCacheName()
getCacheName
in interface ICache<K,V>
public IElementAttributes getElementAttributes()
public void setElementAttributes(IElementAttributes attr)
attr
- public ICompositeCacheAttributes getCacheAttributes()
public void setCacheAttributes(ICompositeCacheAttributes cattr)
cattr
- The new ICompositeCacheAttributes valuepublic IElementAttributes getElementAttributes(K key) throws CacheException, IOException
key
- CacheException
IOException
public boolean isExpired(ICacheElement<K,V> element)
element
- the elementpublic boolean isExpired(ICacheElement<K,V> element, long timestamp, ElementEventType eventMaxlife, ElementEventType eventIdle)
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 exceededpublic void handleElementEvent(ICacheElement<K,V> element, ElementEventType eventType)
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.
element
- the itemeventType
- the event typepublic IMemoryCache<K,V> getMemoryCache()
public int getHitCountRam()
public int getHitCountAux()
public int getMissCountNotFound()
public int getMissCountExpired()
public int getUpdateCount()
public void setKeyMatcher(IKeyMatcher<K> keyMatcher)
setKeyMatcher
in interface ICache<K,V>
keyMatcher
- public IKeyMatcher<K> getKeyMatcher()
Copyright © 2002–2018 The Apache Software Foundation. All rights reserved.