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 voidAdds an entry to the cache.voidaddAll(Collection<? extends T> entries) Adds a number of entries to the cache.protected abstract voidAdds an entry to the cache.final voidAdds a null entry to the cache.protected abstract voidaddNullEntry(Object key) Adds a null entry to the cache.protected voidAutomatically resizes the cache in the case of an overflow.voidclear()Clears the cache.booleanReturns if the cache contains a particular cache key.abstract TRetrieves an entry from the cache.getAll(Collection<?> keys) Returns multiple objects from the cache.longReturns the total number of cache errors.final longgetHits()Returns the total number of cache hits.intReturns the maximum size of the cache.final longReturns the total number of cache requests.protected voidhit()Log a cache hit.booleanisRemote()Returns if the cache is a remote cache.voidInvalidate a cache entry.protected voidrequest()Log a cache request.final voidsetMaxSize(int size) Sets the maximum size of the cache.intsize()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
-