public class Cache<T>
extends java.lang.Object
Object stored in cache must properly implement hashCode
and equals
.
Note that this class is not synchronized. If multiple threads access a cache concurrently, and at least one of the threads modifies the cache, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the cache.
Constructor and Description |
---|
Cache() |
Modifier and Type | Method and Description |
---|---|
T |
cache(T object)
Puts the given object in the cache if it is not already present.
|
int |
getHits()
Returns the number of cache hits, which is the number of times
cache(T) was called
and returned an object that already existed in the cache. |
int |
getMisses()
Returns the number of cache misses, which is the number of times
cache(T) was called
and returned null (after caching the object), effectively representing the size of the cache. |
public T cache(T object)
If the object is already cached, than the instance that exists in the cached is returned. Otherwise, it is placed in the cache and null is returned.
object
- object to cachepublic int getHits()
cache(T)
was called
and returned an object that already existed in the cache.public int getMisses()
cache(T)
was called
and returned null (after caching the object), effectively representing the size of the cache.