|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.mycore.common.MCRCache
public class MCRCache
Instances of this class can be used as object cache. Each MCRCache has a certain capacity, the maximum number of objects the cache will hold. When the cache is full and another object is put into the cache, the cache will discard the least recently used object to get place for the new object. The cache will always hold the most recently used objects by updating its internal structure whenever an object is get from the cache or put into the cache. The cache also provides methods for getting the current cache hit rate and fill rate. Like in a hashtable, an MCRCache uses a unique key for each object.
Hashtable| Nested Class Summary | |
|---|---|
(package private) class |
MCRCache.MCRCacheEntry
For each object in the cache, there is one MCRCacheEntry object encapsulating it. |
| Field Summary | |
|---|---|
protected int |
capacity
The maximum number of objects that the cache can hold |
protected long |
gets
The number of requests to get an object from this cache |
protected long |
hits
The number of hits, where a requested object really was in the cache |
protected Hashtable<Object,MCRCache.MCRCacheEntry> |
index
A hashtable for looking up a cached object by a given key |
protected MCRCache.MCRCacheEntry |
lru
The least recently used object * |
protected MCRCache.MCRCacheEntry |
mru
The most recently used object * |
protected int |
size
The number of objects currently stored in the cache |
protected String |
type
Tch type string for the MCRCacheJMXBridge |
| Constructor Summary | |
|---|---|
MCRCache(int capacity,
String type)
Creates a new cache with a given capacity. |
|
| Method Summary | |
|---|---|
void |
clear()
Clears the cache by removing all entries from the cache |
void |
close()
|
Object |
get(Object key)
Returns an object from the cache for the given key, or null if there currently is no object in the cache with this key. |
int |
getCapacity()
Returns the capacity of this cache. |
int |
getCurrentSize()
Returns the number of objects currently cached. |
double |
getFillRate()
Returns the fill rate of this cache. |
double |
getHitRate()
Returns the hit rate of this cache. |
Object |
getIfUpToDate(Object key,
long time)
Returns an object from the cache for the given key, but only if the cache entry is not older than the given timestamp. |
boolean |
isEmpty()
Returns true if this cache is empty. |
boolean |
isFull()
Returns true if this cache is full. |
List<Object> |
keys()
Returns an iterable list of keys to the cached objects. |
static void |
main(String[] args)
A small sample program for testing this class. |
void |
put(Object key,
Object obj)
Puts an object into the cache, storing it under the given key. |
void |
remove(Object key)
Removes an object from the cache for the given key. |
void |
setCapacity(int capacity)
Changes the capacity of this cache. |
String |
toString()
Returns a String containing information about cache capacity, size, current fill rate and hit rate. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected MCRCache.MCRCacheEntry mru
protected MCRCache.MCRCacheEntry lru
protected Hashtable<Object,MCRCache.MCRCacheEntry> index
protected long gets
protected long hits
protected int size
protected int capacity
protected String type
| Constructor Detail |
|---|
public MCRCache(int capacity,
String type)
capacity - the maximum number of objects this cache will holdtype - the type string for MCRCacheJMXBridge| Method Detail |
|---|
public void put(Object key,
Object obj)
key - the non-null key to store the object underobj - the non-null object to be put into the cachepublic void remove(Object key)
key - the key for the object you want to remove from this cachepublic Object get(Object key)
key - the key for the object you want to get from this cache
public Object getIfUpToDate(Object key,
long time)
key - the key for the object you want to get from this cachetime - the timestamp to check that the cache entry is up to date
public int getCurrentSize()
public int getCapacity()
public void setCapacity(int capacity)
capacity - the maximum number of objects this cache will holdpublic boolean isFull()
public boolean isEmpty()
public double getFillRate()
public double getHitRate()
public void clear()
public String toString()
toString in class Objectpublic static void main(String[] args)
public void close()
public List<Object> keys()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||