View Javadoc

1   package com.atlassian.cache.hazelcast;
2   
3   import java.util.SortedMap;
4   import java.util.concurrent.TimeUnit;
5   
6   import javax.annotation.Nonnull;
7   import javax.annotation.Nullable;
8   
9   import com.atlassian.cache.CacheSettings;
10  import com.atlassian.cache.CacheStatisticsKey;
11  import com.atlassian.cache.ManagedCache;
12  import com.atlassian.util.concurrent.Supplier;
13  
14  import com.google.common.collect.ImmutableSortedMap;
15  import com.hazelcast.config.MapConfig;
16  
17  import static java.util.concurrent.TimeUnit.SECONDS;
18  
19  /**
20   * Hazelcast implementation of common {@link ManagedCache} contract
21   */
22  public abstract class ManagedCacheSupport implements ManagedCache
23  {
24      private final MapConfig config;
25      private final boolean flushable;
26      private final String name;
27  
28      public ManagedCacheSupport(String name, MapConfig config, CacheSettings settings)
29      {
30          this.config = config;
31          this.flushable = settings.getFlushable(true);
32          this.name = name;
33      }
34  
35      @Nullable
36      @Override
37      public Long currentExpireAfterAccessMillis()
38      {
39          long maxIdle = SECONDS.toMillis(config.getMaxIdleSeconds());
40          return (maxIdle > 0 ? maxIdle : null);
41      }
42  
43      @Nullable
44      @Override
45      public Long currentExpireAfterWriteMillis()
46      {
47          long timeToLive = SECONDS.toMillis(config.getTimeToLiveSeconds());
48          return (timeToLive > 0 ? timeToLive : null);
49      }
50  
51      @Nullable
52      @Override
53      public Integer currentMaxEntries()
54      {
55          int maxSize = config.getMaxSizeConfig().getSize();
56          return maxSize > 0 ? maxSize : null;
57      }
58  
59      @Override
60      public boolean isReplicateViaCopy()
61      {
62          return true;
63      }
64  
65      @Nonnull
66      @Override
67      public String getName()
68      {
69          return name;
70      }
71  
72      @Override
73      public boolean isFlushable()
74      {
75          return flushable;
76      }
77  
78      @Override
79      public boolean isLocal()
80      {
81          return false;
82      }
83  
84      @Override
85      public boolean isReplicateAsynchronously()
86      {
87          return false;
88      }
89  
90      @Override
91      public boolean updateExpireAfterAccess(long expireAfter, @Nonnull TimeUnit timeUnit)
92      {
93          config.setMaxIdleSeconds((int) timeUnit.toSeconds(expireAfter));
94          return true;
95      }
96  
97      @Override
98      public boolean updateExpireAfterWrite(long expireAfter, @Nonnull TimeUnit timeUnit)
99      {
100         config.setMaxIdleSeconds((int) timeUnit.toSeconds(expireAfter));
101         return true;
102     }
103 
104     @Override
105     public boolean updateMaxEntries(int newValue)
106     {
107         config.getMaxSizeConfig().setSize(newValue);
108         return true;
109     }
110 
111     @Nonnull
112     @Override
113     public SortedMap<CacheStatisticsKey, Supplier<Long>> getStatistics()
114     {
115         return ImmutableSortedMap.of();
116     }
117 }