The JCache specification has an option to configure a single
ExpiryPolicy per cache. Hazelcast ICache extension
offers the possibility to define a custom
ExpiryPolicy per key by providing a set of method overloads with an
parameter, as in the list of asynchronous methods in the Async Methods section. This means that you can pass custom expiry policies to a cache operation.
Here is how an
ExpirePolicy is set on JCache configuration:
CompleteConfiguration<String, String> config = new MutableConfiguration<String, String>() setExpiryPolicyFactory( AccessedExpiryPolicy.factoryOf( Duration.ONE_MINUTE ) );
To pass a custom
ExpirePolicy, a set of overloads is provided. You can use them as shown in the following code example.
ICache<Integer, String> unwrappedCache = cache.unwrap( ICache.class ); unwrappedCache.put( 1, "value", new AccessedExpiryPolicy( Duration.ONE_DAY ) );
ExpirePolicy instance can be pre-created, cached, and re-used, but only for each cache instance. This is because
implementations can be marked as
java.io.Closeable. The following list shows the provided method overloads over
com.hazelcast.cache.ICache featuring the
put(key, value, expirePolicy)
getAndPut(key, value, expirePolicy)
putIfAbsent(key, value, expirePolicy)
replace(key, value, expirePolicy)
replace(key, oldValue, newValue):
replace(key, oldValue, newValue, expirePolicy)
getAndReplace(key, value, expirePolicy)
Asynchronous method overloads are not listed here. Please see ICache Async Methods for the list of asynchronous method overloads.