net.sf.ehcache.store
Class MemoryStore

java.lang.Object
  extended bynet.sf.ehcache.store.MemoryStore
All Implemented Interfaces:
java.io.Serializable, Store

public class MemoryStore
extends java.lang.Object
implements Store, java.io.Serializable

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

Version:
$Id: MemoryStore.java,v 1.10 2003/12/25 00:43:11 gregluck Exp $
Author:
Greg Luck
See Also:
Serialized Form

Nested Class Summary
 class MemoryStore.SpoolingLinkedHashMap
          An extension of LinkedHashMap which overrides MemoryStore.SpoolingLinkedHashMap.removeEldestEntry(java.util.Map.Entry) to persist cache entries to the auxiliary cache before they are removed.
 class MemoryStore.SpoolingLRUMap
          An LRU Map implementation based on Apache Commons LRUMap.
 
Field Summary
 
Fields inherited from interface net.sf.ehcache.store.Store
CACHE_HUB, DISK_CACHE, STATUS_ALIVE, STATUS_DISPOSED, STATUS_ERROR, STATUS_UNINITIALISED
 
Constructor Summary
MemoryStore(Cache cache, DiskStore diskStore)
          Constructor for the MemoryStore object The backing LinkedHashMap is created with LRU by access order.
 
Method Summary
 void dispose()
          Prepares for shutdown.
 Element get(java.io.Serializable key)
          Gets an item from the cache The last access time in Element is updated.
 Cache getCache()
          Gets the cache that the MemoryStore is used by
 int getCacheType()
          Returns the cache type.
 java.lang.Object[] getKeyArray()
          Gets an Array of the keys for all elements in the memory cache
 java.lang.String getName()
          Returns the cache name.
 int getSize()
          Returns the current cache size.
 int getStatus()
          Gets the status of the MemoryStore.
 java.util.Map loadMapInstance()
          Tries to load a LinkedHashMap (JDK1.4) and then tries to load an LRUMap.
 void put(Element element)
          Puts an item in the cache.
 boolean remove(java.io.Serializable key)
          Removes an item from the cache.
 void removeAll()
          Remove all of the elements from the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MemoryStore

public MemoryStore(Cache cache,
                   DiskStore diskStore)
Constructor for the MemoryStore object The backing LinkedHashMap is created with LRU by access order.

Method Detail

loadMapInstance

public java.util.Map loadMapInstance()
                              throws CacheException
Tries to load a LinkedHashMap (JDK1.4) and then tries to load an LRUMap. This way applications running JDK1.4 do not have a dependency on Apache commons-collections.

Returns:
a Map, being either LinkedHashMap or
Throws:
CacheException

put

public void put(Element element)
Puts an item in the cache. Note that this automatically results in MemoryStore.SpoolingLinkedHashMap.removeEldestEntry(java.util.Map.Entry) being called.

Specified by:
put in interface Store
Parameters:
element - the element to add

removeAll

public void removeAll()
Remove all of the elements from the cache.

Specified by:
removeAll in interface Store

get

public Element get(java.io.Serializable 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

remove

public boolean remove(java.io.Serializable key)
Removes an item from the cache.

Specified by:
remove in interface Store
Parameters:
key - the key, usually a String
Returns:
true if at least one object was removed.

getKeyArray

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

Returns:
An Object[]

getSize

public int getSize()
Returns the current cache size.

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

getCache

public Cache getCache()
Gets the cache that the MemoryStore is used by


getStatus

public int getStatus()
Gets the status of the MemoryStore. Can be Store.STATUS_ALIVE or Store.STATUS_ERROR

Specified by:
getStatus in interface Store

getCacheType

public int getCacheType()
Returns the cache type.

Specified by:
getCacheType in interface Store
Returns:
The cacheType value

getName

public java.lang.String getName()
Returns the cache name.

Specified by:
getName in interface Store

dispose

public void dispose()
Prepares for shutdown.

Specified by:
dispose in interface Store