V - type of the objects stored in the cache.public interface CacheProvider<V>
Usually a caching system returns the value related to a given key
if it is present and still valid and null if it is not
present or expired. The implementations of this interface need
to return null if the given key is not present and an
object of type CacheEntry otherwise. The CacheEntry
need to be returned even if CacheEntry.isExpired() is true.
The concept behind this choice is to perform the following behavior:
touch(CacheKey,long)) then the value
will be updated.
The best practice to perform this point is to set the expected expiration
time in the CacheEntry while the actual cache will expire later.
If more threads are requesting the same key and the key is expired, only the
first thread invoking touch(CacheKey,long) will
succeed, for the other threads the invocation will fail.
This way the first thread will update the value related to the key
while the other threads will consider the cached value as still valid.
If touch(CacheKey,long) is called for a key
not present in the cache a CacheProviderException will be thrown.
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all elements from the cache.
|
CacheEntry<V> |
get(CacheKey key)
Returns the cache entry related to the given key.
|
void |
put(CacheKey key,
V value,
long duration)
Inserts the given key and the related value into the cache.
|
void |
remove(CacheKey key)
Removes the given key from the cache.
|
boolean |
touch(CacheKey key,
long duration)
Defers the expiration time of the entry related to the given key.
|
CacheEntry<V> get(CacheKey key)
If the given key is never been cached null will be returned,
otherwise an object of type CacheEntry will be returned
containing the related value and expiration time.
The value in the CacheEntry can be null.
key - the key to search for.null otherwise.CacheProviderException - if an error occurs during the operation.void put(CacheKey key, V value, long duration)
If the given key is already present, the related CacheEntry
will be replaced.
The duration is expressed in milliseconds. If the duration is 0
or less nothing will be done.
key - key to be cached.value - value to be cached.duration - the amount of time the value will last in cache.CacheProviderException - if an error occurs during the operation.boolean touch(CacheKey key, long duration)
If the key is not present a CacheProviderException
will be thrown.
The duration is expressed in milliseconds. If the duration is 0
or less nothing will be done.
key - the key to search for.duration - number of milliseconds until expiration.true if the operation was successful, @code false} otherwise.CacheProviderException - if an error occurs during the operation.void remove(CacheKey key)
If the key is not present nothing will be done.
key - the key to be removed.CacheProviderException - if an error occurs during the operation.void clear()
This method will clear the underlying cache.
CacheProviderException - if an error occurs during the operation.Copyright © 2011–2020 Nerd4j. All rights reserved.