Top Description Inners Fields Constructors Methods
net.sf.ehcache.store

public Class MemoryStore

extends AbstractStore
implements CacheConfigurationListener, Store
Class Inheritance
All Implemented Interfaces
net.sf.ehcache.store.Store, net.sf.ehcache.config.CacheConfigurationListener
Imports
net.sf.ehcache.CacheEntry, .CacheException, .CacheOperationOutcomes.EvictionOutcome, .Ehcache, .Element, .Status, net.sf.ehcache.concurrent.CacheLockProvider, .ReadWriteLockSync, .Sync, net.sf.ehcache.config.CacheConfiguration, .CacheConfigurationListener, .PinningConfiguration, .SizeOfPolicyConfiguration, net.sf.ehcache.event.RegisteredEventListeners, net.sf.ehcache.pool.Pool, .PoolAccessor, .PoolParticipant, .Size, .SizeOfEngine, .SizeOfEngineLoader, net.sf.ehcache.pool.impl.UnboundedPool, net.sf.ehcache.search.Attribute, net.sf.ehcache.search.attribute.AttributeExtractor, net.sf.ehcache.search.impl.SearchManager, net.sf.ehcache.store.StoreOperationOutcomes.GetOutcome, .StoreOperationOutcomes.PutOutcome, .StoreOperationOutcomes.RemoveOutcome, net.sf.ehcache.store.chm.SelectableConcurrentHashMap, net.sf.ehcache.store.disk.StoreUpdateException, net.sf.ehcache.writer.CacheWriterManager, org.slf4j.Logger, .LoggerFactory, org.terracotta.statistics.OperationStatistic, .Statistic, .StatisticsManager, org.terracotta.statistics.derived.EventRateSimpleMovingAverage, .OperationResultFilter, org.terracotta.statistics.observer.OperationObserver, java.util.ArrayList, .Collection, .EnumSet, .HashSet, .List, .Map, .Set, java.util.concurrent.TimeUnit, java.util.concurrent.locks.Lock, .ReentrantReadWriteLock

A Store implementation suitable for fast, concurrent in memory stores. The policy is determined by that configured in the cache.
Author
Terracotta
Version
$Id: MemoryStore.java 10789 2018-04-26 02:08:13Z adahanne $

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static interface
MemoryStore.BackingFactory

Factory interface to create a MemoryStore backing.

pack-priv static class
MemoryStore.BasicBackingFactory

Simple backing map factory.

private class
MemoryStore.LockProvider

LockProvider implementation that uses the segment locks.

private class
MemoryStore.Participant

PoolParticipant that is used with the HeapPool.

Field Summary

Modifier and TypeField and Description
private final Ehcache
cache

The cache this store is associated with.

private static final int
CONCURRENCY_LEVEL

Set optimisation for 100 concurrent threads.

private final CopyStrategyHandler
pack-priv static final float
protected final OperationObserver<CacheOperationOutcomes.EvictionOutcome>
evictionObserver

Eviction outcome observer

private final OperationObserver<StoreOperationOutcomes.GetOutcome>
private volatile CacheLockProvider
lockProvider

The pool accessor

private static final Logger
private final SelectableConcurrentHashMap
map

Map where items are stored by key.

private static final int
private volatile int
maximumSize

The maximum size of the store (0 == no limit)

private static final CopyStrategyHandler
private volatile Policy
policy

The eviction policy to use

private final PoolAccessor<T extends PoolParticipant>
private final OperationObserver<StoreOperationOutcomes.PutOutcome>
private final OperationObserver<StoreOperationOutcomes.RemoveOutcome>
private volatile Status
status

status.

private final boolean
Inherited from net.sf.ehcache.store.AbstractStore:
attributeExtractorssearchManager

Constructor Summary

AccessConstructor and Description
protected
MemoryStore(Ehcache
the cache
cache
,
Pool
the pool tracking the on-heap usage
pool
,
MemoryStore.BackingFactory factory, final SearchManager
the search manager
searchManager
)

