ehcache

net.sf.ehcache.store
Class LruMemoryStore

java.lang.Object
  extended by net.sf.ehcache.store.LruMemoryStore
All Implemented Interfaces:
Store

public class LruMemoryStore
extends java.lang.Object
implements Store

An implementation of a LruMemoryStore.

This uses LinkedHashMap as its backing map. It uses the LinkedHashMap LRU feature. LRU for this implementation means least recently accessed.

Version:
$Id$
Author:
Greg Luck

Nested Class Summary
 class LruMemoryStore.SpoolingLinkedHashMap
          An extension of LinkedHashMap which overrides LruMemoryStore.SpoolingLinkedHashMap.removeEldestEntry(java.util.Map.Entry) to persist cache entries to the auxiliary cache before they are removed.
 
Field Summary
protected  Ehcache cache
          The cache this store is associated with.
protected  Store diskStore
          The DiskStore associated with this MemoryStore.
protected  java.util.Map map
          Map where items are stored by key.
protected  int maximumSize
          The maximum size of the store (0 == no limit)
protected  Status status
          status.
 
Constructor Summary
LruMemoryStore(Ehcache cache, Store diskStore)
          Constructor for the LruMemoryStore object The backing LinkedHashMap is created with LRU by access order.
 
Method Summary
 boolean bufferFull()
          Memory stores are never backed up and always return false
protected  void clear()
          Clears any data structures and places it back to its state when it was first created.
 boolean containsKey(java.lang.Object key)
          An unsynchronized check to see if a key is in the Store.
 void dispose()
          Prepares for shutdown.
protected  void doPut(Element element)
          Allow specialised actions over adding the element to the map.
protected  void evict(Element element)
          Evict the Element.
 void expireElements()
          Expire all elsments.
 void flush()
          Flush to disk only if the cache is diskPersistent.
 Element get(java.lang.Object key)
          Gets an item from the cache.
 Policy getEvictionPolicy()
           
 java.lang.Object getInternalContext()
          This should not be used, and will generally return null
 java.lang.Object[] getKeyArray()
          Gets an Array of the keys for all elements in the memory cache.
 Element getQuiet(java.lang.Object key)
          Gets an item from the cache, without updating statistics.
 int getSize()
          Returns the current cache size.
 long getSizeInBytes()
          Measures the size of the memory store by measuring the serialized size of all elements.
 Status getStatus()
          Gets the status of the MemoryStore.
 int getTerracottaClusteredSize()
          Returns nothing since a disk store isn't clustered
 boolean isCacheCoherent()
          Indicates whether this store provides a coherent view of all the elements in a cache.
 boolean isClusterCoherent()
          Returns true if the cache is in coherent mode cluster-wide.
protected  boolean isFull()
          An algorithm to tell if the MemoryStore is at or beyond its carrying capacity.
 boolean isNodeCoherent()
          Returns true if the cache is in coherent mode for the current node.
protected  void notifyExpiry(Element element)
          Before eviction elements are checked.
 boolean put(Element element)
          Puts an item in the cache.
 boolean putWithWriter(Element element, CacheWriterManager writerManager)
          Puts an item into the store and the cache writer manager in an atomic operation
 Element remove(java.lang.Object key)
          Removes an Element from the store.
 void removeAll()
          Remove all of the elements from the store.
 Element removeWithWriter(java.lang.Object key, CacheWriterManager writerManager)
          Removes an item from the store and the cache writer manager in an atomic operation.
 void setEvictionPolicy(Policy policy)
          Sets the eviction policy strategy.
 void setNodeCoherent(boolean coherent)
          Sets the cache in coherent or incoherent mode for the current node depending on the parameter.
protected  void spoolAllToDisk()
          Spools all elements to disk, in preparation for shutdown.
protected  void spoolToDisk(Element element)
          Puts the element in the DiskStore.
 void waitUntilClusterCoherent()
          This method waits until the cache is in coherent mode in all the connected nodes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 int maximumSize
The maximum size of the store (0 == no limit)

Constructor Detail

LruMemoryStore

public LruMemoryStore(Ehcache cache,
                      Store diskStore)
Constructor for the LruMemoryStore object The backing LinkedHashMap is created with LRU by access order.

Method Detail

put

public final boolean put(Element element)
                  throws CacheException
