Class LruMemoryStore
- java.lang.Object
-
- net.sf.ehcache.store.AbstractStore
-
- net.sf.ehcache.store.LruMemoryStore
-
- All Implemented Interfaces:
Store
public class LruMemoryStore extends AbstractStore
An implementation of a LruMemoryStore.This uses
LinkedHashMapas its backing map. It uses theLinkedHashMapLRU feature. LRU for this implementation means least recently accessed.- Version:
- $Id$
- Author:
- Greg Luck
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classLruMemoryStore.SpoolingLinkedHashMapAn extension of LinkedHashMap which overridesLruMemoryStore.SpoolingLinkedHashMap.removeEldestEntry(java.util.Map.Entry)to persist cache entries to the auxiliary cache before they are removed.
-
Field Summary
Fields Modifier and Type Field Description protected EhcachecacheThe cache this store is associated with.protected StorediskStoreThe DiskStore associated with this MemoryStore.protected java.util.MapmapMap where items are stored by key.protected longmaximumSizeThe maximum size of the store (0 == no limit)protected Statusstatusstatus.-
Fields inherited from class net.sf.ehcache.store.AbstractStore
attributeExtractors, searchManager
-
Fields inherited from interface net.sf.ehcache.store.Store
CLUSTER_COHERENT, NODE_COHERENT
-
-
Constructor Summary
Constructors Constructor Description LruMemoryStore(Ehcache cache, Store diskStore)Constructor for the LruMemoryStore object The backingLinkedHashMapis created with LRU by access order.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanbufferFull()Memory stores are never backed up and always return falseprotected voidclear()Clears any data structures and places it back to its state when it was first created.booleancontainsKey(java.lang.Object key)An unsynchronized check to see if a key is in the Store.booleancontainsKeyInMemory(java.lang.Object key)A check to see if a key is in the Store and is currently held in memory.booleancontainsKeyOffHeap(java.lang.Object key)A check to see if a key is in the Store and is currently held off-heap.booleancontainsKeyOnDisk(java.lang.Object key)A check to see if a key is in the Store and is currently held on disk.voiddispose()Prepares for shutdown.protected voiddoPut(Element element)Allow specialised actions over adding the element to the map.protected voidevict(Element element)Evict theElement.voidexpireElements()Expire all elsments.voidflush()Flush to disk only if the cache is diskPersistent.Elementget(java.lang.Object key)Gets an item from the cache.PolicygetEvictionPolicy()PolicygetInMemoryEvictionPolicy()intgetInMemorySize()Returns the current local in-memory store sizelonggetInMemorySizeInBytes()Gets the size of the in-memory portion of the store, in bytes.java.lang.ObjectgetInternalContext()This should not be used, and will generally return nulljava.util.ListgetKeys()Gets an Array of the keys for all elements in the memory cache.java.lang.ObjectgetMBean()Optional implementation specific MBean exposed by the store.intgetOffHeapSize()Returns the current local off-heap store sizelonggetOffHeapSizeInBytes()Gets the size of the off-heap portion of the store, in bytes.intgetOnDiskSize()Returns the current local on-disk store sizelonggetOnDiskSizeInBytes()Gets the size of the on-disk portion of the store, in bytes.ElementgetQuiet(java.lang.Object key)Gets an item from the cache, without updating statistics.intgetSize()Returns the current cache size.longgetSizeInBytes()Measures the size of the memory store by using the sizeof engine.StatusgetStatus()Gets the status of the MemoryStore.intgetTerracottaClusteredSize()Returns nothing since a disk store isn't clusteredprotected booleanisFull()An algorithm to tell if the MemoryStore is at or beyond its carrying capacity.protected voidnotifyExpiry(Element element)Before eviction elements are checked.booleanput(Element element)Puts an item in the cache.ElementputIfAbsent(Element element)Unsupported in LruMemoryStorebooleanputWithWriter(Element element, CacheWriterManager writerManager)Puts an item into the store and the cache writer manager in an atomic operationElementremove(java.lang.Object key)Removes an Element from the store.voidremoveAll()Remove all of the elements from the store.ElementremoveElement(Element element, ElementValueComparator comparator)Unsupported in LruMemoryStoreElementremoveWithWriter(java.lang.Object key, CacheWriterManager writerManager)Removes an item from the store and the cache writer manager in an atomic operation.Elementreplace(Element element)Unsupported in LruMemoryStorebooleanreplace(Element old, Element element, ElementValueComparator comparator)Unsupported in LruMemoryStorevoidsetEvictionPolicy(Policy policy)Sets the eviction policy strategy.voidsetInMemoryEvictionPolicy(Policy policy)Sets the eviction policy strategy.protected voidspoolAllToDisk()Spools all elements to disk, in preparation for shutdown.protected voidspoolToDisk(Element element)Puts the element in the DiskStore.-
Methods inherited from class net.sf.ehcache.store.AbstractStore
addStoreListener, executeQuery, getAll, getAllQuiet, getEventListenerList, getSearchAttribute, getSearchAttributes, hasAbortedSizeOf, isCacheCoherent, isClusterCoherent, isNodeCoherent, putAll, recalculateSize, removeAll, removeStoreListener, setAttributeExtractors, setNodeCoherent, waitUntilClusterCoherent
-
-
-
-
Field Detail
-
cache
protected Ehcache cache
The cache this store is associated with.
-
map
protected java.util.Map map
Map where items are stored by key.
-
diskStore
protected final Store diskStore
The DiskStore associated with this MemoryStore.
-
status
protected Status status
status.
-
maximumSize
protected long maximumSize
The maximum size of the store (0 == no limit)
-
-
Method Detail
-
put
public final boolean put(Element element) throws CacheException
Puts an item in the cache. Note that this automatically results inLruMemoryStore.SpoolingLinkedHashMap.removeEldestEntry(java.util.Map.Entry)being called.- Parameters:
element- the element to add- Returns:
- true if this is a new put for the key or element is null. Returns false if it was an update.
- Throws:
CacheException
-
putWithWriter
public final boolean putWithWriter(Element element, CacheWriterManager writerManager) throws CacheException
Puts an item into the store and the cache writer manager in an atomic operation- Returns:
- true if this is a new put for the key or element is null. Returns false if it was an update.
- Throws:
CacheException
-
doPut
protected void doPut(Element element) throws CacheException
Allow specialised actions over adding the element to the map.- Parameters:
element-- Throws:
CacheException
-
get
public final Element get(java.lang.Object key)
Gets an item from the cache.The last access time in
Elementis updated.- Parameters:
key- the cache key- Returns:
- the element, or null if there was no match for the key
-
getQuiet
public final Element getQuiet(java.lang.Object key)
Gets an item from the cache, without updating statistics.- Parameters:
key- the cache key- Returns:
- the element, or null if there was no match for the key
-
remove
public final Element remove(java.lang.Object key)
Removes an Element from the store.- Parameters:
key- the key of the Element, usually a String- Returns:
- the Element if one was found, else null
-
removeWithWriter
public final Element removeWithWriter(java.lang.Object key, CacheWriterManager writerManager) throws CacheException
Removes an item from the store and the cache writer manager in an atomic operation.- Throws:
CacheException
-
removeAll
public final void removeAll() throws CacheExceptionRemove all of the elements from the store.- Throws:
CacheException
-
clear
protected final void clear()
Clears any data structures and places it back to its state when it was first created.
-
dispose
public final void dispose()
Prepares for shutdown.
-
flush
public final void flush()
Flush to disk only if the cache is diskPersistent.
-
spoolAllToDisk
protected final void spoolAllToDisk()
Spools all elements to disk, in preparation for shutdown.This revised implementation is a little slower but avoids using increased memory during the method.
-
spoolToDisk
protected void spoolToDisk(Element element)
Puts the element in the DiskStore. Should only be called if isOverflowToDisk is trueRelies on being called from a synchronized method
- Parameters:
element- The Element
-
getStatus
public final Status getStatus()
Gets the status of the MemoryStore.
-
getKeys
public final java.util.List getKeys()
Gets an Array of the keys for all elements in the memory cache.Does not check for expired entries
- Returns:
- An Object[]
-
getSize
public final int getSize()
Returns the current cache size.- Returns:
- The size value
-
getTerracottaClusteredSize
public final int getTerracottaClusteredSize()
Returns nothing since a disk store isn't clustered- Returns:
- returns 0
-
containsKey
public final boolean containsKey(java.lang.Object key)
An unsynchronized check to see if a key is in the Store. No check is made to see if the Element is expired.- Parameters:
key- The Element key- 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.
-
getSizeInBytes
public final long getSizeInBytes() throws CacheExceptionMeasures the size of the memory store by using the sizeof engine.Warning: This method can be very expensive to run. Allow approximately 1 second per 1MB of entries. Running this method could create liveness problems because the object lock is held for a long period
- Returns:
- the size, in bytes
- Throws:
CacheException
-
evict
protected final void evict(Element element) throws CacheException
Evict theElement.Evict means that the
Elementis:- if, the store is diskPersistent, the
Elementis spooled to the DiskStore - if not, the
Elementis removed.
- Parameters:
element- theElementto be evicted.- Throws:
CacheException
- if, the store is diskPersistent, the
-
notifyExpiry
protected final void notifyExpiry(Element element)
Before eviction elements are checked.- Parameters:
element-
-
isFull
protected final boolean isFull()
An algorithm to tell if the MemoryStore is at or beyond its carrying capacity.
-
expireElements
public void expireElements()
Expire all elsments.This is a default implementation which does nothing. Expiry on demand is only implemented for disk stores.
-
bufferFull
public boolean bufferFull()
Memory stores are never backed up and always return false- Returns:
- true if the store write buffer is backed up.
-
getMBean
public java.lang.Object getMBean()
Optional implementation specific MBean exposed by the store.- Returns:
- implementation specific management bean
-
getEvictionPolicy
public Policy getEvictionPolicy()
- Returns:
- the current eviction policy. This may not be the configured policy, if it has been dynamically set.
- See Also:
setEvictionPolicy(Policy)
-
setEvictionPolicy
public void setEvictionPolicy(Policy policy)
Sets the eviction policy strategy. The Store will use a policy at startup. The store may allow changing the eviction policy strategy dynamically. Otherwise implementations will throw an exception if this method is called.- Parameters:
policy- the new policy
-
getInternalContext
public java.lang.Object getInternalContext()
This should not be used, and will generally return null- Returns:
- some internal context (probably null)
-
containsKeyInMemory
public boolean containsKeyInMemory(java.lang.Object key)
A check to see if a key is in the Store and is currently held in memory.- Parameters:
key- The Element key- Returns:
- true if found. No check is made to see if the Element is expired.
-
containsKeyOffHeap
public boolean containsKeyOffHeap(java.lang.Object key)
A check to see if a key is in the Store and is currently held off-heap.- Parameters:
key- The Element key- Returns:
- true if found. No check is made to see if the Element is expired.
-
containsKeyOnDisk
public boolean containsKeyOnDisk(java.lang.Object key)
A check to see if a key is in the Store and is currently held on disk.- Parameters:
key- The Element key- Returns:
- true if found. No check is made to see if the Element is expired.
-
getInMemoryEvictionPolicy
public Policy getInMemoryEvictionPolicy()
- Returns:
- the current eviction policy. This may not be the configured policy, if it has been dynamically set.
- See Also:
Store.setInMemoryEvictionPolicy(Policy)
-
getInMemorySize
public int getInMemorySize()
Returns the current local in-memory store size- Returns:
- the count of the Elements in the Store and in-memory on the local machine
-
getInMemorySizeInBytes
public long getInMemorySizeInBytes()
Gets the size of the in-memory portion of the store, in bytes.This method may be expensive to run, depending on implementation. Implementers may choose to return an approximate size.
- Returns:
- the approximate in-memory size of the store in bytes
-
getOffHeapSize
public int getOffHeapSize()
Returns the current local off-heap store size- Returns:
- the count of the Elements in the Store and off-heap on the local machine
-
getOffHeapSizeInBytes
public long getOffHeapSizeInBytes()
Gets the size of the off-heap portion of the store, in bytes.- Returns:
- the approximate off-heap size of the store in bytes
-
getOnDiskSize
public int getOnDiskSize()
Returns the current local on-disk store size- Returns:
- the count of the Elements in the Store and on-disk on the local machine
-
getOnDiskSizeInBytes
public long getOnDiskSizeInBytes()
Gets the size of the on-disk portion of the store, in bytes.- Returns:
- the on-disk size of the store in bytes
-
setInMemoryEvictionPolicy
public void setInMemoryEvictionPolicy(Policy policy)
Sets the eviction policy strategy. The Store will use a policy at startup. The store may allow changing the eviction policy strategy dynamically. Otherwise implementations will throw an exception if this method is called.- Parameters:
policy- the new policy
-
putIfAbsent
public Element putIfAbsent(Element element) throws java.lang.NullPointerException
Unsupported in LruMemoryStore- Parameters:
element- element to be added- Returns:
- the element previously cached for this key, or null if none.
- Throws:
java.lang.NullPointerException- if the element is null, or has a null key
-
removeElement
public Element removeElement(Element element, ElementValueComparator comparator) throws java.lang.NullPointerException
Unsupported in LruMemoryStore- Parameters:
element- Element to be removedcomparator- ElementValueComparator to use to compare elements- Returns:
- the Element removed or null if no Element was removed
- Throws:
java.lang.NullPointerException- if the element is null, or has a null key
-
replace
public boolean replace(Element old, Element element, ElementValueComparator comparator) throws java.lang.NullPointerException, java.lang.IllegalArgumentException
Unsupported in LruMemoryStore- Parameters:
old- Element to be test againstelement- Element to be cachedcomparator- ElementValueComparator to use to compare elements- Returns:
- true is the Element was replaced
- Throws:
java.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 equal
-
replace
public Element replace(Element element) throws java.lang.NullPointerException
Unsupported in LruMemoryStore- Parameters:
element- Element to be cached- Returns:
- the Element previously cached for this key, or null if no Element was cached
- Throws:
java.lang.NullPointerException- if the Element is null or has a null key
-
-