Constructs things that all MemoryStores have in common.

Method Summary

Modifier and TypeMethod and Description
public final boolean
bufferFull()

Implements net.sf.ehcache.store.Store.bufferFull.

Memory stores are never backed up and always return false
public final boolean

Returns:

true if the element can be added without provoking an eviction.
canPutWithoutEvicting
(Element
the element
element
)

Check if adding an element won't provoke an eviction.

private void
checkCapacity(final Element
the element added by the action calling this check
elementJustAdded
)

If the store is over capacity, evict elements until capacity is reached

public final boolean

Returns:

true if found. If this method return false, it means that an Element with the given key is definitely not in the MemoryStore. If it returns true, there is an Element there. An attempt to get it may return null if the Element has expired.
containsKey
(final Object
The Element key
key
)

Implements net.sf.ehcache.store.Store.containsKey.

A check to see if a key is in the Store.
public boolean
containsKeyInMemory(Object
The Element key
key
)

Implements net.sf.ehcache.store.Store.containsKeyInMemory.

A check to see if a key is in the Store and is currently held in memory.
public boolean
containsKeyOffHeap(Object
The Element key
key
)

Implements net.sf.ehcache.store.Store.containsKeyOffHeap.

A check to see if a key is in the Store and is currently held off-heap.
public boolean
containsKeyOnDisk(Object
The Element key
key
)

Implements net.sf.ehcache.store.Store.containsKeyOnDisk.

A check to see if a key is in the Store and is currently held on disk.
public static Store

Returns:

an instance of a NotifyingMemoryStore, configured with the appropriate eviction policy
create
(final Ehcache
the cache
cache
,
Pool
the pool tracking the on-heap usage
pool
)

A factory method to create a MemoryStore.

protected static BruteForceSource

Returns:

a BruteForceSource connected to underlying MemoryStore and matching configuration
createBruteForceSource
(MemoryStore
the underlying store acting as source
memoryStore
,
CacheConfiguration
the cache configuration
cacheConfiguration
)

Factory method to wrap the MemoryStore into a BruteForceSource, accounting for transactional and copy configuration

public void
deregistered(CacheConfiguration config)

Implements net.sf.ehcache.config.CacheConfigurationListener.deregistered.

Indicates that this listener was removed from the given configuration
pack-priv static Policy

Returns:

the chosen eviction policy
determineEvictionPolicy
(Ehcache
the cache
cache
)

Chooses the Policy from the cache configuration

private boolean
public void
diskCapacityChanged(int
previous capacity
oldCapacity
,
int
new capacity
newCapacity
)

Implements net.sf.ehcache.config.CacheConfigurationListener.diskCapacityChanged.

Indicates a change in the configurations disk store capacity
public synchronized void
dispose()

Implements net.sf.ehcache.store.Store.dispose.

Prepares for shutdown.
public Collection<Element>

Returns:

element collection
elementSet
()

Get a collection of the elements in this store

protected boolean

Returns:

true if succeeded, false otherwise
evict
(final Element
the element to be evicted
element
)

Evicts the element from the store

protected Element

Returns:

the evicted element, if any. Otherwise null
expireElement
(final Object
the key
key
)

Evicts the element for the given key, if it exists and is expired

public void
private Element

Returns:

the element chosen as candidate for eviction
findEvictionCandidate
(final Element
the element added by the action calling this check
elementJustAdded
)

Find a "relatively" unused element.

public void
flush()

Implements net.sf.ehcache.store.Store.flush.

Flush to disk only if the cache is diskPersistent.
public final Element

Returns:

the element, or null if there was no match for the key
get
(final Object
the key of the Element
key
)

Implements net.sf.ehcache.store.Store.get.

Gets an item from the cache.
private static boolean
getAdvancedBooleanConfigProperty(String property, String cacheName, boolean defaultValue)

pack-priv static CopyStrategyHandler
protected static int

Returns:

the calculated initialCapacity. Returns 0 if the parameter maximumSizeGoal is less than or equal to 0
getInitialCapacityForLoadFactor
(int
the desired maximum size goal
maximumSizeGoal
,
float
the load factor
loadFactor
)

