| |
|
| java.lang.Object org.nemesis.forum.util.cache.Cache
Field Summary | |
protected LinkedList | ageList Linked list to maintain time that cache objects were initially added
to the cache, most recently added to oldest added. | protected long | cacheHitscacheMisses Maintain the number of cache hits and misses. | protected HashMap | cachedObjectsHash Maintains the hash of cached objects. | protected static long | currentTime One of the major potential bottlenecks of the cache is performing
System.currentTimeMillis() for every cache get operation. | protected LinkedList | lastAccessedList Linked list to maintain order that cache objects are accessed
in, most used to least used. | protected long | maxLifetime Maximum length of time objects can exist in cache before expiring. | protected int | maxSize Maximum size in bytes that the cache can grow to. | protected int | size Maintains the current size of the cache in bytes. | protected static CacheTimer | timer A cache timer updates the current time once a second in a seperate
thread. |
Constructor Summary | |
public | Cache() Create a new cache with default values. | public | Cache(int maxSize) Create a new cache and specify the maximum size for the cache in bytes. | public | Cache(long maxLifetime) Create a new cache and specify the maximum lifetime of objects. | public | Cache(int maxSize, long maxLifetime) Create a new cache and specify the maximum size of for the cache in
bytes, and the maximum lifetime of objects. |
Method Summary | |
public synchronized void | add(Object key, Cacheable object) Adds a new Cacheable object to the cache. | public synchronized void | clear() Clears the cache of all objects. | public synchronized Cacheable | get(Object key) Gets an object from cache. | public long | getCacheHits() Returns the number of cache hits. | public long | getCacheMisses() Returns the number of cache misses. | public int | getMaxSize() Returns the maximum size of the cache in bytes. | public synchronized int | getNumElements() Returns the number of objects in the cache. | public int | getSize() Returns the current size of the cache in bytes. | public synchronized void | remove(Object key) Removes an object from cache. | public void | setMaxSize(int maxSize) Sets the maximum size of the cache in bytes. | public Collection | values() Returns a collection view of the values contained in the cache. |
ageList | protected LinkedList ageList(Code) | | Linked list to maintain time that cache objects were initially added
to the cache, most recently added to oldest added.
|
cacheHitscacheMisses | protected long cacheHitscacheMisses(Code) | | Maintain the number of cache hits and misses. A cache hit occurs every
time the get method is called and the cache contains the requested
object. A cache miss represents the opposite occurence.
Keeping track of cache hits and misses lets one measure how efficient
the cache is; the higher the percentage of hits, the more efficient.
|
cachedObjectsHash | protected HashMap cachedObjectsHash(Code) | | Maintains the hash of cached objects. Hashing provides the best
performance for fast lookups.
|
currentTime | protected static long currentTime(Code) | | One of the major potential bottlenecks of the cache is performing
System.currentTimeMillis() for every cache get operation. Instead,
we maintain a global timestamp that gets updated once a second. This
means that cache expirations can be no more than one second accurate.
|
lastAccessedList | protected LinkedList lastAccessedList(Code) | | Linked list to maintain order that cache objects are accessed
in, most used to least used.
|
maxLifetime | protected long maxLifetime(Code) | | Maximum length of time objects can exist in cache before expiring.
Default is that objects have no maximum lifetime.
|
maxSize | protected int maxSize(Code) | | Maximum size in bytes that the cache can grow to. Default
maximum size is 128 K.
|
size | protected int size(Code) | | Maintains the current size of the cache in bytes.
|
timer | protected static CacheTimer timer(Code) | | A cache timer updates the current time once a second in a seperate
thread.
|
Cache | public Cache()(Code) | | Create a new cache with default values. Default cache size is 128K with
no maximum lifetime.
|
Cache | public Cache(int maxSize)(Code) | | Create a new cache and specify the maximum size for the cache in bytes.
Items added to the cache will have no maximum lifetime.
Parameters: maxSize - the maximum size of the cache in bytes. |
Cache | public Cache(long maxLifetime)(Code) | | Create a new cache and specify the maximum lifetime of objects. The
time should be specified in milleseconds. The minimum lifetime of any
cache object is 1000 milleseconds (1 second). Additionally, cache
expirations have a 1000 millesecond resolution, which means that all
objects are guaranteed to be expired within 1000 milliseconds of their
maximum lifetime.
Parameters: maxLifetime - the maximum amount of time objects can exist incache before being deleted. |
Cache | public Cache(int maxSize, long maxLifetime)(Code) | | Create a new cache and specify the maximum size of for the cache in
bytes, and the maximum lifetime of objects.
Parameters: maxSize - the maximum size of the cache in bytes. Parameters: maxLifetime - the maximum amount of time objects can exist incache before being deleted. |
add | public synchronized void add(Object key, Cacheable object)(Code) | | Adds a new Cacheable object to the cache. The key must be unique.
Parameters: key - a unique key for the object being put into cache. Parameters: object - the Cacheable object to put into cache. |
clear | public synchronized void clear()(Code) | | Clears the cache of all objects. The size of the cache is reset to 0.
|
get | public synchronized Cacheable get(Object key)(Code) | | Gets an object from cache. This method will return null under two
conditions:
- The object referenced by the key was never added to cache.
- The object referenced by the key has expired from cache.
Parameters: key - the unique key of the object to get. the Cacheable object corresponding to unique key. |
getCacheHits | public long getCacheHits()(Code) | | Returns the number of cache hits. A cache hit occurs every
time the get method is called and the cache contains the requested
object.
Keeping track of cache hits and misses lets one measure how efficient
the cache is; the higher the percentage of hits, the more efficient.
the number of cache hits. |
getCacheMisses | public long getCacheMisses()(Code) | | Returns the number of cache misses. A cache miss occurs every
time the get method is called and the cache does not contain the
requested object.
Keeping track of cache hits and misses lets one measure how efficient
the cache is; the higher the percentage of hits, the more efficient.
the number of cache hits. |
getMaxSize | public int getMaxSize()(Code) | | Returns the maximum size of the cache in bytes. If the cache grows too
large, the least frequently used items will automatically be deleted so
that the cache size doesn't exceed the maximum.
the maximum size of the cache in bytes. |
getNumElements | public synchronized int getNumElements()(Code) | | Returns the number of objects in the cache.
the number of objects in the cache. |
getSize | public int getSize()(Code) | | Returns the current size of the cache in bytes.
the size of the cache in bytes. |
remove | public synchronized void remove(Object key)(Code) | | Removes an object from cache.
Parameters: key - the unique key of the object to remove. |
setMaxSize | public void setMaxSize(int maxSize)(Code) | | Sets the maximum size of the cache in bytes. If the cache grows too
large, the least frequently used items will automatically be deleted so
that the cache size doesn't exceed the maximum.
Parameters: maxSize - the maximum size of the cache in bytes. |
values | public Collection values()(Code) | | Returns a collection view of the values contained in the cache.
The Collection is unmodifiable to prevent cache integrity issues.
a Collection of the cache entries. |
|
|
|