@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2002 - 2017 the original author or authors .
* Copyright 2002 - 2019 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -55,6 +55,7 @@ public interface Cache {
* a cached { @code null } value . A straight { @code null } being
* a cached { @code null } value . A straight { @code null } being
* returned means that the cache contains no mapping for this key .
* returned means that the cache contains no mapping for this key .
* @see # get ( Object , Class )
* @see # get ( Object , Class )
* @see # get ( Object , Callable )
* /
* /
@Nullable
@Nullable
ValueWrapper get ( Object key ) ;
ValueWrapper get ( Object key ) ;
@ -94,6 +95,7 @@ public interface Cache {
* @return the value to which this cache maps the specified key
* @return the value to which this cache maps the specified key
* @throws ValueRetrievalException if the { @code valueLoader } throws an exception
* @throws ValueRetrievalException if the { @code valueLoader } throws an exception
* @since 4 . 3
* @since 4 . 3
* @see # get ( Object )
* /
* /
@Nullable
@Nullable
< T > T get ( Object key , Callable < T > valueLoader ) ;
< T > T get ( Object key , Callable < T > valueLoader ) ;
@ -102,8 +104,13 @@ public interface Cache {
* Associate the specified value with the specified key in this cache .
* Associate the specified value with the specified key in this cache .
* < p > If the cache previously contained a mapping for this key , the old
* < p > If the cache previously contained a mapping for this key , the old
* value is replaced by the specified value .
* value is replaced by the specified value .
* < p > Actual registration may be performed in an asynchronous or deferred
* fashion , with subsequent lookups possibly not seeing the entry yet .
* This may for example be the case with transactional cache decorators .
* Use { @link # putIfAbsent } for guaranteed immediate registration .
* @param key the key with which the specified value is to be associated
* @param key the key with which the specified value is to be associated
* @param value the value to be associated with the specified key
* @param value the value to be associated with the specified key
* @see # putIfAbsent ( Object , Object )
* /
* /
void put ( Object key , @Nullable Object value ) ;
void put ( Object key , @Nullable Object value ) ;
@ -112,19 +119,19 @@ public interface Cache {
* if it is not set already .
* if it is not set already .
* < p > This is equivalent to :
* < p > This is equivalent to :
* < pre > < code >
* < pre > < code >
* Object existingValue = cache . get ( key ) ;
* ValueWrapper existingValue = cache . get ( key ) ;
* if ( existingValue = = null ) {
* if ( existingValue = = null ) {
* cache . put ( key , value ) ;
* cache . put ( key , value ) ;
* return null ;
* } else {
* return existingValue ;
* }
* }
* return existingValue ;
* < / code > < / pre >
* < / code > < / pre >
* except that the action is performed atomically . While all out - of - the - box
* except that the action is performed atomically . While all out - of - the - box
* { @link CacheManager } implementations are able to perform the put atomically ,
* { @link CacheManager } implementations are able to perform the put atomically ,
* the operation may also be implemented in two steps , e . g . with a check for
* the operation may also be implemented in two steps , e . g . with a check for
* presence and a subsequent put , in a non - atomic way . Check the documentation
* presence and a subsequent put , in a non - atomic way . Check the documentation
* of the native cache implementation that you are using for more details .
* of the native cache implementation that you are using for more details .
* < p > The default implementation delegates to { @link # get ( Object ) } and
* { @link # put ( Object , Object ) } along the lines of the code snippet above .
* @param key the key with which the specified value is to be associated
* @param key the key with which the specified value is to be associated
* @param value the value to be associated with the specified key
* @param value the value to be associated with the specified key
* @return the value to which this cache maps the specified key ( which may be
* @return the value to which this cache maps the specified key ( which may be
@ -132,21 +139,73 @@ public interface Cache {
* mapping for that key prior to this call . Returning { @code null } is therefore
* mapping for that key prior to this call . Returning { @code null } is therefore
* an indicator that the given { @code value } has been associated with the key .
* an indicator that the given { @code value } has been associated with the key .
* @since 4 . 1
* @since 4 . 1
* @see # put ( Object , Object )
* /
* /
@Nullable
@Nullable
ValueWrapper putIfAbsent ( Object key , @Nullable Object value ) ;
default ValueWrapper putIfAbsent ( Object key , @Nullable Object value ) {
ValueWrapper existingValue = get ( key ) ;
if ( existingValue = = null ) {
put ( key , value ) ;
}
return existingValue ;
}
/ * *
/ * *
* Evict the mapping for this key from this cache if it is present .
* Evict the mapping for this key from this cache if it is present .
* < p > Actual eviction may be performed in an asynchronous or deferred
* fashion , with subsequent lookups possibly still seeing the entry .
* This may for example be the case with transactional cache decorators .
* Use { @link # evictIfPresent } for guaranteed immediate removal .
* @param key the key whose mapping is to be removed from the cache
* @param key the key whose mapping is to be removed from the cache
* @see # evictIfPresent ( Object )
* /
* /
void evict ( Object key ) ;
void evict ( Object key ) ;
/ * *
/ * *
* Remove all mappings from the cache .
* Evict the mapping for this key from this cache if it is present ,
* expecting the key to be immediately invisible for subsequent lookups .
* < p > The default implementation delegates to { @link # evict ( Object ) } ,
* returning { @code false } for not - determined prior presence of the key .
* Cache providers and in particular cache decorators are encouraged
* to perform immediate eviction if possible ( e . g . in case of generally
* deferred cache operations within a transaction ) and to reliably
* determine prior presence of the given key .
* @param key the key whose mapping is to be removed from the cache
* @return { @code true } if the cache was known to have a mapping for
* this key before , { @code false } if it did not ( or if prior presence
* could not be determined )
* @since 5 . 2
* @see # evict ( Object )
* /
default boolean evictIfPresent ( Object key ) {
evict ( key ) ;
return false ;
}
/ * *
* Clear the cache through removing all mappings .
* < p > Actual clearing may be performed in an asynchronous or deferred
* fashion , with subsequent lookups possibly still seeing the entries .
* This may for example be the case with transactional cache decorators .
* Use { @link # invalidate ( ) } for guaranteed immediate removal of entries .
* @see # invalidate ( )
* /
* /
void clear ( ) ;
void clear ( ) ;
/ * *
* Invalidate the cache through removing all mappings , expecting all
* entries to be immediately invisible for subsequent lookups .
* @return { @code true } if the cache was known to have mappings before ,
* { @code false } if it did not ( or if prior presence of entries could
* not be determined )
* @since 5 . 2
* @see # clear ( )
* /
default boolean invalidate ( ) {
clear ( ) ;
return false ;
}
/ * *
/ * *
* A ( wrapper ) object representing a cache value .
* A ( wrapper ) object representing a cache value .