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

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

public class SynchronizedLruCache<K,V>
extends AbstractLruCache<K,V>

Threadsafe synchronized implementation of LruCache based on LinkedHashMap. Threadsafety is provided by method synchronization. This cache implementation should be used with low number of threads.

Author:
Frederic Bregier, Damian Momot

Field Summary
static int DEFAULT_INITIAL_CAPACITY
           
static float DEFAULT_LOAD_FACTOR
           
 
Constructor Summary
SynchronizedLruCache(int capacity, long ttl)
          Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR and DEFAULT_INITIAL_CAPACITY
SynchronizedLruCache(int capacity, long ttl, int initialCapacity)
          Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR
SynchronizedLruCache(int capacity, long ttl, int initialCapacity, float loadFactor)
          Creates new SynchronizedLruCache
 
Method Summary
 void clear()
          Removes all entries from cache
 void forceClearOldest()
          Removes all oldest entries from cache (ttl based)
 V get(K key)
          Returns value cached with key.
 int getCapacity()
          Returns cache capacity
protected  InterfaceLruCacheEntry<V> getEntry(K key)
          Returns LruCacheEntry mapped by key or null if it does not exist
 int getSize()
          Returns number of entries stored in cache (including invalid ones)
 void put(K key, V value, long ttl)
          Puts value under key into cache with desired TTL
protected  void putEntry(K key, InterfaceLruCacheEntry<V> entry)
          Puts entry into cache
 void remove(K key)
          Removes entry from cache (if exists)
 
Methods inherited from class goldengate.common.lru.AbstractLruCache
contains, createEntry, get, get, getTtl, getValue, isEmpty, put
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_INITIAL_CAPACITY

public static final int DEFAULT_INITIAL_CAPACITY
See Also:
Constant Field Values

DEFAULT_LOAD_FACTOR

public static final float DEFAULT_LOAD_FACTOR
See Also:
Constant Field Values
Constructor Detail

SynchronizedLruCache

public SynchronizedLruCache(int capacity,
                            long ttl,
                            int initialCapacity,
                            float loadFactor)
Creates new SynchronizedLruCache

Parameters:
capacity - max cache capacity
ttl - time to live in milliseconds
initialCapacity - initial cache capacity
loadFactor -

SynchronizedLruCache

public SynchronizedLruCache(int capacity,
                            long ttl,
                            int initialCapacity)
Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR

Parameters:
capacity - max cache capacity
ttl - time to live in milliseconds
initialCapacity - initial cache capacity

SynchronizedLruCache

public SynchronizedLruCache(int capacity,
                            long ttl)
Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR and DEFAULT_INITIAL_CAPACITY

Parameters:
capacity - max cache capacity
ttl - time to live in milliseconds
Method Detail

clear

public void clear()
Description copied from interface: InterfaceLruCache
Removes all entries from cache


get

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

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

getCapacity

public int getCapacity()
Description copied from interface: InterfaceLruCache
Returns cache capacity

Returns:
capacity of cache

getEntry

protected InterfaceLruCacheEntry<V> getEntry(K key)
Description copied from class: AbstractLruCache
Returns LruCacheEntry mapped by key or null if it does not exist

Specified by:
getEntry in class AbstractLruCache<K,V>
Returns:
LruCacheEntry

getSize

public int getSize()
Description copied from interface: InterfaceLruCache
Returns number of entries stored in cache (including invalid ones)

Returns:
number of entries

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>
Overrides:
put in class AbstractLruCache<K,V>
ttl - time to live in milliseconds

putEntry

protected void putEntry(K key,
                        InterfaceLruCacheEntry<V> entry)
Description copied from class: AbstractLruCache
Puts entry into cache

Specified by:
putEntry in class AbstractLruCache<K,V>

remove

public void remove(K key)
Description copied from interface: InterfaceLruCache
Removes entry from cache (if exists)


forceClearOldest

public void forceClearOldest()
Description copied from interface: InterfaceLruCache
Removes all oldest entries from cache (ttl based)



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