Calculates the initialCapacity for a desired maximumSize goal and loadFactor.

public Policy
public int
getInMemorySize()

Implements net.sf.ehcache.store.Store.getInMemorySize.

Returns the current local in-memory store size
public long
getInMemorySizeInBytes()

Implements net.sf.ehcache.store.Store.getInMemorySizeInBytes.

Gets the size of the in-memory portion of the store, in bytes.
public Object
getInternalContext()

Implements net.sf.ehcache.store.Store.getInternalContext.

This should not be used, and will generally return null
public final List<?>

Returns:

An List
getKeys
()

Implements net.sf.ehcache.store.Store.getKeys.

Gets an Array of the keys for all elements in the memory cache.
public Object
getMBean()

Implements net.sf.ehcache.store.Store.getMBean.

Optional implementation specific MBean exposed by the store.
public int
getOffHeapSize()

Implements net.sf.ehcache.store.Store.getOffHeapSize.

Returns the current local off-heap store size
public long
getOffHeapSizeInBytes()

Implements net.sf.ehcache.store.Store.getOffHeapSizeInBytes.

Gets the size of the off-heap portion of the store, in bytes.
public int
getOnDiskSize()

Implements net.sf.ehcache.store.Store.getOnDiskSize.

Returns the current local on-disk store size
public long
getOnDiskSizeInBytes()

Implements net.sf.ehcache.store.Store.getOnDiskSizeInBytes.

Gets the size of the on-disk portion of the store, in bytes.
public final Element

Returns:

the element, or null if there was no match for the key
getQuiet
(Object
the cache key
key
)

Implements net.sf.ehcache.store.Store.getQuiet.

Gets an item from the cache, without updating statistics.
public final int

Returns:

The size value
getSize
()

Implements net.sf.ehcache.store.Store.getSize.

Returns the current store size.
public final Status
getStatus()

Implements net.sf.ehcache.store.Store.getStatus.

Gets the status of the MemoryStore.
public final int

Returns:

returns 0
getTerracottaClusteredSize
()

Implements net.sf.ehcache.store.Store.getTerracottaClusteredSize.

Returns nothing since a disk store isn't clustered
private Lock
public boolean
hasAbortedSizeOf()

Overrides net.sf.ehcache.store.AbstractStore.hasAbortedSizeOf.

Implements net.sf.ehcache.store.Store.hasAbortedSizeOf.

Checks if the cache may contain elements for which the SizeOf engine gave up and only partially calculated the size.
private boolean
public final boolean

Returns:

true if the store is full, false otherwise
isFull
()

An algorithm to tell if the MemoryStore is at or beyond its carrying capacity.

protected Set<?>

Returns:

keySet
keySet
()

Returns the keySet for this store

public void
loggingChanged(boolean
old value whether logging was enabled or not
oldValue
,
boolean
new value whether logging was enabled or not
newValue
)

Implements net.sf.ehcache.config.CacheConfigurationListener.loggingChanged.

Indicates a change in the configuration for enable/disable logging
public void
maxBytesLocalDiskChanged(final long
old value in bytes
oldValue
,
final long
new value in bytes
newValue
)

Implements net.sf.ehcache.config.CacheConfigurationListener.maxBytesLocalDiskChanged.

Indicates a change in the configuration for maxBytesLocalDisk setting
public void
maxBytesLocalHeapChanged(final long
old value in bytes
oldValue
,
final long
new value in bytes
newValue
)

Implements net.sf.ehcache.config.CacheConfigurationListener.maxBytesLocalHeapChanged.

Indicates a change in the configuration for maxBytesLocalHeap setting
public void
maxEntriesInCacheChanged(final long
old value
oldValue
,
final long
new value
newValue
)

Implements net.sf.ehcache.config.CacheConfigurationListener.maxEntriesInCacheChanged.

