Top Description Inners Fields Constructors Methods
org.springframework.cache.jcache

public Class JCacheCache

extends AbstractValueAdaptingCache
Class Inheritance
Imports
java.util.concurrent.Callable, java.util.function.Function, javax.cache.Cache, javax.cache.processor.EntryProcessor, .EntryProcessorException, .MutableEntry, org.springframework.cache.support.AbstractValueAdaptingCache, org.springframework.lang.Nullable, org.springframework.util.Assert

org.springframework.cache.Cache implementation on top of a javax.cache.Cache instance.

Note

This class has been updated for JCache 1.0, as of Spring 4.0.

Authors
Juergen Hoeller, Stephane Nicoll
Since
3.2
See Also
JCacheCacheManager

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
private final Cache<Object, Object>
private final JCacheCache.ValueLoaderEntryProcessor

Constructor Summary

AccessConstructor and Description
public
JCacheCache(Cache<Object, Object>
backing JCache Cache instance
jcache
)

Create a JCacheCache instance.

public
JCacheCache(Cache<Object, Object>
backing JCache Cache instance
jcache
,
boolean
whether to accept and convert null values for this cache
allowNullValues
)

Create a JCacheCache instance.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Implements org.springframework.cache.Cache.clear.

Clear the cache through removing all mappings.

public void
evict(Object
the key whose mapping is to be removed from the cache
key
)

Implements org.springframework.cache.Cache.evict.

Evict the mapping for this key from this cache if it is present.

public boolean
evictIfPresent(Object
the key whose mapping is to be removed from the cache
key
)

Overrides default org.springframework.cache.Cache.evictIfPresent.

Evict the mapping for this key from this cache if it is present, expecting the key to be immediately invisible for subsequent lookups.

public <T> T
get(Object
the key whose associated value is to be returned
key
,
Callable<T> valueLoader)

Implements org.springframework.cache.Cache.get.

Return the value to which this cache maps the specified key, obtaining that value from valueLoader if necessary.

public final String
getName()

Implements org.springframework.cache.Cache.getName.

Return the cache name.

public final Cache<Object, Object>
getNativeCache()

Implements org.springframework.cache.Cache.getNativeCache.

Return the underlying native cache provider.

public boolean
invalidate()

Overrides default org.springframework.cache.Cache.invalidate.

Invalidate the cache through removing all mappings, expecting all entries to be immediately invisible for subsequent lookups.

protected Object
lookup(Object
the key whose associated value is to be returned
key
)

Implements abstract org.springframework.cache.support.AbstractValueAdaptingCache.lookup.

Perform an actual lookup in the underlying store.

public void
put(Object
the key with which the specified value is to be associated
key
,
Object
the value to be associated with the specified key
value
)

Implements org.springframework.cache.Cache.put.

Associate the specified value with the specified key in this cache.

public Cache.ValueWrapper
putIfAbsent(Object
the key with which the specified value is to be associated
key
,
Object
the value to be associated with the specified key
value
)

Overrides default org.springframework.cache.Cache.putIfAbsent.

Atomically associate the specified value with the specified key in this cache if it is not set already.

Inherited from org.springframework.cache.support.AbstractValueAdaptingCache:
fromStoreValuegetgetisAllowNullValuestoStoreValuetoValueWrapper

Field Detail

cacheback to summary
private final Cache<Object, Object> cache
valueLoaderEntryProcessorback to summary
private final JCacheCache.ValueLoaderEntryProcessor valueLoaderEntryProcessor

Constructor Detail

JCacheCacheback to summary
public JCacheCache(Cache<Object, Object> jcache)

Create a JCacheCache instance.

Parameters
jcache:Cache<Object, Object>

backing JCache Cache instance

JCacheCacheback to summary
public JCacheCache(Cache<Object, Object> jcache, boolean allowNullValues)

Create a JCacheCache instance.

Parameters
jcache:Cache<Object, Object>

backing JCache Cache instance

allowNullValues:boolean

whether to accept and convert null values for this cache

Method Detail

clearback to summary
public void clear()

Implements org.springframework.cache.Cache.clear.

