public class MemoryStore extends AbstractStore implements TierableStore, PoolableStore, CacheConfigurationListener
Modifier and Type | Class and Description |
---|---|
protected static interface |
MemoryStore.BackingFactory
Factory interface to create a MemoryStore backing.
|
(package private) static class |
MemoryStore.BasicBackingFactory
Simple backing map factory.
|
private class |
MemoryStore.LockProvider
LockProvider implementation that uses the segment locks.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
alwaysPutOnHeap |
private Ehcache |
cache
The cache this store is associated with.
|
private static int |
CONCURRENCY_LEVEL
Set optimisation for 100 concurrent threads.
|
(package private) static float |
DEFAULT_LOAD_FACTOR
This is the default from
ConcurrentHashMap . |
private boolean |
elementPinningEnabled |
private RateStatistic |
hitRate |
private CacheLockProvider |
lockProvider
The pool accessor
|
private static org.slf4j.Logger |
LOG |
private SelectableConcurrentHashMap |
map
Map where items are stored by key.
|
private static int |
MAX_EVICTION_RATIO |
private int |
maximumSize
The maximum size of the store (0 == no limit)
|
private RateStatistic |
missRate |
private Policy |
policy
The eviction policy to use
|
private PoolAccessor |
poolAccessor |
private Status |
status
status.
|
private boolean |
storePinned |
attributeExtractors
CLUSTER_COHERENT, NODE_COHERENT
Modifier | Constructor and Description |
---|---|
protected |
MemoryStore(Ehcache cache,
Pool pool,
boolean notify,
MemoryStore.BackingFactory factory)
Constructs things that all MemoryStores have in common.
|
Modifier and Type | Method and Description |
---|---|
boolean |
bufferFull()
Memory stores are never backed up and always return false
|
boolean |
canPutWithoutEvicting(Element element)
Check if adding an element won't provoke an eviction.
|
private void |
checkCapacity(Element elementJustAdded)
If the store is over capacity, evict elements until capacity is reached
|
boolean |
containsKey(java.lang.Object key)
A check to see if a key is in the Store.
|
boolean |
containsKeyInMemory(java.lang.Object key)
A check to see if a key is in the Store and is currently held in memory.
|
boolean |
containsKeyOffHeap(java.lang.Object key)
A check to see if a key is in the Store and is currently held off-heap.
|
boolean |
containsKeyOnDisk(java.lang.Object key)
A check to see if a key is in the Store and is currently held on disk.
|
static MemoryStore |
create(Ehcache cache,
Pool pool)
A factory method to create a MemoryStore.
|
void |
deregistered(CacheConfiguration config)
Indicates that this listener was removed from the given configuration
|
private static Policy |
determineEvictionPolicy(Ehcache cache)
Chooses the Policy from the cache configuration
|
private boolean |
determineStorePinned(CacheConfiguration cacheConfiguration) |
void |
diskCapacityChanged(int oldCapacity,
int newCapacity)
Indicates a change in the configurations disk store capacity
|
void |
dispose()
Prepares for shutdown.
|
java.util.Collection<Element> |
elementSet()
Get a collection of the elements in this store
|
protected boolean |
evict(Element element)
Evicts the element from the store
|
boolean |
evictFromOnDisk(int count,
long size)
Perform eviction to release on-disk resources
|
boolean |
evictFromOnHeap(int count,
long size)
Perform eviction to release on-heap resources
|
protected Element |
expireElement(java.lang.Object key)
Evicts the element for the given key, if it exists and is expired
|
void |
expireElements()
Expire all elements.
|
void |
fill(Element element)
Add this element to the cache if the key is already present or the add
can succeed without resorting to eviction.
|
private Element |
findEvictionCandidate(Element elementJustAdded)
Find a "relatively" unused element.
|
void |
flush()
Flush to disk only if the cache is diskPersistent.
|
Element |
get(java.lang.Object key)
Gets an item from the cache.
|
private static boolean |
getAdvancedBooleanConfigProperty(java.lang.String property,
java.lang.String cacheName,
boolean defaultValue) |
long |
getApproximateDiskByteSize()
Return the approximate disk size in bytes
|
long |
getApproximateDiskCountSize()
Return the approximate disk size
|
float |
getApproximateDiskHitRate()
Return the approximate disk hit rate
|
float |
getApproximateDiskMissRate()
Return the approximate disk miss rate
|
long |
getApproximateHeapByteSize()
Return the approximate heap size in bytes
|
long |
getApproximateHeapCountSize()
Return the approximate heap size
|
float |
getApproximateHeapHitRate()
Return the approximate heap hit rate
|
float |
getApproximateHeapMissRate()
Return the approximate heap miss rate
|
protected static int |
getInitialCapacityForLoadFactor(int maximumSizeGoal,
float loadFactor)
Calculates the initialCapacity for a desired maximumSize goal and loadFactor.
|
Policy |
getInMemoryEvictionPolicy() |
int |
getInMemorySize()
Returns the current local in-memory store size
|
long |
getInMemorySizeInBytes()
Gets the size of the in-memory portion of the store, in bytes.
|
java.lang.Object |
getInternalContext()
This should not be used, and will generally return null
|
java.util.List<?> |
getKeys()
Gets an Array of the keys for all elements in the memory cache.
|
java.lang.Object |
getMBean()
Optional implementation specific MBean exposed by the store.
|
int |
getOffHeapSize()
Returns the current local off-heap store size
|
long |
getOffHeapSizeInBytes()
Gets the size of the off-heap portion of the store, in bytes.
|
int |
getOnDiskSize()
Returns the current local on-disk store size
|
long |
getOnDiskSizeInBytes()
Gets the size of the on-disk portion of the store, in bytes.
|
java.util.Set |
getPresentPinnedKeys()
Returns all the keys that are pinned, for which there is a mapping present
|
Element |
getQuiet(java.lang.Object key)
Gets an item from the cache, without updating statistics.
|
int |
getSize()
Returns the current store size.
|
Status |
getStatus()
Gets the status of the MemoryStore.
|
int |
getTerracottaClusteredSize()
Returns nothing since a disk store isn't clustered
|
private java.util.concurrent.locks.Lock |
getWriteLock(java.lang.Object key) |
boolean |
hasAbortedSizeOf()
Checks if the cache may contain elements for which the SizeOf engine gave up and only
partially calculated the size.
|
private boolean |
isClockEviction() |
boolean |
isFull()
An algorithm to tell if the MemoryStore is at or beyond its carrying capacity.
|
boolean |
isPersistent()
Is this store persistent (data survives a JVM restart)
|
boolean |
isPinned(java.lang.Object key)
Check if the key is pinned
|
private boolean |
isPinningEnabled(Element element) |
boolean |
isTierPinned()
Is this TierableStore pinned ?
|
protected java.util.Set<?> |
keySet()
Returns the keySet for this store
|
void |
loggingChanged(boolean oldValue,
boolean newValue)
Indicates a change in the configuration for enable/disable logging
|
void |
maxBytesLocalDiskChanged(long oldValue,
long newValue)
Indicates a change in the configuration for maxBytesLocalDisk setting
|
void |
maxBytesLocalHeapChanged(long oldValue,
long newValue)
Indicates a change in the configuration for maxBytesLocalHeap setting
|
void |
memoryCapacityChanged(int oldCapacity,
int newCapacity)
Indicates a change in the configurations memory store capacity
|
protected void |
notifyDirectEviction(Element element)
Called when an element is evicted even before it could be installed inside the store
|
private void |
notifyExpiry(Element element)
Before eviction elements are checked.
|
boolean |
put(Element element)
Puts an item in the store.
|
Element |
putIfAbsent(Element element)
Put an element in the store if no element is currently mapped to the elements key.
|
boolean |
putWithWriter(Element element,
CacheWriterManager writerManager)
Puts an item into the store and the cache writer manager in an atomic operation
|
void |
recalculateSize(java.lang.Object key)
Recalculate size of the element mapped to the key
|
void |
registered(CacheConfiguration config)
Indicates that this listener was registered with the given configuration
|
Element |
remove(java.lang.Object key)
Removes an Element from the store.
|
void |
removeAll()
Remove all of the elements from the store.
|
Element |
removeElement(Element element,
ElementValueComparator comparator)
Remove the Element mapped to the key for the supplied element if the value of the supplied Element
is equal to the value of the cached Element.
|
private boolean |
removeElementChosenByEvictionPolicy(Element elementJustAdded)
Removes the element chosen by the eviction policy
|
boolean |
removeIfNotPinned(java.lang.Object key)
This method will only remove if the element or the store is not pinned and the key is present in the store
|
void |
removeNoReturn(java.lang.Object key)
Removes an item from the cache.
|
Element |
removeWithWriter(java.lang.Object key,
CacheWriterManager writerManager)
Removes an item from the store and the cache writer manager in an atomic operation.
|
Element |
replace(Element element)
Replace the cached element only if an Element is currently cached for this key
|
boolean |
replace(Element old,
Element element,
ElementValueComparator comparator)
Replace the cached element only if the value of the current Element is equal to the value of the
supplied old Element.
|
private Element[] |
sampleElements(java.lang.Object keyHint)
Uses random numbers to sample the entire map.
|
void |
setInMemoryEvictionPolicy(Policy policy)
Sets the eviction policy strategy.
|
void |
setPinned(java.lang.Object key,
boolean pinned)
Mark the key as pinned or not
|
void |
timeToIdleChanged(long oldTti,
long newTti)
Indicates a change in the configurations time to idle
|
void |
timeToLiveChanged(long oldTtl,
long newTtl)
Indicates a change in the configurations time to live
|
void |
unpinAll()
unpin all pinned keys
|
addStoreListener, executeQuery, getAll, getAllQuiet, getEventListenerList, getSearchAttribute, isCacheCoherent, isClusterCoherent, isNodeCoherent, putAll, removeAll, removeStoreListener, setAttributeExtractors, setNodeCoherent, waitUntilClusterCoherent
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addStoreListener, executeQuery, getAll, getAllQuiet, getSearchAttribute, isCacheCoherent, isClusterCoherent, isNodeCoherent, putAll, removeAll, removeStoreListener, setAttributeExtractors, setNodeCoherent, waitUntilClusterCoherent
static final float DEFAULT_LOAD_FACTOR
ConcurrentHashMap
. It should never be used, because we size
the map to the max size of the store.private static final int CONCURRENCY_LEVEL
private static final int MAX_EVICTION_RATIO
private static final org.slf4j.Logger LOG
private final boolean alwaysPutOnHeap
private final Ehcache cache
private final SelectableConcurrentHashMap map
private final PoolAccessor poolAccessor
private final RateStatistic hitRate
private final RateStatistic missRate
private final boolean storePinned
private final boolean elementPinningEnabled
private volatile int maximumSize
private volatile Status status
private volatile Policy policy
private volatile CacheLockProvider lockProvider
protected MemoryStore(Ehcache cache, Pool pool, boolean notify, MemoryStore.BackingFactory factory)
cache
- the cachepool
- the pool tracking the on-heap usagenotify
- whether to notify the Cache's EventNotificationService on eviction and expiryprivate boolean determineStorePinned(CacheConfiguration cacheConfiguration)
protected static int getInitialCapacityForLoadFactor(int maximumSizeGoal, float loadFactor)
maximumSizeGoal
- the desired maximum size goalloadFactor
- the load factorpublic static MemoryStore create(Ehcache cache, Pool pool)
cache
- the cachepool
- the pool tracking the on-heap usagepublic void setPinned(java.lang.Object key, boolean pinned)
public boolean isPinned(java.lang.Object key)
private boolean isPinningEnabled(Element element)
public void fill(Element element)
fill
in interface TierableStore
element
- element to be addedpublic boolean removeIfNotPinned(java.lang.Object key)
removeIfNotPinned
in interface TierableStore
key
- the key to the elementStore.remove(Object)
public boolean put(Element element) throws CacheException
put
in interface Store
element
- the element to addCacheException
public boolean putWithWriter(Element element, CacheWriterManager writerManager) throws CacheException
putWithWriter
in interface Store
CacheException
public final Element get(java.lang.Object key)
Element
is updated.public final Element getQuiet(java.lang.Object key)
public Element remove(java.lang.Object key)
public void removeNoReturn(java.lang.Object key)
removeNoReturn
in interface TierableStore
public boolean isTierPinned()
isTierPinned
in interface TierableStore
public java.util.Set getPresentPinnedKeys()
getPresentPinnedKeys
in interface TierableStore
public boolean isPersistent()
isPersistent
in interface TierableStore
public final Element removeWithWriter(java.lang.Object key, CacheWriterManager writerManager) throws CacheException
removeWithWriter
in interface Store
CacheException
public final boolean bufferFull()
bufferFull
in interface Store
public void expireElements()
expireElements
in interface Store
protected Element expireElement(java.lang.Object key)
key
- the keyprivate static Policy determineEvictionPolicy(Ehcache cache)
cache
- the cachepublic final void removeAll() throws CacheException
removeAll
in interface Store
CacheException
public void flush()
public final java.util.List<?> getKeys()
protected java.util.Set<?> keySet()
public final int getSize()
public final int getTerracottaClusteredSize()
getTerracottaClusteredSize
in interface Store
public final boolean containsKey(java.lang.Object key)
containsKey
in interface Store
key
- The Element keyprivate void notifyExpiry(Element element)
element
- the element to notify about its expiryprotected void notifyDirectEviction(Element element)
element
- the evicted elementpublic final boolean isFull()
public final boolean canPutWithoutEvicting(Element element)
element
- the elementprivate void checkCapacity(Element elementJustAdded)
elementJustAdded
- the element added by the action calling this checkprivate boolean removeElementChosenByEvictionPolicy(Element elementJustAdded)
elementJustAdded
- it is possible for this to be nullprivate Element findEvictionCandidate(Element elementJustAdded)
elementJustAdded
- the element added by the action calling this checkprivate Element[] sampleElements(java.lang.Object keyHint)
keyHint
- a key used as a hint indicating where the just added element ispublic java.lang.Object getInternalContext()
getInternalContext
in interface Store
public final Status getStatus()
public void timeToIdleChanged(long oldTti, long newTti)
timeToIdleChanged
in interface CacheConfigurationListener
oldTti
- previous time to idle valuenewTti
- new time to idle valuepublic void timeToLiveChanged(long oldTtl, long newTtl)
timeToLiveChanged
in interface CacheConfigurationListener
oldTtl
- previous time to live valuenewTtl
- new time to live valuepublic void diskCapacityChanged(int oldCapacity, int newCapacity)
diskCapacityChanged
in interface CacheConfigurationListener
oldCapacity
- previous capacitynewCapacity
- new capacitypublic void loggingChanged(boolean oldValue, boolean newValue)
loggingChanged
in interface CacheConfigurationListener
oldValue
- old value whether logging was enabled or notnewValue
- new value whether logging was enabled or notpublic void memoryCapacityChanged(int oldCapacity, int newCapacity)
memoryCapacityChanged
in interface CacheConfigurationListener
oldCapacity
- previous capacitynewCapacity
- new capacityprivate boolean isClockEviction()
public void registered(CacheConfiguration config)
registered
in interface CacheConfigurationListener
public void deregistered(CacheConfiguration config)
deregistered
in interface CacheConfigurationListener
public void maxBytesLocalHeapChanged(long oldValue, long newValue)
maxBytesLocalHeapChanged
in interface CacheConfigurationListener
oldValue
- old value in bytesnewValue
- new value in bytespublic void maxBytesLocalDiskChanged(long oldValue, long newValue)
maxBytesLocalDiskChanged
in interface CacheConfigurationListener
oldValue
- old value in bytesnewValue
- new value in bytespublic boolean containsKeyInMemory(java.lang.Object key)
containsKeyInMemory
in interface Store
key
- The Element keypublic boolean containsKeyOffHeap(java.lang.Object key)
containsKeyOffHeap
in interface Store
key
- The Element keypublic boolean containsKeyOnDisk(java.lang.Object key)
containsKeyOnDisk
in interface Store
key
- The Element keypublic Policy getInMemoryEvictionPolicy()
getInMemoryEvictionPolicy
in interface Store
Store.setInMemoryEvictionPolicy(Policy)
public int getInMemorySize()
getInMemorySize
in interface Store
public long getInMemorySizeInBytes()
getInMemorySizeInBytes
in interface Store
public int getOffHeapSize()
getOffHeapSize
in interface Store
public long getOffHeapSizeInBytes()
getOffHeapSizeInBytes
in interface Store
public int getOnDiskSize()
getOnDiskSize
in interface Store
public long getOnDiskSizeInBytes()
getOnDiskSizeInBytes
in interface Store
public boolean hasAbortedSizeOf()
hasAbortedSizeOf
in interface Store
hasAbortedSizeOf
in class AbstractStore
public void setInMemoryEvictionPolicy(Policy policy)
setInMemoryEvictionPolicy
in interface Store
policy
- the new policypublic Element putIfAbsent(Element element) throws java.lang.NullPointerException
putIfAbsent
in interface Store
element
- element to be addedjava.lang.NullPointerException
- if the element is null, or has a null keyprotected boolean evict(Element element)
element
- the element to be evictedpublic Element removeElement(Element element, ElementValueComparator comparator) throws java.lang.NullPointerException
removeElement
in interface Store
element
- Element to be removedcomparator
- ElementValueComparator to use to compare elementsjava.lang.NullPointerException
- if the element is null, or has a null keypublic boolean replace(Element old, Element element, ElementValueComparator comparator) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
replace
in interface Store
old
- Element to be test againstelement
- Element to be cachedcomparator
- ElementValueComparator to use to compare elementsjava.lang.NullPointerException
- if the either Element is null or has a null keyjava.lang.IllegalArgumentException
- if the two Element keys are non-null but not equalpublic Element replace(Element element) throws java.lang.NullPointerException
public java.lang.Object getMBean()
public boolean evictFromOnHeap(int count, long size)
evictFromOnHeap
in interface PoolableStore
count
- the number of elements to evictsize
- the size in bytes to free (hint)public boolean evictFromOnDisk(int count, long size)
evictFromOnDisk
in interface PoolableStore
count
- the number of elements to evictsize
- the size in bytes to free (hint)public float getApproximateDiskHitRate()
getApproximateDiskHitRate
in interface PoolableStore
public float getApproximateDiskMissRate()
getApproximateDiskMissRate
in interface PoolableStore
public long getApproximateDiskCountSize()
getApproximateDiskCountSize
in interface PoolableStore
public long getApproximateDiskByteSize()
getApproximateDiskByteSize
in interface PoolableStore
public float getApproximateHeapHitRate()
getApproximateHeapHitRate
in interface PoolableStore
public float getApproximateHeapMissRate()
getApproximateHeapMissRate
in interface PoolableStore
public long getApproximateHeapCountSize()
getApproximateHeapCountSize
in interface PoolableStore
public long getApproximateHeapByteSize()
getApproximateHeapByteSize
in interface PoolableStore
private java.util.concurrent.locks.Lock getWriteLock(java.lang.Object key)
public java.util.Collection<Element> elementSet()
private static boolean getAdvancedBooleanConfigProperty(java.lang.String property, java.lang.String cacheName, boolean defaultValue)
public void recalculateSize(java.lang.Object key)
AbstractStore
recalculateSize
in interface Store
recalculateSize
in class AbstractStore
key
- the key