javajsonspringmemcachedsimple-spring-memcached

JSON serialization with simple spring memcached


I'm unable to use simple-spring-memcached with default serialization type set to JSON. The error I get is:

java.lang.IllegalArgumentException: Cannot use JSON serialization because dedicated cache transcoder is null!
at com.google.code.ssm.CacheImpl.set(CacheImpl.java:290) ~[simple-spring-memcached-3.5.0.jar:na]
at com.google.code.ssm.CacheImpl.set(CacheImpl.java:125) ~[simple-spring-memcached-3.5.0.jar:na]
at com.google.code.ssm.PrefixedCacheImpl.set(PrefixedCacheImpl.java:130) ~[simple-spring-memcached-3.5.0.jar:na]
at com.google.code.ssm.spring.SSMCache.put(SSMCache.java:159) ~[spring-cache-3.5.0.jar:na]
at org.springframework.cache.interceptor.CacheAspectSupport.update(CacheAspectSupport.java:351) [spring-context-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:214) [spring-context-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66) [spring-context-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) [spring-aop-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at com.sun.proxy.$Proxy105.findByValue(Unknown Source) [na:na]

My Configuration is :

    @Configuration()
    @EnableAspectJAutoProxy
    @EnableCaching
    public class SimpleSpringCacheConfig {

    @Autowired
    private Environment env;

    private static final String DEFAULT_MEMCACHED_HOST = "127.0.0.1:11211";
    private static final Integer DEFAULT_MEMCACHED_TTL_SECONDS = 3600;
    private static final Integer DEFAULT_MEMCACHED_TIMEOUT_MILLIS = 500;

    private static final String PROPERTY_MEMCACHED_HOSTS = "service.caching.memcached.hosts";
    private static final String PROPERTY_DEFAULT_TTL_SECONDS="service.caching.default.ttl.seconds";

    private static final Logger log = LoggerFactory.getLogger(SimpleSpringCacheConfig.class);

    //reference config on https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started#Spring_3.1_Cache_Integration
    @Bean
    public CacheManager cacheManager() throws Exception
    {
            MemcacheClientFactoryImpl cacheClientFactory = new MemcacheClientFactoryImpl();
            AddressProvider addressProvider = new DefaultAddressProvider(env.getProperty(PROPERTY_MEMCACHED_HOSTS, DEFAULT_MEMCACHED_HOST));
            CacheConfiguration cacheConfiguration = new CacheConfiguration();

            cacheConfiguration.setKeyPrefixSeparator("_");
            cacheConfiguration.setUseNameAsKeyPrefix(true);
            cacheConfiguration.setConsistentHashing(true);
            cacheConfiguration.setOperationTimeout(DEFAULT_MEMCACHED_TIMEOUT_MILLIS);

            CacheFactory cacheFactory = new CacheFactory();
            cacheFactory.setCacheName("simpleMemcachedCache");
            cacheFactory.setCacheClientFactory(cacheClientFactory);
            cacheFactory.setAddressProvider(addressProvider);
            cacheFactory.setConfiguration(cacheConfiguration);
            cacheFactory.setDefaultSerializationType(SerializationType.JSON);

            Cache object = cacheFactory.getObject();

            int ttl = env.getProperty(PROPERTY_DEFAULT_TTL_SECONDS, Integer.class, DEFAULT_MEMCACHED_TTL_SECONDS);

            //@CacheEvict(..., "allEntries" = true) won't work because allowClear is false, 
            //so we won't flush accidentally all entries from memcached instance..
            SSMCache ssmCache = new SSMCache(object, ttl, false);

            ArrayList<SSMCache> ssmCaches = new ArrayList<SSMCache>();
            ssmCaches.add(0, ssmCache);

            SSMCacheManager ssmCacheManager = new SSMCacheManager();
            ssmCacheManager.setCaches(ssmCaches);

            return ssmCacheManager;
    }
}

According to the guide here https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started#Serialization it doesn't seem necessary to define a custom transcoder. What could I be doing wrong?

I'm using the following versions.. spring-cache: v3.5.0 spymemcached-provider: v3.5.0 spring: v3.2.8.RELEASE


Solution

  • There is no requirement to define JSON transcoder if you use SSM xml configuration. In your case you need to fully initialize CacheFactory object so invoke:

    cacheFactory.afterPropertiesSet();
    

    just before

    Cache object = cacheFactory.getObject();