You can retrieve javax.cache.Cache
instances directly through HazelcastInstance::getCache(String name)
method.
The parameter name
in HazelcastInstance::getCache(String name)
is the full cache name except the Hazelcast prefix, i.e., /hz/
.
If you create a cache through a CacheManager
which has its own specified URI scope (and/or specified classloader),
it must be prepended to the pure cache name as a prefix while retrieving the cache through HazelcastInstance::getCache(String name)
.
Prefix generation for full cache name (except the Hazelcast prefix, which is /hz/
) is exposed through
com.hazelcast.cache.CacheUtil#getPrefixedCacheName(String name, java.net.URI uri, ClassLoader classloader)
.
If the URI scope and classloader is not specified, the pure cache name can be used directly while retrieving cache over HazelcastInstance
.
If you have a cache which is not created, but is defined/exists (cache is specified in Hazelcast configuration but not created yet), you can retrieve this cache by its name. This also triggers cache creation before retrieving it. This retrieval is supported through HazelcastInstance
. However, HazelcastInstance
does not support creating a cache by specifying configuration; this is supported by Hazelcast's CacheManager
as it is.
NOTE: If a valid (rather than 1.0.0-PFD or 0.x versions) JCache library does not exist on the classpath, IllegalStateException
is thrown.
JCache and Hazelcast Instance Awareness
HazelcastInstance
is injected into the following cache API interfaces (provided by javax.cache.Cache
and com.hazelcast.cache.ICache
) if they implement HazelcastInstanceAware
interface:
-
ExpiryPolicyFactory
andExpiryPolicy
[provided byjavax.cache.Cache
] -
CacheLoaderFactory
andCacheLoader
[provided byjavax.cache.Cache
] -
CacheWriteFactory
andCacheWriter
[provided byjavax.cache.Cache
] -
EntryProcessor
[provided byjavax.cache.Cache
] -
CacheEntryListener
(CacheEntryCreatedListener
,CacheEntryUpdatedListener
,CacheEntryRemovedListener
,CacheEntryExpiredListener
) [provided byjavax.cache.Cache
] -
CacheEntryEventFilter
[provided byjavax.cache.Cache
] -
CompletionListener
[provided byjavax.cache.Cache
] -
CachePartitionLostListener
[provided bycom.hazelcast.cache.ICache
]