Once you have a Cache
available, you can find out the key/value types with which it was configured by inspecting its Configuration
:
// assuming you already have a Cache object available:
javax.cache.configuration.Configuration cacheConfig = cache.getConfiguration(Configuration.class);
Class keyType = cacheConfig.getKeyType();
Class valueType = cacheConfig.getValueType();
Now, in order to obtain the Cache
itself when using JCache 1.0 if you are not aware of its configured key/value types options, your only option is to use cacheManager.getCache(name)
. However according to JCache 1.0 this will only work if the Cache
was not configured with any specific types (ie Object.class
is the key and value type), otherwise will fail with IllegalArgumentException
(see [1]). Essentially you are caught in a vicious circle.
This behavior has been updated in JCache 1.1 (see related issue in [2]): now cacheManager.getCache(name)
specification is relaxed and may return the Cache
if one exists by the given name, regardless of configured key/value types. The only catch right now is that JCache 1.1 was released just a couple of days ago (December 16th, 2017, see [3]), so most probably implementations are not yet updated to allow this behavior.
[1] https://github.com/jsr107/jsr107spec/blob/v1.0.0/src/main/java/javax/cache/CacheManager.java#L200-L201
[2] https://github.com/jsr107/jsr107spec/issues/340#issuecomment-254070714
[3] http://search.maven.org/#artifactdetails%7Cjavax.cache%7Ccache-api%7C1.1.0%7Cjar