Created
April 8, 2015 09:52
-
-
Save thomasdarimont/8b16bc32b47a17e31ac3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package demo; | |
| import java.util.Arrays; | |
| import java.util.Collection; | |
| import java.util.concurrent.TimeUnit; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.boot.SpringApplication; | |
| import org.springframework.boot.autoconfigure.SpringBootApplication; | |
| import org.springframework.cache.Cache; | |
| import org.springframework.cache.CacheManager; | |
| import org.springframework.cache.annotation.CacheConfig; | |
| import org.springframework.cache.annotation.Cacheable; | |
| import org.springframework.cache.annotation.CachingConfigurerSupport; | |
| import org.springframework.cache.annotation.EnableCaching; | |
| import org.springframework.cache.concurrent.ConcurrentMapCacheManager; | |
| import org.springframework.cache.interceptor.CacheOperationInvocationContext; | |
| import org.springframework.cache.interceptor.CacheResolver; | |
| import org.springframework.context.ConfigurableApplicationContext; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.context.annotation.Primary; | |
| @SpringBootApplication | |
| @EnableCaching | |
| public class CacheResolverConfigExample { | |
| public static void main(String[] args) { | |
| ConfigurableApplicationContext ctx = SpringApplication.run(CacheResolverConfigExample.class, args); | |
| BusinessService service = ctx.getBean(BusinessService.class); | |
| System.out.println(service.computeWithCacheA("a")); | |
| System.out.println(service.computeWithCacheB("b")); | |
| System.out.println(service.computeWithCacheA("a")); | |
| System.out.println(service.computeWithCacheB("b")); | |
| } | |
| @Bean | |
| public CacheManager cacheManagerA() { | |
| return new ConcurrentMapCacheManager(); | |
| } | |
| @Bean | |
| @Primary | |
| public CacheManager cacheManagerB() { | |
| return new ConcurrentMapCacheManager(); | |
| } | |
| @Configuration | |
| static class CachingConfig extends CachingConfigurerSupport { | |
| @Autowired CacheManager cacheManagerA; | |
| @Autowired CacheManager cacheManagerB; | |
| public CachingConfig() {} | |
| @Override | |
| public CacheResolver cacheResolver() { | |
| return new CacheResolver() { | |
| @Override | |
| public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) { | |
| Class<?> returnType = context.getMethod().getReturnType(); | |
| if (BusinessObjectA.class.equals(returnType)) { | |
| return Arrays.asList(cacheManagerA.getCache(returnType.getSimpleName())); | |
| } else if (BusinessObjectB.class.equals(returnType)) { | |
| return Arrays.asList(cacheManagerB.getCache(returnType.getSimpleName())); | |
| } | |
| return null; | |
| } | |
| }; | |
| } | |
| } | |
| @Bean | |
| public BusinessService businessService() { | |
| return new DefaultBusinessService(); | |
| } | |
| public static interface BusinessService { | |
| BusinessObjectA computeWithCacheA(String param); | |
| BusinessObjectB computeWithCacheB(String param); | |
| } | |
| @CacheConfig(cacheNames = { "BusinessObjectA", "BusinessObjectB" }) | |
| static class DefaultBusinessService implements BusinessService { | |
| @Override | |
| @Cacheable | |
| public BusinessObjectA computeWithCacheA(String param) { | |
| try { | |
| TimeUnit.SECONDS.sleep(5); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| return new BusinessObjectA(param); | |
| } | |
| @Override | |
| @Cacheable | |
| public BusinessObjectB computeWithCacheB(String param) { | |
| try { | |
| TimeUnit.SECONDS.sleep(5); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| return new BusinessObjectB(param); | |
| } | |
| } | |
| static class BusinessObject { | |
| private final String value; | |
| public BusinessObject(String value) { | |
| this.value = value + System.currentTimeMillis(); | |
| } | |
| @Override | |
| public String toString() { | |
| return super.toString() + " value: " + value; | |
| } | |
| } | |
| public static class BusinessObjectA extends BusinessObject { | |
| public BusinessObjectA(String value) { | |
| super(value); | |
| } | |
| } | |
| public static class BusinessObjectB extends BusinessObject { | |
| public BusinessObjectB(String value) { | |
| super(value); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment