I'm using Hazelcast 3.12 jCache implementation. My cache has been declared declaratively (via hazelcast-config.xml), however I want to be able to change the ExpiryPolicy's duration dynamically, as the timeout value is only available programatically.
I've looked at the docs for ICache but do not see any methods that would allow me to retreive and/or modify the expiry policy for the cache as a whole.
My cache is declared as:
<cache name="toto">
<async-backup-count>1</async-backup-count>
<backup-count>1</backup-count>
<cache-entry-listeners>
<cache-entry-listener old-value-required="true">
<cache-entry-listener-factory class-name="cache.UserRolesEntryListenerStaticFactory"/>
</cache-entry-listener>
</cache-entry-listeners>
<key-type class-name="java.lang.String"/>
<management-enabled>true</management-enabled>
<statistics-enabled>true</statistics-enabled>
<quorum-ref>1</quorum-ref>
<partition-lost-listeners></partition-lost-listeners>
</cache>
and I would like to set/update the expiry policy when it is retrieved as:
@Produces
@Singleton
@UserRolesCache
public Cache<String, String[]> createUserRoleCache(@HazelcastDistributed CacheManager cacheManager) {
Cache cache = cacheManager.getCache("toto");
// get expiry timeout from a 3rd service
int timeout = configService.getCacheExpiry();
// how to set the expiry policy here???
// cache.setExpiryPolicy(.....) ?????
}
Is this feasible using the jCache or Hazelcast API?
There is no explicit way to change the default expiry policy of a
Cache
(either in JCache or Hazelcast-specific APIs).You can achieve the same effect by configuring your
Cache
with a customFactory<ExpiryPolicy>
. In yourExpiryPolicy
implementation you can consult your external service to query the current expiry duration and return that so the JCache implementation applies it. Notice that since the expiry policy is queried on each entry creation/access/update, it is best thatExpiryPolicy
methods implementation do not involve any remote service queries or database access, otherwise you will experience high latency. For example, it is best to register your expiry policy as a listener to your external service (if supported) or have a separate executor to schedule queries to the external service for updates.Example implementation using JCache API:
You can supply your custom expiry policy factory class name in declarative Hazelcast XML config like this:
As a side-note, there are methods in
ICache
, the Hazelcast-specific extendedCache
interface, that allow you to perform operations on a key or set of keys with a custom expiry policy specified per-key (but not change the cache-wide applicable expiry policy).