goldengate.common.lru
Class AbstractLruCache<K,V>

java.lang.Object
  extended by goldengate.common.lru.AbstractLruCache<K,V>
All Implemented Interfaces:
InterfaceLruCache<K,V>
Direct Known Subclasses:
SynchronizedLruCache

public abstract class AbstractLruCache<K,V>
extends Object
implements InterfaceLruCache<K,V>

Base class for concrete implementations

Author:
Frederic Bregier, Damian Momot

Constructor Summary
protected AbstractLruCache(long ttl)
          Constructs BaseLruCache
 
Method Summary
 boolean contains(K key)
          Checks whether cache contains valid entry for key
protected  InterfaceLruCacheEntry<V> createEntry(V value, long ttl)
          Creates new LruCacheEntry.
 V get(K key)
          Returns value cached with key.
 V get(K key, Callable<V> callback)
          Tries to get element from cache.
 V get(K key, Callable<V> callback, long ttl)
          Tries to get element from cache.
protected abstract  InterfaceLruCacheEntry<V> getEntry(K key)
          Returns LruCacheEntry mapped by key or null if it does not exist
 long getTtl()
          Returns cache TTL
protected  V getValue(K key)
          Tries to retrieve value by it's key.
 boolean isEmpty()
          Checks whether cache is empty.
 void put(K key, V value)
          Puts value under key into cache.
 void put(K key, V value, long ttl)
          Puts value under key into cache with desired TTL
protected abstract  void putEntry(K key, InterfaceLruCacheEntry<V> entry)
          Puts entry into cache
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface goldengate.common.lru.InterfaceLruCache
clear, forceClearOldest, getCapacity, getSize, remove
 

Constructor Detail

AbstractLruCache

protected AbstractLruCache(long ttl)
Constructs BaseLruCache

Parameters:
ttl -
Throws:
IllegalArgumentException - if ttl is not positive
Method Detail

contains

public boolean contains(K key)
Description copied from interface: InterfaceLruCache
Checks whether cache contains valid entry for key

Specified by:
contains in interface InterfaceLruCache<K,V>
Returns:
true if cache contains key and entry is valid

createEntry

protected InterfaceLruCacheEntry<V> createEntry(V value,
                                                long ttl)
Creates new LruCacheEntry. It can be used to change implementation of LruCacheEntry

Parameters:
value -
ttl -
Returns:
LruCacheEntry

get

public V get(K key)
Description copied from interface: InterfaceLruCache
Returns value cached with key.

Specified by:
get in interface InterfaceLruCache<K,V>
Returns:
value or null if key doesn't exist or entry is not valid

get

public V get(K key,
             Callable<V> callback)
      throws Exception
Description copied from interface: InterfaceLruCache
Tries to get element from cache. If get fails callback is used to create element and returned value is stored in cache. Default TTL is used

Specified by:
get in interface InterfaceLruCache<K,V>
Returns:
Value
Throws:
Exception - if callback throws exception

get

public V get(K key,
             Callable<V> callback,
             long ttl)
      throws Exception
Description copied from interface: InterfaceLruCache
Tries to get element from cache. If get fails callback is used to create element and returned value is stored in cache

Specified by:
get in interface InterfaceLruCache<K,V>
ttl - time to live in milliseconds
Returns:
Value
Throws:
Exception - if callback throws exception

getTtl

public long getTtl()
Description copied from interface: InterfaceLruCache
Returns cache TTL

Specified by:
getTtl in interface InterfaceLruCache<K,V>
Returns:
ttl in milliseconds

getEntry

protected abstract InterfaceLruCacheEntry<V> getEntry(K key)
Returns LruCacheEntry mapped by key or null if it does not exist

Parameters:
key -
Returns:
LruCacheEntry

getValue

protected V getValue(K key)
Tries to retrieve value by it's key. Automatically removes entry if it's not valid (LruCacheEntry.getValue() returns null)

Parameters:
key -
Returns:
Value

isEmpty

public boolean isEmpty()
Description copied from interface: InterfaceLruCache
Checks whether cache is empty. If any entry exists (including invalid one) this method will return true

Specified by:
isEmpty in interface InterfaceLruCache<K,V>
Returns:
true if no entries are stored in cache

put

public void put(K key,
                V value)
Description copied from interface: InterfaceLruCache
Puts value under key into cache. Default TTL is used

Specified by:
put in interface InterfaceLruCache<K,V>

put

public void put(K key,
                V value,
                long ttl)
Description copied from interface: InterfaceLruCache
Puts value under key into cache with desired TTL

Specified by:
put in interface InterfaceLruCache<K,V>
ttl - time to live in milliseconds

putEntry

protected abstract void putEntry(K key,
                                 InterfaceLruCacheEntry<V> entry)
Puts entry into cache

Parameters:
key -
entry -


Copyright © 2009-2012 Frederic Bregier. All Rights Reserved.