Class Cache<T extends Cacheable>
java.lang.Object
org.deltava.util.cache.Cache<T>
- Type Parameters:
T
- the cacheable object type
- Direct Known Subclasses:
AgingCache
,ExpiringCache
,JedisCache
,NullCache
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal void
Adds an entry to the cache.void
addAll
(Collection<? extends T> entries) Adds a number of entries to the cache.protected abstract void
Adds an entry to the cache.final void
Adds a null entry to the cache.protected abstract void
addNullEntry
(Object key) Adds a null entry to the cache.protected void
Automatically resizes the cache in the case of an overflow.void
clear()
Clears the cache.boolean
Returns if the cache contains a particular cache key.abstract T
Retrieves an entry from the cache.getAll
(Collection<?> keys) Returns multiple objects from the cache.long
Returns the total number of cache errors.final long
getHits()
Returns the total number of cache hits.int
Returns the maximum size of the cache.final long
Returns the total number of cache requests.protected void
hit()
Log a cache hit.boolean
isRemote()
Returns if the cache is a remote cache.void
Invalidate a cache entry.protected void
request()
Log a cache request.final void
setMaxSize
(int size) Sets the maximum size of the cache.int
size()
Returns the current size of the cache.
-
Field Details
-
_cache
-
-
Constructor Details
-
Cache
protected Cache(int maxSize) Initializes the cache.- Parameters:
maxSize
- the maximum size of the cache
-
-
Method Details
-
addAll
Adds a number of entries to the cache.- Parameters:
entries
- a Collection of Cacheable entries
-
contains
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
-
size
public int size()Returns the current size of the cache.- Returns:
- the number of entries in the cache
-
getMaxSize
public 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 theirget(Object)
method to keep statistics.- See Also:
-
request
protected void request()Log a cache request. Implementations should call this method from theirget(Object)
method to keep statistics.- See Also:
-
getHits
public final long getHits()Returns the total number of cache hits.- Returns:
- the number of hits
- See Also:
-
getRequests
public final long getRequests()Returns the total number of cache requests.- Returns:
- the number of requests
- See Also:
-
getErrors
public long getErrors()Returns the total number of cache errors.- Returns:
- the number of errors
-
isRemote
public boolean isRemote()Returns if the cache is a remote cache.- Returns:
- TRUE if remote, otherwise FALSE
-
addEntry
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
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
-
addNull
Adds a null entry to the cache.- Parameters:
key
- the entry key
-
get
-
getAll
Returns multiple objects from the cache.- Parameters:
keys
- the cache keys- Returns:
- a Map of results keyed by cache key
-