스프링은 빈 설정을 간편히 하라고 네임스페이스를 많이 제공해주는데, 실제로 개발할 때 매우 편합니다. 그냥 스프링이 제공해주는 그.대.로. 사용할 때에만요. 스프링 캐시의 핵심 API는 다음과 같습니다.

CacheManager: EhCache의 CacheManager와 이름이 같지만, 이건 패키지가 다르고 스프링이 제공하는 캐시 추상화의 핵심 인터페이습니다.

Cacahe: 이건 CacheManager에서 get(“캐시 이름”)을 사용해서 가져올 수 있는 클래스고요.

KeyGenerator: 캐시 키값을 생성하는 방식을 Strategy Pattern화 시켜둔걸로 보시면 됩니다. 기본 구현체로는 해당 객체의 hashCode()를 사용하는 DefaultKeyGenerator를 사용하는데, 분산 환경에는 적절치 못할 겁니다.

사실 얘네들 정도를 확장하려면, 더 깊게 들어가지 않아도 됩니다. 그런데 저는 좀 더 필요한 경우를 보게 됐습니다; 보게 됐다? 요구 사항이 있었습니다;

아무튼 이런 것들을 이용해서 Cache AOP를 제공할 때의 주요 API는 다음과 같습니다.

CacheInterceptor: MethodInterceptor의 구현체로, 스프링 AOP에서 캐시 기능을 Advice를 구현하나 핵신 구현체입니다.

CacheOperationSource: 이건 CacheInterceptor에서 사용하는 인터페이스로, 특정 메서드에서 수행할 캐시 오퍼레이션이 무엇인지 알려주는 역할을 합니다. 기본 구현체로는 AnnotationCacheOperationSource를 사용합니다.

BeanFactoryCacheOperationSourceAdvisor: 스프링 AOP를 아시는 분들은 뭔지 잘 아시겠죠. Advisor니까 Advice와 Pointcut의 조합이구요. 이 안에 기본으로 CacheOperationSourcePoincut이 Pointcut으로 들어있습니다. 따라서 캐시 적용 가능한 메서드에만 포인트컷이 걸립니다.

이것들을 모두 이용하는게…

[xml]
<cache:annotation-driven/>
[/xml]

이 한줄입니다. 이걸 풀어서 적으면 이렇게 됩니다.

[xml]
<!-- cache setup -->
<bean id="annotationCacheOperationSource" class="org.springframework.cache.annotation.AnnotationCacheOperationSource" />
<bean id="cacheInterceptor" class="org.springframework.cache.interceptor.CacheInterceptor">
<property name="cacheDefinitionSources" ref="annotationCacheOperationSource" />
<property name="cacheManager" ref="cacheManager"/>
</bean>
<bean id="cacheAdvisor" class="org.springframework.cache.interceptor.BeanFactoryCacheOperationSourceAdvisor">
<property name="cacheDefinitionSource" ref="annotationCacheOperationSource" />
<property name="advice" ref="cacheInterceptor" />
</bean>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="ehcache.xml"/>
[/xml]

이 중에서 원하는 객체를 확장해서 교체하면 되겠죠. 끝~