Indicates a change in the configuration for maxEntriesInCache setting
public void
memoryCapacityChanged(int
previous capacity
oldCapacity
,
int
new capacity
newCapacity
)

Implements net.sf.ehcache.config.CacheConfigurationListener.memoryCapacityChanged.

Indicates a change in the configurations memory store capacity
protected void
notifyDirectEviction(final Element
the evicted element
element
)

Called when an element is evicted even before it could be installed inside the store

private void
notifyExpiry(final Element
the element to notify about its expiry
element
)

Before eviction elements are checked.

public boolean
put(final Element
the element to add
element
)

Implements net.sf.ehcache.store.Store.put.

Puts an item in the store.
public Element
putIfAbsent(Element
element to be added
element
)

Implements net.sf.ehcache.store.Store.putIfAbsent.

Put an element in the store if no element is currently mapped to the elements key.
public boolean
putWithWriter(Element element, CacheWriterManager writerManager)

Implements net.sf.ehcache.store.Store.putWithWriter.

Puts an item into the store and the cache writer manager in an atomic operation
public void
recalculateSize(Object
the key
key
)

Overrides net.sf.ehcache.store.AbstractStore.recalculateSize.

Implements net.sf.ehcache.store.Store.recalculateSize.

Recalculate size of the element mapped to the key
public void
registered(CacheConfiguration config)

Implements net.sf.ehcache.config.CacheConfigurationListener.registered.

Indicates that this listener was registered with the given configuration
public Element

Returns:

the Element if one was found, else null
remove
(final Object
the key of the Element, usually a String
key
)

Implements net.sf.ehcache.store.Store.remove.

Removes an Element from the store.
public final void
removeAll()

Implements net.sf.ehcache.store.Store.removeAll.

Remove all of the elements from the store.
public Element
removeElement(Element
Element to be removed
element
,
ElementValueComparator
ElementValueComparator to use to compare elements
comparator
)

Implements net.sf.ehcache.store.Store.removeElement.

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

Returns:

true if an element was removed, false otherwise.
removeElementChosenByEvictionPolicy
(final Element
it is possible for this to be null
elementJustAdded
)

Removes the element chosen by the eviction policy

public final Element
removeWithWriter(Object key, CacheWriterManager writerManager)

Implements net.sf.ehcache.store.Store.removeWithWriter.

Removes an item from the store and the cache writer manager in an atomic operation.
public boolean
replace(Element
Element to be test against
old
,
Element
Element to be cached
element
,
ElementValueComparator
ElementValueComparator to use to compare elements
comparator
)

Implements net.sf.ehcache.store.Store.replace.

Replace the cached element only if the value of the current Element is equal to the value of the supplied old Element.
public Element
replace(Element
Element to be cached
element
)

Implements net.sf.ehcache.store.Store.replace.

Replace the cached element only if an Element is currently cached for this key
private Element[]

Returns:

a random sample of elements
sampleElements
(Object
a key used as a hint indicating where the just added element is
keyHint
)

Uses random numbers to sample the entire map.

public void
public void
setInMemoryEvictionPolicy(Policy
the new policy
policy
)

Implements net.sf.ehcache.store.Store.setInMemoryEvictionPolicy.

Sets the eviction policy strategy.
public void
timeToIdleChanged(long
previous time to idle value
oldTti
,
long
new time to idle value
newTti
)

Implements net.sf.ehcache.config.CacheConfigurationListener.timeToIdleChanged.

Indicates a change in the configurations time to idle
public void
timeToLiveChanged(long
previous time to live value
oldTtl
,
long
new time to live value
newTtl
)

Implements net.sf.ehcache.config.CacheConfigurationListener.timeToLiveChanged.

Indicates a change in the configurations time to live
Inherited from net.sf.ehcache.store.AbstractStore:
addStoreListenerexecuteQuerygetAllgetAllQuietgetEventListenerListgetSearchAttributegetSearchAttributesisCacheCoherentisClusterCoherentisNodeCoherentputAllremoveAllremoveStoreListenersetNodeCoherentwaitUntilClusterCoherent