Puts an item in the cache. Note that this automatically results in LruMemoryStore.SpoolingLinkedHashMap.removeEldestEntry(java.util.Map.Entry) being called.

Specified by:
put in interface Store
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

Specified by:
putWithWriter in interface Store
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 Element is updated.

Specified by:
get in interface Store
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.

Specified by:
getQuiet in interface Store
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.

Specified by:
remove in interface 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.

Specified by:
removeWithWriter in interface Store
Throws:
CacheException

removeAll

public final void removeAll()
                     throws CacheException
Remove all of the elements from the store.

Specified by:
removeAll in interface 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.

Specified by:
dispose in interface Store

flush

public final void flush()
Flush to disk only if the cache is diskPersistent.

Specified by:
flush in interface Store

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 true

Relies on being called from a synchronized method

Parameters:
element - The Element

getStatus

public final Status getStatus()
Gets the status of the MemoryStore.

Specified by:
getStatus in interface Store

getKeyArray

public final java.lang.Object[] getKeyArray()
Gets an Array of the keys for all elements in the memory cache.

Does not check for expired entries

Specified by:
getKeyArray in interface Store
Returns:
An Object[]

getSize

public final int getSize()
Returns the current cache size.

Specified by:
getSize in interface Store
Returns:
The size value

getTerracottaClusteredSize

public final int getTerracottaClusteredSize()
Returns nothing since a disk store isn't clustered

Specified by:
getTerracottaClusteredSize in interface Store
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.

Specified by:
containsKey in interface Store
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 CacheException
Measures the size of the memory store by measuring the serialized size of all elements. If the objects are not Serializable they count as 0.

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

Specified by:
getSizeInBytes in interface Store
Returns:
the size, in bytes
Throws:
CacheException

evict

protected final void evict(Element element)
                    throws CacheException
Evict the Element.

Evict means that the Element is:

Parameters:
element - the Element to be evicted.
Throws:
CacheException

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.

Specified by:
expireElements in interface Store

bufferFull

public boolean bufferFull()
Memory stores are never backed up and always return false

Specified by:
bufferFull in interface Store
Returns:
true if the store write buffer is backed up.

getEvictionPolicy

public Policy getEvictionPolicy()
Specified by:
getEvictionPolicy in interface Store
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.

Specified by:
setEvictionPolicy in interface Store
Parameters:
policy - the new policy

getInternalContext

public java.lang.Object getInternalContext()
This should not be used, and will generally return null

Specified by:
getInternalContext in interface Store
Returns:
some internal context (probably null)

waitUntilClusterCoherent

public void waitUntilClusterCoherent()
This method waits until the cache is in coherent mode in all the connected nodes. If the cache is already in coherent mode it returns immediately

It applies to coherent clustering mechanisms only e.g. Terracotta

Specified by:
waitUntilClusterCoherent in interface Store

setNodeCoherent

public void setNodeCoherent(boolean coherent)
Sets the cache in coherent or incoherent mode for the current node depending on the parameter. Calling setNodeCoherent(true) when the cache is already in coherent mode or calling setNodeCoherent(false) when already in incoherent mode will be a no-op.

It applies to coherent clustering mechanisms only e.g. Terracotta

Specified by:
setNodeCoherent in interface Store
Parameters:
coherent - true transitions to coherent mode, false to incoherent mode

isNodeCoherent

public boolean isNodeCoherent()
Returns true if the cache is in coherent mode for the current node. Returns false otherwise.

It applies to coherent clustering mechanisms only e.g. Terracotta

Specified by:
isNodeCoherent in interface Store
Returns:
true if the cache is in coherent mode cluster-wide, false otherwise

isClusterCoherent

public boolean isClusterCoherent()
Returns true if the cache is in coherent mode cluster-wide. Returns false otherwise.

It applies to coherent clustering mechanisms only e.g. Terracotta

Specified by:
isClusterCoherent in interface Store
Returns:
true if the cache is in coherent mode cluster-wide, false otherwise

isCacheCoherent

public boolean isCacheCoherent()
Indicates whether this store provides a coherent view of all the elements in a cache. Note that this is same as calling Store.isClusterCoherent() (introduced since 2.0) Use Store.isNodeCoherent() to find out if the cache is coherent in the current node in the cluster

Specified by:
isCacheCoherent in interface Store
Returns:
true if the store is coherent; or false if the store potentially splits the cache storage with another store or isn't internally coherent

ehcache

true