All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.client.hotrod.impl.InvalidatedNearRemoteCache Maven / Gradle / Ivy

package org.infinispan.client.hotrod.impl;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.VersionedValue;
import org.infinispan.client.hotrod.near.NearCacheService;

/**
 * Near {@link org.infinispan.client.hotrod.RemoteCache} implementation
 * enabling
 *
 * @param 
 * @param 
 */
public class InvalidatedNearRemoteCache extends RemoteCacheImpl {

   private final NearCacheService nearcache;

   public InvalidatedNearRemoteCache(RemoteCacheManager rcm, String name, NearCacheService nearcache) {
      super(rcm, name);
      this.nearcache = nearcache;
   }

   @Override
   public V get(Object key) {
      VersionedValue versioned = getVersioned((K) key);
      return versioned != null ? versioned.getValue() : null;
   }

   @Override
   public VersionedValue getVersioned(K key) {
      VersionedValue nearValue = nearcache.get(key);
      if (nearValue == null) {
         VersionedValue remoteValue = super.getVersioned(key);
         if (remoteValue != null)
            nearcache.putIfAbsent(key, remoteValue);

         return remoteValue;
      }

      return nearValue;
   }

   @Override
   public V put(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) {
      V ret = super.put(key, value, lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit);
      nearcache.remove(key); // Eager invalidation to avoid race
      return ret;
   }

   @Override
   public void putAll(Map map, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) {
      super.putAll(map, lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit);
      map.keySet().forEach(nearcache::remove);
   }

   @Override
   public V replace(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) {
      boolean hasForceReturnValue = operationsFactory.hasFlag(Flag.FORCE_RETURN_VALUE);
      V prev = super.replace(key, value, lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit);
      invalidateNearCacheIfNeeded(hasForceReturnValue, key, prev);
      return prev;
   }

   @Override
   public boolean replaceWithVersion(K key, V newValue, long version, long lifespan, TimeUnit lifespanTimeUnit, long maxIdle, TimeUnit maxIdleTimeUnit) {
      boolean replaced = super.replaceWithVersion(key, newValue, version, lifespan, lifespanTimeUnit, maxIdle, maxIdleTimeUnit);
      if (replaced) nearcache.remove(key);
      return replaced;
   }

   @Override
   public V remove(Object key) {
      boolean hasForceReturnValue = operationsFactory.hasFlag(Flag.FORCE_RETURN_VALUE);
      V prev = super.remove(key);
      invalidateNearCacheIfNeeded(hasForceReturnValue, key, prev);
      return prev;
   }

   @Override
   public boolean removeWithVersion(K key, long version) {
      boolean removed = super.removeWithVersion(key, version);
      if (removed) nearcache.remove(key); // Eager invalidation to avoid race
      return removed;
   }

   @Override
   public void clear() {
      super.clear();
      nearcache.clear(); // Clear near cache too
   }

   @SuppressWarnings("unchecked")
   void invalidateNearCacheIfNeeded(boolean hasForceReturnValue, Object key, Object prev) {
      if (!hasForceReturnValue || prev != null)
         nearcache.remove((K) key);
   }

   @Override
   public void start() {
      nearcache.start(this);
   }

   @Override
   public void stop() {
      nearcache.stop(this);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy