Сериализация JSON с помощью простого Spring memcached

Я не могу использовать simple-spring-memcached с типом сериализации по умолчанию, установленным в JSON. Ошибка, которую я получаю:

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]

Моя конфигурация:

    @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;
    }
}

Согласно руководству здесь https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started#Serialization не представляется необходимым определять собственный транскодер. Что я могу делать неправильно?

Я использую следующие версии. spring-cache: v3.5.0 spymemcached-provider: v3.5.0 spring: v3.2.8.RELEASE


person Gagan    schedule 02.09.2015    source источник


Ответы (1)


Нет необходимости определять транскодер JSON, если вы используете конфигурацию SSM xml. В вашем случае вам нужно полностью инициализировать объект CacheFactory, поэтому вызовите:

cacheFactory.afterPropertiesSet();

как раз перед

Cache object = cacheFactory.getObject();
person ragnor    schedule 02.09.2015