org.deltava.util.cache
Class Cache<T extends Cacheable>

java.lang.Object
  extended by org.deltava.util.cache.Cache<T>
Direct Known Subclasses:
AgingCache, ExpiringCache, FileCache

public abstract class Cache<T extends Cacheable>
extends Object

An an abstract class to store common cache operations. These caches can store null entries to prevent repeated uncached calls for invalid keys.

Since:
1.0
Version:
3.1
Author:
Luke

Field Summary
protected  ConcurrentMap<Object,CacheEntry<T>> _cache
           
 
Constructor Summary
protected Cache(int maxSize)
          Initializes the cache.
 
Method Summary
 void add(T entry)
          Adds an entry to the cache.
 void addAll(Collection<? extends T> entries)
          Adds a number of entries to the cache.
protected abstract  void addEntry(T entry)
          Adds an entry to the cache.
 void addNull(String key)
          Adds a null entry to the cache.
protected abstract  void addNullEntry(Object key)
          Adds a null entry to the cache.
protected  void checkOverflow()
          Automatically resizes the cache in the case of an overflow.
 void clear()
          Clears the cache.
 boolean contains(Object key)
          Returns if the cache contains a particular cache key.
abstract  T get(Object key)
          Retrieves an entry from the cache.
 long getHits()
          Returns the total number of cache hits.
 int getMaxSize()
          Returns the maximum size of the cache.
 long getRequests()
          Returns the total number of cache requests
protected  void hit()
          Log a cache hit.
 void remove(Object key)
          Invalidate a cache entry.
protected  void request()
          Log a cache request.
 void setMaxSize(int size)
          Sets the maximum size of the cache.
 int size()
          Returns the current size of the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_cache

protected ConcurrentMap<Object,CacheEntry<T extends Cacheable>> _cache
Constructor Detail

Cache

protected Cache(int maxSize)
Initializes the cache.

Parameters:
maxSize - the maximum size of the cache
Method Detail

addAll

public void addAll(Collection<? extends T> entries)
Adds a number of entries to the cache.

Parameters:
entries - a Collection of Cacheable entries

contains

public boolean contains(Object key)
Returns if the cache contains a particular cache key.

Parameters:
key - the cache key
Returns:
TRUE if the cache contains the key, otherwise FALSE

clear

public void clear()
Clears the cache.


remove

public void remove(Object key)
Invalidate a cache entry.

Parameters:
key - the entry key

size

public final int size()
Returns the current size of the cache.

Returns:
the number of entries in the cache

getMaxSize

public final int getMaxSize()
Returns the maximum size of the cache.

Returns:
the maximum number of entries in the cache

setMaxSize

public final void setMaxSize(int size)
Sets the maximum size of the cache.

Parameters:
size - the maximum number of entries

checkOverflow

protected void checkOverflow()
Automatically resizes the cache in the case of an overflow. This is done by sorting the cache entries using their natural order, and removing the first entry.


hit

protected void hit()
Log a cache hit. Implementations should call this method from their get(Object) method to keep statistics.

See Also:
request(), getHits(), getRequests()

request

protected void request()
Log a cache request. Implementations should call this method from their get(Object) method to keep statistics.

See Also:
hit(), getRequests()

getHits

public final long getHits()
Returns the total number of cache hits.

Returns:
the number of hits
See Also:
getRequests()

getRequests

public final long getRequests()
Returns the total number of cache requests

Returns:
the number of requests
See Also:
getHits()

addEntry

protected abstract void addEntry(T entry)
Adds an entry to the cache. Subclasses need to implement this method to handle cache additions, the public method forces an overflow check.

Parameters:
entry - the entry to add

addNullEntry

protected abstract void addNullEntry(Object key)
Adds a null entry to the cache. Subclasses need to implement this method to handle cache additions, the public method forces an overflow check.

Parameters:
key - the entry key

add

public final void add(T entry)
Adds an entry to the cache.

Parameters:
entry - the entry to add

addNull

public final void addNull(String key)
Adds a null entry to the cache.

Parameters:
key - the entry key

get

public abstract T get(Object key)
Retrieves an entry from the cache.

Parameters:
key - the cache key
Returns:
the cache entry, or null if the key is not present or the entry is invalid


Copyright © 2004-2009 Global Virtual Airlines Group. All Rights Reserved.