지난 post에 이어서,
우선 정상적으로 memcached서버가 운영되고 있는지 client나 ps로 확인해보자.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
141fe50f0cc0 mysql:latest "docker-entrypoint..." 2 days ago Up 3 seconds 0.0.0.0:3306->3306/tcp docker-mysql
a035ab1ff691 memcached "docker-entrypoint..." 3 days ago Up 9 seconds 0.0.0.0:11211->11211/tcp docker-memcache
build.gradle에 ssm에 관련된 lib를 추가한 후, build
def ssmVersion = "3.6.1"
...
compile("com.google.code.simple-spring-memcached:simple-spring-memcached:${ssmVersion}")
compile("com.google.code.simple-spring-memcached:xmemcached-provider:${ssmVersion}")
compile("com.google.code.simple-spring-memcached:spring-cache:${ssmVersion}")
그럼 Spring 설정에 cached 사용에 대한 Config를 추가한다.
@Configuration
@EnableCaching
@EnableAspectJAutoProxy
@ImportResource("classpath:simplesm-context.xml")
public class DockerSpringappsSSMConfiguration extends CachingConfigurerSupport {
@Value("${memcached.server}")
private String server;
@Value("${memcached.cache.name}")
private String cacheName;
@Bean
@Override
public CacheManager cacheManager() {
Set<SSMCache> ssmCacheSet = new HashSet<>();
SSMCache ssmCache = new SSMCache(cacheFactory().getCache(), 300, false);
ssmCacheSet.add(ssmCache);
SSMCacheManager ssmCacheManager = new SSMCacheManager();
ssmCacheManager.setCaches(ssmCacheSet);
return ssmCacheManager;
}
@Bean
@DependsOn("cacheBase")
public CacheFactory cacheFactory() {
CacheFactory cacheFactory = new CacheFactory();
cacheFactory.setCacheName(cacheName);
cacheFactory.setCacheClientFactory(new MemcacheClientFactoryImpl());
XMemcachedConfiguration cacheConfiguration = createCacheConfiguration(server);
cacheFactory.setAddressProvider(new DefaultAddressProvider(server));
cacheFactory.setConfiguration(cacheConfiguration);
return cacheFactory;
}
private XMemcachedConfiguration createCacheConfiguration(final String server) {
XMemcachedConfiguration cacheConfiguration = new XMemcachedConfiguration();
cacheConfiguration.setConsistentHashing(true);
cacheConfiguration.setUseBinaryProtocol(true);
return cacheConfiguration;
}
}
주) 위의 simplesm-context.xml 는 simple-spring-memcached-3.6.1.jar 여기를 찾아보면 나옴.
코드는 다음과 같다.
...
@ReadThroughSingleCache(namespace = "EmployeeFindService:findCachedById_v01", expiration = 600)
public Employee findById(@ParameterValueKeyProvider Long id) {
return employeeFindService.findById(id);
}
...