public interface HGCache<Key,Value>
A simple generic, read-only caching interface. An implementation must be initialized
by providing the means to load data that is not in the cache - a
RefResolver
instance. Therefore, there's no put
method, the cache makes the decision if and when to actually keep data in it. Consequently,
the cache is operational only when there is RefResolver
currently in effect.
Otherwise, the get
will simply throw a NullPointerException
when attempting to access it.
Note, however, that there is a remove
to explicitly remove an element from
the cache. This method should generally be called only when the item is being removed
from permanent storage as well.
When and how element are purged from the cache is not mandated by the interface.
NOTE: Implementation are expected to be thread-safe.
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear (i.e.
|
Value |
get(Key key)
Retrieve an element from the cache.
|
Value |
getIfLoaded(Key key)
Retrieve and return an element from the cache if it's already there or
return
null otherwise. |
RefResolver<Key,Value> |
getResolver()
Return the
RefResolver used to load data in the cache. |
boolean |
isLoaded(Key key)
Return
true if the element with the given key is currently in the
cache and false otherwise. |
void |
remove(Key key)
Force removal of an element from the cache.
|
void |
setResolver(RefResolver<Key,Value> resolver)
Set the
RefResolver to be used to load data in the cache. |
int |
size()
Return the number of elements currently in the cache.
|
void setResolver(RefResolver<Key,Value> resolver)
Set the RefResolver
to be used to load data in the cache.
RefResolver<Key,Value> getResolver()
Return the RefResolver
used to load data in the cache.
Value get(Key key)
Retrieve an element from the cache. If the element is already in the cache,
it is simply returned. Otherwise, the RefResolver
will be used to
obtain it automatically from permanent storage.
key
- The key of the element.Value getIfLoaded(Key key)
Retrieve and return an element from the cache if it's already there or
return null
otherwise. This method will not call the
RefResolver
when the element is not found in the cache.
key
- The key of the element.null
is it's not found in the cache.boolean isLoaded(Key key)
Return true
if the element with the given key is currently in the
cache and false
otherwise.
void remove(Key key)
Force removal of an element from the cache. This method is generally used when the data has been (or is being) removed from the permanent storage as well.
key
- The key of the element.void clear()
Clear (i.e. force removal of) all elements from the cache.
int size()
Return the number of elements currently in the cache.
Copyright © 2015. All rights reserved.