JSON 使用简单的 spring memcached 进行序列化
JSON serialization with simple spring memcached
我无法使用默认序列化类型设置为 JSON 的 simple-spring-memcached。我得到的错误是:
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-缓存:v3.5.0
spymemcached-provider: v3.5.0
spring: v3.2.8.RELEASE
如果您使用 SSM xml 配置,则无需定义 JSON 转码器。在您的情况下,您需要完全初始化 CacheFactory 对象,因此调用:
cacheFactory.afterPropertiesSet();
就在
之前
Cache object = cacheFactory.getObject();
我无法使用默认序列化类型设置为 JSON 的 simple-spring-memcached。我得到的错误是:
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-缓存:v3.5.0 spymemcached-provider: v3.5.0 spring: v3.2.8.RELEASE
如果您使用 SSM xml 配置,则无需定义 JSON 转码器。在您的情况下,您需要完全初始化 CacheFactory 对象,因此调用:
cacheFactory.afterPropertiesSet();
就在
之前Cache object = cacheFactory.getObject();