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

org.infinispan.spring.provider.CacheDelegate Maven / Gradle / Ivy

Go to download

The Infinispan Spring Integration project provides Spring integration for Infinispan, a high performance distributed cache. Its primary features are * An implementation of org.springframework.cache.CacheManager, Spring's central caching abstraction, backed by Infinispan's EmbeddedCacheManager. To be used if your Spring-powered application and Infinispan are colocated, i.e. running within the same VM. * An implementation of org.springframework.cache.CacheManager backed by Infinispan's RemoteCacheManager. To bes used if your Spring-powered application accesses Infinispan remotely, i.e. over the network. * An implementation of org.springframework.cache.CacheManager backed by a CacheContainer reference. To be used if your Spring- powered application needs access to a CacheContainer defined outside the application (e.g. retrieved from JNDI) * Spring namespace support allowing shortcut definitions for all the components above In addition, Infinispan Spring Integration offers various FactoryBeans for facilitating creation of Infinispan core classes - Cache, CacheManager, ... - within a Spring context.

There is a newer version: 8.1.0.Alpha2
Show newest version
package org.infinispan.spring.provider;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.util.Assert;

/**
 * Since there are 2 Cache implementation with exactly the same implementation details -
 * is it convenient to introduce common abstraction and delegate all the methods.
 * This is exactly what happens here.
 *
 * @author Sebastian Laskawiec
 */
class CacheDelegate implements Cache {

   private final org.infinispan.commons.api.BasicCache nativeCache;

   /**
    * @param nativeCache underlying cache
    */
   public CacheDelegate(final org.infinispan.commons.api.BasicCache nativeCache) {
      Assert.notNull(nativeCache, "A non-null Infinispan cache implementation is required");
      this.nativeCache = nativeCache;
   }

   /**
    * @see org.springframework.cache.Cache#getName()
    */
   @Override
   public String getName() {
      return this.nativeCache.getName();
   }

   /**
    * @see org.springframework.cache.Cache#getNativeCache()
    */
   @Override
   public org.infinispan.commons.api.BasicCache getNativeCache() {
      return this.nativeCache;
   }

   /**
    * @see org.springframework.cache.Cache#get(Object)
    */
   @Override
   public ValueWrapper get(final Object key) {
      return toValueWrapper(nativeCache.get(key));
   }

   @Override
   public  T get(Object key, Class type) {
      Object value = nativeCache.get(key);
      if (value != null && type != null && !type.isInstance(value)) {
         throw new IllegalStateException("Cached value is not of required type [" + type.getName() + "]: " + value);
      }
      return (T) value;
   }

   /**
    * @see org.springframework.cache.Cache#put(Object, Object)
    */
   @Override
   public void put(final Object key, final Object value) {
      this.nativeCache.put(key, value != null ? value : NullValue.NULL);
   }

   @Override
   public ValueWrapper putIfAbsent(Object key, Object value) {
      return toValueWrapper(this.nativeCache.putIfAbsent(key, value));
   }

   /**
    * @see org.springframework.cache.Cache#evict(Object)
    */
   @Override
   public void evict(final Object key) {
      this.nativeCache.remove(key);
   }

   /**
    * @see org.springframework.cache.Cache#clear()
    */
   @Override
   public void clear() {
      this.nativeCache.clear();
   }


   /**
    * @see Object#toString()
    */
   @Override
   public String toString() {
      return "InfinispanCache [nativeCache = " + this.nativeCache + "]";
   }

   private ValueWrapper toValueWrapper(Object value) {
      if (value == null) {
         return null;
      }
      if (value == NullValue.NULL) {
         return NullValue.NULL;
      }
      return new SimpleValueWrapper(value);
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy