Maven
com.google.code.simple-spring-memcached
spring-cache
3.5.0
com.google.code.simple-spring-memcached
spymemcached-provider
3.5.0
net.spy
spymemcached
2.11.7
코드
@EnableCaching
@Configuration
public class MemoryCacheConfig extends CachingConfigurerSupport {
@SuppressWarnings(value = "unused")
private static Logger logger = LoggerFactory.getLogger(MemoryCacheConfig.class);
@Override
@Bean
public CacheManager cacheManager() {
ExtendedSSMCacheManager cacheManager = new ExtendedSSMCacheManager();
try {
List list = new ArrayList();
list.add(new SSMCache(defaultCache("song"), 300, false));
cacheManager.setCaches(list);
} catch (Exception e) {
e.printStackTrace();
}
return cacheManager;
}
@Override
@Bean
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
@Override
public CacheResolver cacheResolver() {
return null;
}
@Override
public CacheErrorHandler errorHandler() {
return null;
}
private Cache defaultCache(String cacheName) throws Exception {
CacheFactory cacheFactory = new CacheFactory();
cacheFactory.setCacheName(cacheName);
cacheFactory.setCacheClientFactory(new MemcacheClientFactoryImpl());
String serverHost = "IP:PORT";
cacheFactory.setAddressProvider(new DefaultAddressProvider(serverHost));
cacheFactory.setConfiguration(cacheConfiguration());
return cacheFactory.getObject();
}
@Bean
public SpymemcachedConfiguration cacheConfiguration() {
SpymemcachedConfiguration cacheConfiguration = new SpymemcachedConfiguration();
cacheConfiguration.setConsistentHashing(true);
cacheConfiguration.setUseBinaryProtocol(true);
cacheConfiguration.setOperationTimeout(200);
cacheConfiguration.setKeyPrefixSeparator(".");
cacheConfiguration.setUseNameAsKeyPrefix(true);
cacheConfiguration.setTimeoutExceptionThreshold(20);
cacheConfiguration.setUseNagleAlgorithm(false);
cacheConfiguration.setFailureMode(FailureMode.Redistribute);
cacheConfiguration.setDefaultTranscoder(new SerializingTranscoder());
cacheConfiguration.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
return cacheConfiguration;
}
public String getBasePackage() {
return "com.creamsugardonut";
}
}
관련