org.neo4j.kernel.impl.cache
Class LruCache<K,E>

java.lang.Object
  extended by org.neo4j.kernel.impl.cache.LruCache<K,E>
All Implemented Interfaces:
Cache<K,E>

public class LruCache<K,E>
extends Object
implements Cache<K,E>

Simple implementation of Least-recently-used cache. The cache has a maxSize set and when the number of cached elements exceeds that limit the least recently used element will be removed.


Constructor Summary
LruCache(String name, int maxSize, AdaptiveCacheManager cacheManager)
          Creates a LRU cache.
 
Method Summary
 void clear()
          Removing all cached elements.
 void elementCleaned(E element)
           
 E get(K key)
          Returns the cached element for key.
 String getName()
          Returns the name of the cache.
 long hitCount()
           
 boolean isAdaptive()
           
 int maxSize()
          Returns the maximum size of this cache.
 long missCount()
           
 void put(K key, E element)
          Adds element to cache.
 void putAll(Map<K,E> map)
           
 E remove(K key)
          Removes the element for key from cache and returns it.
 void resize(int newMaxSize)
          Changes the max size of the cache.
 void setAdaptiveStatus(boolean status)
           
 int size()
          Returns the cache size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LruCache

public LruCache(String name,
                int maxSize,
                AdaptiveCacheManager cacheManager)
Creates a LRU cache. If maxSize < 1 an IllegalArgumentException is thrown.

Parameters:
name - name of cache
maxSize - maximum size of this cache
cacheManager - adaptive cache manager or null if adaptive caching not needed
Method Detail

getName

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

Specified by:
getName in interface Cache<K,E>
Returns:
name of the cache

put

public void put(K key,
                E element)
Description copied from interface: Cache
Adds element to cache.

Specified by:
put in interface Cache<K,E>
Parameters:
key - the key for the element

remove

public E remove(K key)
Description copied from interface: Cache
Removes the element for key from cache and returns it. If the no element for key exists null is returned.

Specified by:
remove in interface Cache<K,E>
Parameters:
key - the key for the element
Returns:
the removed element or null if element didn't exist

get

public E get(K key)
Description copied from interface: Cache
Returns the cached element for key. If the element isn't in cache null is returned.

Specified by:
get in interface Cache<K,E>
Parameters:
key - the key for the element
Returns:
the cached element or null if element didn't exist

clear

public void clear()
Description copied from interface: Cache
Removing all cached elements.

Specified by:
clear in interface Cache<K,E>

size

public int size()
Description copied from interface: Cache
Returns the cache size.

Specified by:
size in interface Cache<K,E>
Returns:
cache size

maxSize

public int maxSize()
Returns the maximum size of this cache.

Specified by:
maxSize in interface Cache<K,E>
Returns:
maximum size

resize

public void resize(int newMaxSize)
Changes the max size of the cache. If newMaxSize is greater then maxSize() next invoke to maxSize() will return newMaxSize and the entries in cache will not be modified.

If newMaxSize is less then size() the cache will shrink itself removing least recently used element until size() equals newMaxSize. For each element removed the elementCleaned(E) method is invoked.

If newMaxSize is less then 1 an IllegalArgumentException is thrown.

Specified by:
resize in interface Cache<K,E>
Parameters:
newMaxSize - the new maximum size of the cache

elementCleaned

public void elementCleaned(E element)
Specified by:
elementCleaned in interface Cache<K,E>

isAdaptive

public boolean isAdaptive()
Specified by:
isAdaptive in interface Cache<K,E>

setAdaptiveStatus

public void setAdaptiveStatus(boolean status)
Specified by:
setAdaptiveStatus in interface Cache<K,E>

putAll

public void putAll(Map<K,E> map)
Specified by:
putAll in interface Cache<K,E>

hitCount

public long hitCount()
Specified by:
hitCount in interface Cache<K,E>

missCount

public long missCount()
Specified by:
missCount in interface Cache<K,E>


Copyright © 2002-2012 The Neo4j Graph Database Project. All Rights Reserved.