org.apache.shindig.common.cache
Class SoftExpiringCache<K,V>

java.lang.Object
  extended by org.apache.shindig.common.cache.SoftExpiringCache<K,V>

public class SoftExpiringCache<K,V>
extends Object

A cache that uses a soft expiration policy. Entries will be kept around for potentially as long as the underlying cache permits, but we keep a timestamp around to retain a notion of the actual age. This provides users of this class with the option of keeping an "expired" entry beyond the normal lifetime. As soon as an entry expires from the underlying cache, it disappears from here as well. Note that this isn't actually a cache itself, but rather a wrapper for one. It differs in the getElement method substantially, since the returned objects are not the same as the V parameter.


Nested Class Summary
static class SoftExpiringCache.CachedObject<V>
           
 
Constructor Summary
SoftExpiringCache(Cache<K,V> cache)
          Create a new TtlCache with the given capacity and TTL values.
 
Method Summary
 void addElement(K key, V value, long maxAge)
          Add an element to the cache, with the intended max age for its cache entry provided in milliseconds.
 SoftExpiringCache.CachedObject<V> getElement(K key)
          Retrieve an element from the cache by key.
 void setTimeSource(TimeSource timeSource)
          Set a new time source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SoftExpiringCache

public SoftExpiringCache(Cache<K,V> cache)
Create a new TtlCache with the given capacity and TTL values. The cache provider provides an implementation of the actual storage.

Parameters:
cache - The underlying cache that will store actual data.
Method Detail

getElement

public SoftExpiringCache.CachedObject<V> getElement(K key)
Retrieve an element from the cache by key. If there is no such element for that key in the cache, or if the element has timed out, null is returned.

Parameters:
key - Key whose element to look up.
Returns:
Element in the cache, if present and not timed out.

addElement

public void addElement(K key,
                       V value,
                       long maxAge)
Add an element to the cache, with the intended max age for its cache entry provided in milliseconds.

Parameters:
key - The key to store the entry for.
value - The value to store.
maxAge - The maximum age for this entry before it is deemed expired.

setTimeSource

public void setTimeSource(TimeSource timeSource)
Set a new time source. For use in testing.

Parameters:
timeSource - New time source to use.


Copyright © 2007-2012. All Rights Reserved.