com.thimbleware.jmemcached
Class LRUCache<ID_TYPE,ITEM_TYPE>

java.lang.Object
  extended by com.thimbleware.jmemcached.LRUCache<ID_TYPE,ITEM_TYPE>

public final class LRUCache<ID_TYPE,ITEM_TYPE>
extends java.lang.Object

Simple non-thread-safe LRU hash map cache. Thread safety is expected to be provided by the caller.


Constructor Summary
LRUCache(int maximumItems, long maximumSize, long ceilingSize)
          Caches are created as empty, and populated through use.
 
Method Summary
 void clear()
          Start from beginning, and remove all items from the cache; if cache is disabled, do nothing.
 long count()
           
 ITEM_TYPE get(ID_TYPE aId)
          Retrieve an existing item from the cache.
 long getCeilingSize()
           
 int getMaximumItems()
           
 long getMaximumSize()
           
 long getSize()
           
 boolean has(ID_TYPE aId)
          Return true only if the corresponding item is in the cache, and has been in it for no more that fRefreshInterval milliseconds; if caching is disabled, then always return false.
 java.util.Set<ID_TYPE> keys()
           
 void put(ID_TYPE aId, ITEM_TYPE aItem, long item_size)
          If the item is already present, then replace it; otherwise, add it.
 void remove(ID_TYPE key)
          Remove an entry from the cache
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LRUCache

public LRUCache(int maximumItems,
                long maximumSize,
                long ceilingSize)
Caches are created as empty, and populated through use.

Parameters:
maximumItems - maximum number of items allowed in the cache
maximumSize - maximum size in bytes of the cache
ceilingSize - number of bytes to attempt to leave as ceiling room
Method Detail

has

public boolean has(ID_TYPE aId)
Return true only if the corresponding item is in the cache, and has been in it for no more that fRefreshInterval milliseconds; if caching is disabled, then always return false.

Parameters:
aId - is non-null.
Returns:
if the corresponding item is in the cache
Throws:
java.lang.IllegalArgumentException - if a param does not comply.

get

public ITEM_TYPE get(ID_TYPE aId)
Retrieve an existing item from the cache.

Parameters:
aId - is non-null, and corresponds to an existing item in the cache.
Returns:
a non-null Object
Throws:
java.lang.IllegalArgumentException - if aId is null, or if the item is not in the cache
java.lang.IllegalStateException - if the item in the cache is null or the cache is disabled

put

public void put(ID_TYPE aId,
                ITEM_TYPE aItem,
                long item_size)
If the item is already present, then replace it; otherwise, add it.

If the cache is disabled, do nothing.

Parameters:
aId - is non-null
aItem - is non-null
item_size - is the size of aItem in bytes
Throws:
java.lang.IllegalArgumentException - if param does not comply

remove

public void remove(ID_TYPE key)
Remove an entry from the cache

Parameters:
key - the key for the entry

clear

public void clear()
Start from beginning, and remove all items from the cache; if cache is disabled, do nothing.

Forces a re-population of all items into the cache.


keys

public java.util.Set<ID_TYPE> keys()
Returns:
the set of all keys in the cache

count

public long count()
Returns:
the number of entries in the cache

getMaximumItems

public int getMaximumItems()
Returns:
the maximum number of items in the cache

getSize

public long getSize()
Returns:
the size (in bytes) of all entries in the cache.

getMaximumSize

public long getMaximumSize()
Returns:
the maximum capacity (in bytes) of the cache

getCeilingSize

public long getCeilingSize()
Returns:
the reserved headroom/ceiling size (in bytes)


Copyright © 2008. All Rights Reserved.