org.apache.jackrabbit.core.cache
Class ConcurrentCache<K,V>

java.lang.Object
  extended by org.apache.jackrabbit.core.cache.AbstractCache
      extended by org.apache.jackrabbit.core.cache.ConcurrentCache<K,V>
All Implemented Interfaces:
Cache

public class ConcurrentCache<K,V>
extends AbstractCache

Concurrent cache implementation that uses cache segments to minimize the chance of lock contention. The LRU algorithm is used to evict excess entries from each cache segment separately, which makes the combined eviction algorithm similar but not exactly the same as LRU. None of the methods of this class are synchronized, but they are all thread-safe.


Constructor Summary
ConcurrentCache()
           
ConcurrentCache(int numberOfSegments)
           
 
Method Summary
 void clear()
          Clears all segments of the cache.
 boolean containsKey(K key)
          Checks if the identified entry is cached.
 V get(K key)
          Returns the identified cache entry.
 boolean isEmpty()
          Checks if the cache size is zore.
 V put(K key, V value, long size)
          Adds the given entry to the cache.
 V remove(K key)
          Removes the identified entry from the cache.
 void setMaxMemorySize(long size)
          Sets the maximum size of the cache and evicts any excess items until the current size falls within the given limit.
 V[] values()
          Returns all values in the cache.
 
Methods inherited from class org.apache.jackrabbit.core.cache.AbstractCache
dispose, getAccessCount, getMaxMemorySize, getMemoryUsed, isTooBig, recordCacheAccess, recordSizeChange, resetAccessCount, setAccessListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConcurrentCache

public ConcurrentCache(int numberOfSegments)

ConcurrentCache

public ConcurrentCache()
Method Detail

containsKey

public boolean containsKey(K key)
Checks if the identified entry is cached.

Parameters:
key - entry key
Returns:
true if the entry is cached, false otherwise

get

public V get(K key)
Returns the identified cache entry.

Parameters:
key - entry key
Returns:
entry value, or null if not found

values

public V[] values()
Returns all values in the cache. Note that this method is not synchronized over the entire cache, so it is only guaranteed to return accurate results when there are no concurrent threads modifying the cache.

Returns:
cached values

put

public V put(K key,
             V value,
             long size)
Adds the given entry to the cache.

Parameters:
key - entry key
value - entry value
size - entry size
Returns:
the previous value, or null

remove

public V remove(K key)
Removes the identified entry from the cache.

Parameters:
key - entry key
Returns:
removed entry, or null if not found

clear

public void clear()
Clears all segments of the cache. Note that even this method is not synchronized over the entire cache, so it needs to explicitly count the cache size changes and may return with a non-empty cache if other threads have concurrently been adding new entries.


isEmpty

public boolean isEmpty()
Checks if the cache size is zore.


setMaxMemorySize

public void setMaxMemorySize(long size)
Sets the maximum size of the cache and evicts any excess items until the current size falls within the given limit.

Specified by:
setMaxMemorySize in interface Cache
Overrides:
setMaxMemorySize in class AbstractCache
Parameters:
size - the size in bytes


Copyright © 2004-2010 The Apache Software Foundation. All Rights Reserved.