title: Spring cache using memcached
body: <p>I'm using <a href="https://github.com/AKQABER/simple-spring-memcached" rel="nofollow">https://github.com/AKQABER/simple-spring-memcached</a> to have memcached integrated as a cache implementation with spring.</p>
  
  <p>But I'had no success :P</p>
  
  <p>I'm using spring 4 and spring-data-jpa</p>
  
  <pre><code>public interface FooRepository extends JpaRepository&lt;Foo, Long&gt; {
  
  @Cacheable(value = "defaultCache", key = "lala")
      Foo findByNameAndDescription(String name, String description);
  } 
  </code></pre>
  
  <p>I hardcoded the key for test purposes. Nothing special, use a cache called "defaultCache".
  The configuration of memcached is:</p>
  
  <pre><code>@Configuration
  @EnableAspectJAutoProxy
  public class CacheConfiguration
  {
  
  @Bean
  public CacheManager cacheManager()
  {
      MemcacheClientFactoryImpl cacheClientFactory = new MemcacheClientFactoryImpl();
      AddressProvider addressProvider = new DefaultAddressProvider("127.0.0.1:11211");
      com.google.code.ssm.providers.CacheConfiguration cacheConfiguration = new com.google.code.ssm.providers.CacheConfiguration();
      cacheConfiguration.setConsistentHashing(true); //TODO check this
  
      CacheFactory cacheFactory = new CacheFactory();
      cacheFactory.setCacheName           ( "defaultCache"        );
      cacheFactory.setCacheClientFactory  ( cacheClientFactory    );
      cacheFactory.setAddressProvider     ( addressProvider       );
      cacheFactory.setConfiguration       ( cacheConfiguration    );
  
      Cache object = null;
      try {
          object = cacheFactory.getObject();
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      SSMCache ssmCache = new SSMCache(object, 10000, true); //TODO be very carefully here, third param allow remove all entries!!
  
      ArrayList&lt;SSMCache&gt; ssmCaches = new ArrayList&lt;SSMCache&gt;();
      ssmCaches.add(0, ssmCache);
  
      SSMCacheManager ssmCacheManager = new SSMCacheManager();
      ssmCacheManager.setCaches(ssmCaches);
  
  
      return ssmCacheManager;
  }
  }
  </code></pre>
  
  <p>And I can test it using a simple main class with code: </p>
  
  <pre><code>CacheManager cacheManager = context.getBean(CacheManager.class);
      FooRepository repository = context.getBean(FooRepository.class);
  
      Cache defaultCache = cacheManager.getCache("defaultCache");
      defaultCache.clear();
      Foo byNameAndDescription = repository.findByNameAndDescription(DEFAULT_NAME,    DEFAULT_DESCRIPTION);
      Object obj = defaultCache.get("lala");
  </code></pre>
  
  <p>And obj is always null. However if I use defaultCache object directly I can put and get items to/from cache without problems. Any idea?</p>