Doc from org.springframework.cache.Cache.clear.

Clear the cache through removing all mappings.

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 invalidate() for guaranteed immediate removal of entries.

If the cache is supposed to be compatible with CompletableFuture and reactive interactions, the clear operation needs to be effectively non-blocking, with any backend write-through happening asynchronously. This goes along with a cache implemented and configured to support retrieve(Object) and retrieve(Object, Supplier).

Annotations
@Override
evictback to summary
public void evict(Object key)

Implements org.springframework.cache.Cache.evict.

Doc from org.springframework.cache.Cache.evict.

Evict the mapping for this key from this cache if it is present.

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 evictIfPresent for guaranteed immediate removal.

If the cache is supposed to be compatible with CompletableFuture and reactive interactions, the evict operation needs to be effectively non-blocking, with any backend write-through happening asynchronously. This goes along with a cache implemented and configured to support retrieve(Object) and retrieve(Object, Supplier).

Parameters
key:Object

the key whose mapping is to be removed from the cache

Annotations
@Override
evictIfPresentback to summary
public boolean evictIfPresent(Object key)

Overrides default org.springframework.cache.Cache.evictIfPresent.

Doc from org.springframework.cache.Cache.evictIfPresent.

Evict the mapping for this key from this cache if it is present, expecting the key to be immediately invisible for subsequent lookups.

The default implementation delegates to evict(Object), returning 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.

Parameters
key:Object

the key whose mapping is to be removed from the cache

Returns:boolean

true if the cache was known to have a mapping for this key before, false if it did not (or if prior presence could not be determined)

Annotations
@Override
getback to summary
public <T> T get(Object key, Callable<T> valueLoader)

Implements org.springframework.cache.Cache.get.

Doc from org.springframework.cache.Cache.get.

Return the value to which this cache maps the specified key, obtaining that value from valueLoader if necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache and return" pattern.

If possible, implementations should ensure that the loading operation is synchronized so that the specified valueLoader is only called once in case of concurrent access on the same key.

If the valueLoader throws an exception, it is wrapped in a ValueRetrievalException

Parameters
key:Object

the key whose associated value is to be returned

Returns:T

the value to which this cache maps the specified key

Annotations
@Override
@Nullable
@SuppressWarnings:unchecked
getNameback to summary
public final String getName()

Implements org.springframework.cache.Cache.getName.

Doc from org.springframework.cache.Cache.getName.

Return the cache name.

Annotations
@Override
getNativeCacheback to summary
public final Cache<Object, Object> getNativeCache()

Implements org.springframework.cache.Cache.getNativeCache.

Doc from org.springframework.cache.Cache.getNativeCache.

Return the underlying native cache provider.

Annotations
@Override
invalidateback to summary
public boolean invalidate()

Overrides default org.springframework.cache.Cache.invalidate.

Doc from org.springframework.cache.Cache.invalidate.

Invalidate the cache through removing all mappings, expecting all entries to be immediately invisible for subsequent lookups.

Returns:boolean

true if the cache was known to have mappings before, false if it did not (or if prior presence of entries could not be determined)

Annotations
@Override
lookupback to summary
protected Object lookup(Object key)

Implements abstract org.springframework.cache.support.AbstractValueAdaptingCache.lookup.

Doc from org.springframework.cache.support.AbstractValueAdaptingCache.lookup.

Perform an actual lookup in the underlying store.

Parameters
key:Object

the key whose associated value is to be returned

Returns:Object

the raw store value for the key, or null if none

Annotations
@Override
@Nullable
putback to summary
public void put(Object key, Object value)

Implements org.springframework.cache.Cache.put.

Doc from org.springframework.cache.Cache.put.

Associate the specified value with the specified key in this cache.

If the cache previously contained a mapping for this key, the old value is replaced by the specified value.

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 putIfAbsent for guaranteed immediate registration.

If the cache is supposed to be compatible with CompletableFuture and reactive interactions, the put operation needs to be effectively non-blocking, with any backend write-through happening asynchronously. This goes along with a cache implemented and configured to support retrieve(Object) and retrieve(Object, Supplier).

Parameters
key:Object

the key with which the specified value is to be associated

value:Object

the value to be associated with the specified key

Annotations
@Override
putIfAbsentback to summary
public Cache.ValueWrapper putIfAbsent(Object key, Object value)

Overrides default org.springframework.cache.Cache.putIfAbsent.

Doc from org.springframework.cache.Cache.putIfAbsent.

Atomically associate the specified value with the specified key in this cache if it is not set already.

This is equivalent to:


ValueWrapper existingValue = cache.get(key);
if (existingValue == null) {
    cache.put(key, value);
}
return existingValue;
except that the action is performed atomically. While all out-of-the-box CacheManager implementations are able to perform the put atomically, 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 of the native cache implementation that you are using for more details.

The default implementation delegates to get(Object) and put(Object, Object) along the lines of the code snippet above.

Parameters
key:Object

the key with which the specified value is to be associated

value:Object

the value to be associated with the specified key

Returns:Cache.ValueWrapper

the value to which this cache maps the specified key (which may be null itself), or also null if the cache did not contain any mapping for that key prior to this call. Returning null is therefore an indicator that the given value has been associated with the key.

Annotations
@Override
@Nullable
org.springframework.cache.jcache back to summary

private Class JCacheCache.PutIfAbsentEntryProcessor

extends Object
implements EntryProcessor<Object, Object, Object>
Class Inheritance
  • java.lang.Object
  • org.springframework.cache.jcache.JCacheCache.PutIfAbsentEntryProcessor
All Implemented Interfaces
javax.cache.processor.EntryProcessor

Field Summary

Modifier and TypeField and Description
private static final JCacheCache.PutIfAbsentEntryProcessor

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Object
process(MutableEntry<Object, Object>
the entry
entry
,
Object...
a number of arguments to the process.
arguments
)

Implements javax.cache.processor.EntryProcessor.process.

Process an entry.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
private static final JCacheCache.PutIfAbsentEntryProcessor INSTANCE

Constructor Detail

PutIfAbsentEntryProcessorback to summary
private PutIfAbsentEntryProcessor()

Method Detail

processback to summary
public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException

Implements javax.cache.processor.EntryProcessor.process.

Doc from javax.cache.processor.EntryProcessor.process.

Process an entry.

Parameters
entry:MutableEntry<Object, Object>

the entry

arguments:Object[]

a number of arguments to the process.

Returns:Object

the user-defined result of the processing, if any.

Annotations
@Override
@Nullable
Exceptions
EntryProcessorException:
if there is a failure in entry processing.
org.springframework.cache.jcache back to summary

private final Class JCacheCache.ValueLoaderEntryProcessor

extends Object
implements EntryProcessor<Object, Object, Object>
Class Inheritance
  • java.lang.Object
  • org.springframework.cache.jcache.JCacheCache.ValueLoaderEntryProcessor
All Implemented Interfaces
javax.cache.processor.EntryProcessor

Field Summary

Modifier and TypeField and Description
private final Function<Object, Object>
private final Function<Object, Object>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Object
process(MutableEntry<Object, Object>
the entry
entry
,
Object...
a number of arguments to the process.
arguments
)

Implements javax.cache.processor.EntryProcessor.process.

Process an entry.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fromStoreValueback to summary
private final Function<Object, Object> fromStoreValue
toStoreValueback to summary
private final Function<Object, Object> toStoreValue

Constructor Detail

ValueLoaderEntryProcessorback to summary
private ValueLoaderEntryProcessor(Function<Object, Object> fromStoreValue, Function<Object, Object> toStoreValue)

Method Detail

processback to summary
public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException

Implements javax.cache.processor.EntryProcessor.process.

Doc from javax.cache.processor.EntryProcessor.process.

Process an entry.

Parameters
entry:MutableEntry<Object, Object>

the entry

arguments:Object[]

a number of arguments to the process.

Returns:Object

the user-defined result of the processing, if any.

Annotations
@Override
@Nullable
@SuppressWarnings:unchecked
Exceptions
EntryProcessorException:
if there is a failure in entry processing.