Class HazelcastHybridCache<K,V>

java.lang.Object
com.atlassian.cache.hazelcast.ManagedHybridCacheSupport
com.atlassian.cache.hazelcast.HazelcastHybridCache<K,V>
All Implemented Interfaces:
com.atlassian.cache.Cache<K,V>, com.atlassian.cache.ManagedCache, com.atlassian.cache.ReadThroughCache<K,V>

public class HazelcastHybridCache<K,V> extends ManagedHybridCacheSupport implements com.atlassian.cache.Cache<K,V>
Implementation of ManagedCache and Cache that can be used when the cached values do not implement Serializable but cache invalidation must work cluster-wide.

This implementation tracks versions of entries in Hazelcast IMap, but stores the actual values in a local cache. Caches entries are invalidated when the cache is modified through one of the put, clear, remove or replace methods. Cache entries are never invalidated when get(Object) is called, not even when a value is lazily created and added to the cache.

These semantics mean that hybrid caches should only be used as computing caches (by providing a CacheLoader) and that values should not be manually put(Object, Object) in the cache. Doing so will lead to frequent cache evictions.

Since:
2.4.0
  • Constructor Details

    • HazelcastHybridCache

      public HazelcastHybridCache(String name, com.atlassian.cache.CacheFactory localCacheFactory, com.hazelcast.map.IMap<K,Long> versionMap, com.atlassian.cache.CacheLoader<K,V> cacheLoader, HazelcastCacheManager cacheManager)
  • Method Details

    • clear

      public void clear()
      Specified by:
      clear in interface com.atlassian.cache.ManagedCache
    • containsKey

      public boolean containsKey(@Nonnull K k)
      Specified by:
      containsKey in interface com.atlassian.cache.ReadThroughCache<K,V>
    • get

      public V get(@Nonnull K key)
      Specified by:
      get in interface com.atlassian.cache.ReadThroughCache<K,V>
    • get

      @Nonnull public V get(@Nonnull K key, @Nonnull com.atlassian.cache.Supplier<? extends V> valueSupplier)
      Specified by:
      get in interface com.atlassian.cache.ReadThroughCache<K,V>
    • getKeys

      @Nonnull public Collection<K> getKeys()
      Specified by:
      getKeys in interface com.atlassian.cache.ReadThroughCache<K,V>
    • getName

      @Nonnull public String getName()
      Specified by:
      getName in interface com.atlassian.cache.ManagedCache
      Specified by:
      getName in interface com.atlassian.cache.ReadThroughCache<K,V>
      Overrides:
      getName in class ManagedHybridCacheSupport
    • isReplicateAsynchronously

      public boolean isReplicateAsynchronously()
      Specified by:
      isReplicateAsynchronously in interface com.atlassian.cache.ManagedCache
    • put

      public void put(@Nonnull K key, @Nonnull V value)
      Specified by:
      put in interface com.atlassian.cache.Cache<K,V>
    • putIfAbsent

      public V putIfAbsent(@Nonnull K key, @Nonnull V value)

      This implementation has a weak spot: When there is an existing value, this method must return it. However, if that value is stored in a remote node, it cannot be returned. null is returned instead, which signals success. In order to not break any contracts, this implementation will invalidate the value on all nodes in this situation.

      Specified by:
      putIfAbsent in interface com.atlassian.cache.Cache<K,V>
    • remove

      public void remove(@Nonnull K key)
      Specified by:
      remove in interface com.atlassian.cache.ReadThroughCache<K,V>
    • remove

      public boolean remove(@Nonnull K key, @Nonnull V value)
      Specified by:
      remove in interface com.atlassian.cache.ReadThroughCache<K,V>
    • removeAll

      public void removeAll()
      Specified by:
      removeAll in interface com.atlassian.cache.ReadThroughCache<K,V>
    • replace

      public boolean replace(@Nonnull K key, @Nonnull V oldValue, @Nonnull V newValue)
      Specified by:
      replace in interface com.atlassian.cache.Cache<K,V>
    • updateExpireAfterAccess

      public boolean updateExpireAfterAccess(long expireAfter, @Nonnull TimeUnit timeUnit)
      Specified by:
      updateExpireAfterAccess in interface com.atlassian.cache.ManagedCache
      Overrides:
      updateExpireAfterAccess in class ManagedHybridCacheSupport
    • updateExpireAfterWrite

      public boolean updateExpireAfterWrite(long expireAfter, @Nonnull TimeUnit timeUnit)
      Specified by:
      updateExpireAfterWrite in interface com.atlassian.cache.ManagedCache
      Overrides:
      updateExpireAfterWrite in class ManagedHybridCacheSupport
    • updateMaxEntries

      public boolean updateMaxEntries(int newValue)
      Specified by:
      updateMaxEntries in interface com.atlassian.cache.ManagedCache
      Overrides:
      updateMaxEntries in class ManagedHybridCacheSupport
    • addListener

      public void addListener(@Nonnull com.atlassian.cache.CacheEntryListener<K,V> listener, boolean includeValues)
      Specified by:
      addListener in interface com.atlassian.cache.Cache<K,V>
    • removeListener

      public void removeListener(@Nonnull com.atlassian.cache.CacheEntryListener<K,V> listener)
      Specified by:
      removeListener in interface com.atlassian.cache.Cache<K,V>
    • getLocalCache

      protected com.atlassian.cache.ManagedCache getLocalCache()
      Specified by:
      getLocalCache in class ManagedHybridCacheSupport
    • isFlushable

      public boolean isFlushable()
      Specified by:
      isFlushable in interface com.atlassian.cache.ManagedCache