org.springframework.cache.ehcache
Class EhCacheCache

java.lang.Object
  extended by org.springframework.cache.ehcache.EhCacheCache
All Implemented Interfaces:
Cache<Object,Object>

public class EhCacheCache
extends Object
implements Cache<Object,Object>

Cache implementation on top of an Ehcache instance.

Author:
Costin Leau

Constructor Summary
EhCacheCache(Ehcache ehcache)
          Creates a EhCacheCache instance.
 
Method Summary
 void clear()
          Removes all mappings from the cache.
 boolean containsKey(Object key)
          Returns true if this cache contains a mapping for the specified key.
 boolean containsValue(Object value)
           
 Object get(Object key)
          Returns the value to which this cache maps the specified key.
 String getName()
          Returns the cache name.
 Ehcache getNativeCache()
          Returns the the native, underlying cache provider.
 Object put(Object key, Object value)
          Associates the specified value with the specified key in this cache (optional operation).
 Object putIfAbsent(Object key, Object value)
          If the specified key is not already associated with a value, associate it with the given value.
 Object remove(Object key)
          Removes the mapping for this key from this cache if it is present (optional operation).
 boolean remove(Object key, Object value)
          Remove entry for key only if currently mapped to given value.
 Object replace(Object key, Object value)
          Replace entry for key only if currently mapped to some value.
 boolean replace(Object key, Object oldValue, Object newValue)
          Replace entry for key only if currently mapped to given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EhCacheCache

public EhCacheCache(Ehcache ehcache)
Creates a EhCacheCache instance.

Parameters:
ehcache - backing Ehcache instance
Method Detail

getName

public String getName()
Description copied from interface: Cache
Returns the cache name.

Specified by:
getName in interface Cache<Object,Object>
Returns:
the cache name.

getNativeCache

public Ehcache getNativeCache()
Description copied from interface: Cache
Returns the the native, underlying cache provider.

Specified by:
getNativeCache in interface Cache<Object,Object>
Returns:

clear

public void clear()
Description copied from interface: Cache
Removes all mappings from the cache.

Specified by:
clear in interface Cache<Object,Object>

containsKey

public boolean containsKey(Object key)
Description copied from interface: Cache
Returns true if this cache contains a mapping for the specified key. More formally, returns true if and only if this cache contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)

Specified by:
containsKey in interface Cache<Object,Object>
Parameters:
key - key whose presence in this cache is to be tested.
Returns:
true if this cache contains a mapping for the specified key.

containsValue

public boolean containsValue(Object value)

get

public Object get(Object key)
Description copied from interface: Cache
Returns the value to which this cache maps the specified key. Returns null if the cache contains no mapping for this key. A return value of null does not necessarily indicate that the cache contains no mapping for the key; it's also possible that the cache explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

More formally, if this cache contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

Specified by:
get in interface Cache<Object,Object>
Parameters:
key - key whose associated value is to be returned.
Returns:
the value to which this cache maps the specified key, or null if the cache contains no mapping for this key.
See Also:
Cache.containsKey(Object)

put

public Object put(Object key,
                  Object value)
Description copied from interface: Cache
Associates the specified value with the specified key in this cache (optional operation). If the cache previously contained a mapping for this key, the old value is replaced by the specified value. (A cache m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.))

Specified by:
put in interface Cache<Object,Object>
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the cache previously associated null with the specified key, if the implementation supports null values.

remove

public Object remove(Object key)
Description copied from interface: Cache
Removes the mapping for this key from this cache if it is present (optional operation). More formally, if this cache contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The cache can contain at most one such mapping.)

Returns the value to which the cache previously associated the key, or null if the cache contained no mapping for this key. (A null return can also indicate that the cache previously associated null with the specified key if the implementation supports null values.) The cache will not contain a mapping for the specified key once the call returns.

Specified by:
remove in interface Cache<Object,Object>
Parameters:
key - key whose mapping is to be removed from the cache.
Returns:
previous value associated with specified key, or null if there was no mapping for key.

putIfAbsent

public Object putIfAbsent(Object key,
                          Object value)
Description copied from interface: Cache
If the specified key is not already associated with a value, associate it with the given value. This is equivalent to:
  if (!cache.containsKey(key)) 
     return cache.put(key, value);
  else
     return cache.get(key);
        

Specified by:
putIfAbsent in interface Cache<Object,Object>
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the cache previously associated null with the specified key, if the implementation supports null values.

remove

public boolean remove(Object key,
                      Object value)
Description copied from interface: Cache
Remove entry for key only if currently mapped to given value. Similar to:
   if ((cache.containsKey(key) && cache.get(key).equals(value)) {
      cache.remove(key);
      return true;
   } 
   else 
      return false;
 

Specified by:
remove in interface Cache<Object,Object>
Parameters:
key - key with which the specified value is associated.
value - value associated with the specified key.
Returns:
true if the value was removed, false otherwise

replace

public Object replace(Object key,
                      Object value)
Description copied from interface: Cache
Replace entry for key only if currently mapped to some value. Acts as
 
  if ((cache.containsKey(key)) {
     return cache.put(key, value);
 } else return null;
 
except that the action is performed atomically.

Specified by:
replace in interface Cache<Object,Object>
Parameters:
key - key with which the specified value is associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the cache previously associated null with the specified key, if the implementation supports null values.

replace

public boolean replace(Object key,
                       Object oldValue,
                       Object newValue)
Description copied from interface: Cache
Replace entry for key only if currently mapped to given value. Similar to:
 
  if ((cache.containsKey(key) && cache.get(key).equals(oldValue)) {
     cache.put(key, newValue);
     return true;
 } else return false;
 

Specified by:
replace in interface Cache<Object,Object>
Parameters:
key - key with which the specified value is associated.
oldValue - value expected to be associated with the specified key.
newValue - value to be associated with the specified key.
Returns:
true if the value was replaced