net.sf.ehcache.store

Class MemoryStore

public abstract class MemoryStore extends Object implements Store

An abstract class for the Memory Stores. All Memory store implementations for different policies (e.g: FIFO, LFU, LRU, etc.) should extend this class.

Version: $Id: MemoryStore.java 191 2006-09-03 22:41:48Z gregluck $

Author: Surya Suravarapu

Field Summary
protected Ehcachecache
The cache this store is associated with.
protected DiskStorediskStore
The DiskStore associated with this MemoryStore.
protected Mapmap
Map where items are stored by key.
protected Statusstatus
status.
Constructor Summary
protected MemoryStore(Ehcache cache, DiskStore diskStore)
Constructs things that all MemoryStores have in common.
Method Summary
protected voidclear()
Clears any data structures and places it back to its state when it was first created.
booleancontainsKey(Object key)
An unsynchronized check to see if a key is in the Store.
static MemoryStorecreate(Ehcache cache, DiskStore diskStore)
A factory method to create a MemoryStore.
voiddispose()
Prepares for shutdown.
protected voiddoPut(Element element)
Allow specialised actions over adding the element to the map.
protected voidevict(Element element)
Evict the Element.
voidflush()
Flush to disk.
Elementget(Object key)
Gets an item from the cache.
Object[]getKeyArray()
Gets an Array of the keys for all elements in the memory cache.
ElementgetQuiet(Object key)
Gets an item from the cache, without updating Element statistics.
intgetSize()
Returns the current cache size.
longgetSizeInBytes()
Measures the size of the memory store by measuring the serialized size of all elements.
StatusgetStatus()
Gets the status of the MemoryStore.
protected booleanisFull()
An algorithm to tell if the MemoryStore is at or beyond its carrying capacity.
protected voidnotifyExpiry(Element element)
Before eviction elements are checked.
voidput(Element element)
Puts an item in the cache.
Elementremove(Object key)
Removes an Element from the store.
voidremoveAll()
Remove all of the elements from the store.
protected voidspoolAllToDisk()
Spools all elements to disk, in preparation for shutdown.
protected voidspoolToDisk(Element element)
Puts the element in the DiskStore.

Field Detail

cache

protected Ehcache cache
The cache this store is associated with.

diskStore

protected final DiskStore diskStore
The DiskStore associated with this MemoryStore.

map

protected Map map
Map where items are stored by key.

status

protected Status status
status.

Constructor Detail

MemoryStore

protected MemoryStore(Ehcache cache, DiskStore diskStore)
Constructs things that all MemoryStores have in common.

Parameters: cache diskStore

Method Detail

clear

protected final void clear()
Clears any data structures and places it back to its state when it was first created.

containsKey

public final boolean containsKey(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.

create

public static MemoryStore create(Ehcache cache, DiskStore diskStore)
A factory method to create a MemoryStore.

Parameters: cache diskStore

Returns: an instance of a MemoryStore, configured with the appropriate eviction policy

dispose

public final void dispose()
Prepares for shutdown.

doPut

protected void doPut(Element element)
Allow specialised actions over adding the element to the map.

Parameters: element

evict

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

Evict means that the Element is:

Parameters: element the Element to be evicted.

flush

public final void flush()
Flush to disk.

get

public final Element get(Object key)
Gets an item from the cache.

The last access time in Element is updated.

Parameters: key the cache key

Returns: the element, or null if there was no match for the key

getKeyArray

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

Does not check for expired entries

Returns: An Object[]

getQuiet

public final Element getQuiet(Object key)
Gets an item from the cache, without updating Element statistics.

Parameters: key the cache key

Returns: the element, or null if there was no match for the key

getSize

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

Returns: The size value

getSizeInBytes

public final long getSizeInBytes()
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

Returns: the size, in bytes

getStatus

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

isFull

protected final boolean isFull()
An algorithm to tell if the MemoryStore is at or beyond its carrying capacity.

notifyExpiry

protected final void notifyExpiry(Element element)
Before eviction elements are checked.

Parameters: element

put

public final void put(Element element)
Puts an item in the cache. Note that this automatically results in SpoolingLinkedHashMap being called.

Parameters: element the element to add

remove

public final Element remove(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

removeAll

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

spoolAllToDisk

protected final void spoolAllToDisk()
Spools all elements to disk, in preparation for shutdown.

Relies on being called from a synchronized method

This revised implementation is a little slower but avoids using increased memory during the method.

spoolToDisk

protected final void spoolToDisk(Element element)
Puts the element in the DiskStore. Should only be called if Ehcache is true

Relies on being called from a synchronized method

Parameters: element The Element