View Javadoc

1   package com.atlassian.cache.hazelcast;
2   
3   import com.atlassian.cache.CacheSettings;
4   
5   import com.hazelcast.config.MapConfig;
6   import com.hazelcast.config.MaxSizeConfig;
7   
8   import static com.hazelcast.config.MaxSizeConfig.MaxSizePolicy.PER_NODE;
9   
10  /**
11   * Performs the (re)configuration of Hazelcast {@link MapConfig} objects.
12   *
13   * @since 2.4.0
14   */
15  public class HazelcastMapConfigConfigurator
16  {
17      // If the cache is a hybrid cache, only the entry versions are tracked in the IMap. These versions should remain
18      // cached longer than the values in local caches. A multiplier is applied to the config parameters that affect
19      // cache eviction to enforce this.
20      public static final int HYBRID_MULTIPLIER = 2;
21  
22      public MapConfig configureMapConfig(CacheSettings settings, MapConfig mapConfig)
23      {
24          boolean hybrid = !settings.getReplicateViaCopy(true);
25          Integer multiplier = hybrid ? HYBRID_MULTIPLIER : 1;
26          Integer maxEntries = settings.getMaxEntries();
27          if (maxEntries != null)
28          {
29              mapConfig.setMaxSizeConfig(new MaxSizeConfig().setMaxSizePolicy(PER_NODE).setSize(multiplier * maxEntries));
30              mapConfig.setEvictionPolicy(MapConfig.EvictionPolicy.LFU);
31          }
32  
33          final Long expireAfterAccess = settings.getExpireAfterAccess();
34          if (expireAfterAccess != null)
35          {
36              mapConfig.setMaxIdleSeconds(multiplier * roundUpToWholeSeconds(expireAfterAccess));
37          }
38  
39          final Long expireAfterWrite = settings.getExpireAfterWrite();
40          if (expireAfterWrite != null)
41          {
42              mapConfig.setTimeToLiveSeconds(multiplier * roundUpToWholeSeconds(expireAfterWrite));
43          }
44  
45          return mapConfig;
46      }
47  
48      private static int roundUpToWholeSeconds(final Long expireAfterAccess)
49      {
50          return (int) Math.ceil(expireAfterAccess / 1000d);
51      }
52  }