@Test public void cacheManager_configuredMultipleCachesWithStack_configuresCacheManager() throws Exception { //Arrange HttpServer httpServer = MetaDataServer.setupHttpServer(); HttpContext instanceIdHttpContext = httpServer.createContext("/latest/meta-data/instance-id", new MetaDataServer.HttpResponseWriterHandler("testInstanceId")); this.context = new AnnotationConfigApplicationContext(); this.context.register(MockCacheConfigurationWithStackCaches.class); this.context.register(ElastiCacheAutoConfiguration.class); //Act this.context.refresh(); //Assert CacheManager cacheManager = this.context.getBean(CachingConfigurer.class).cacheManager(); assertTrue(cacheManager.getCacheNames().contains("sampleCacheOneLogical")); assertTrue(cacheManager.getCacheNames().contains("sampleCacheTwoLogical")); assertEquals(2, cacheManager.getCacheNames().size()); httpServer.removeContext(instanceIdHttpContext); }
@Test public void cacheManager_configuredNoCachesWithNoStack_configuresNoCacheManager() throws Exception { //Arrange HttpServer httpServer = MetaDataServer.setupHttpServer(); HttpContext instanceIdHttpContext = httpServer.createContext("/latest/meta-data/instance-id", new MetaDataServer.HttpResponseWriterHandler("testInstanceId")); this.context = new AnnotationConfigApplicationContext(); this.context.register(ElastiCacheAutoConfiguration.class); //Act this.context.refresh(); //Assert CacheManager cacheManager = this.context.getBean(CachingConfigurer.class).cacheManager(); assertEquals(0, cacheManager.getCacheNames().size()); httpServer.removeContext(instanceIdHttpContext); }
@Test public void enableElasticache_configuredWithExplicitCluster_configuresExplicitlyConfiguredCaches() throws Exception { //Arrange //Act this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithExplicitStackConfiguration.class); //Assert CacheManager cacheManager = this.context.getBean(CachingConfigurer.class).cacheManager(); assertEquals(2, cacheManager.getCacheNames().size()); Cache firstCache = cacheManager.getCache("firstCache"); assertNotNull(firstCache.getName()); assertEquals(0, getExpirationFromCache(firstCache)); Cache secondCache = cacheManager.getCache("secondCache"); assertNotNull(secondCache.getName()); assertEquals(0, getExpirationFromCache(secondCache)); }
@Test public void enableElasticache_configuredWithExplicitClusterAndExpiration_configuresExplicitlyConfiguredCachesWithCustomExpirationTimes() throws Exception { //Arrange //Act this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithExplicitStackConfigurationAndExpiryTime.class); //Assert CacheManager cacheManager = this.context.getBean(CachingConfigurer.class).cacheManager(); assertEquals(2, cacheManager.getCacheNames().size()); Cache firstCache = cacheManager.getCache("firstCache"); assertNotNull(firstCache.getName()); assertEquals(23, getExpirationFromCache(firstCache)); Cache secondCache = cacheManager.getCache("secondCache"); assertNotNull(secondCache.getName()); assertEquals(42, getExpirationFromCache(secondCache)); }
@Test public void enableElasticache_configuredWithExplicitClusterAndExpiration_configuresExplicitlyConfiguredCachesWithMixedExpirationTimes() throws Exception { //Arrange //Act this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithExplicitStackConfigurationAndMixedExpiryTime.class); //Assert CacheManager cacheManager = this.context.getBean(CachingConfigurer.class).cacheManager(); assertEquals(2, cacheManager.getCacheNames().size()); Cache firstCache = cacheManager.getCache("firstCache"); assertNotNull(firstCache.getName()); assertEquals(12, getExpirationFromCache(firstCache)); Cache secondCache = cacheManager.getCache("secondCache"); assertNotNull(secondCache.getName()); assertEquals(42, getExpirationFromCache(secondCache)); }
@Test public void enableElasticache_configuredWithoutExplicitCluster_configuresImplicitlyConfiguredCaches() throws Exception { //Arrange //Act this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithNoExplicitStackConfiguration.class); //Assert CacheManager cacheManager = this.context.getBean(CachingConfigurer.class).cacheManager(); assertEquals(2, cacheManager.getCacheNames().size()); Cache firstCache = cacheManager.getCache("sampleCacheOneLogical"); assertNotNull(firstCache.getName()); assertEquals(0, getExpirationFromCache(firstCache)); Cache secondCache = cacheManager.getCache("sampleCacheTwoLogical"); assertNotNull(secondCache.getName()); assertEquals(0, getExpirationFromCache(secondCache)); }
@Test public void enableElasticache_configuredWithoutExplicitClusterButDefaultExpiryTime_configuresImplicitlyConfiguredCachesWithDefaultExpiryTimeOnAllCaches() throws Exception { //Arrange //Act this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithNoExplicitStackConfigurationAndDefaultExpiration.class); //Assert CacheManager cacheManager = this.context.getBean(CachingConfigurer.class).cacheManager(); assertEquals(2, cacheManager.getCacheNames().size()); Cache firstCache = cacheManager.getCache("sampleCacheOneLogical"); assertNotNull(firstCache.getName()); assertEquals(23, getExpirationFromCache(firstCache)); Cache secondCache = cacheManager.getCache("sampleCacheTwoLogical"); assertNotNull(secondCache.getName()); assertEquals(23, getExpirationFromCache(secondCache)); }
@Override protected void useCachingConfigurer(CachingConfigurer config) { super.useCachingConfigurer(config); if (config instanceof JCacheConfigurer) { this.exceptionCacheResolver = ((JCacheConfigurer) config).exceptionCacheResolver(); } }
@Bean public CachingConfigurer cachingConfigurer(AmazonElastiCache amazonElastiCache, ResourceIdResolver resourceIdResolver, List<CacheFactory> cacheFactories) { if (this.annotationAttributes != null && this.annotationAttributes.getAnnotationArray("value").length > 0) { return new ElastiCacheCacheConfigurer(amazonElastiCache, resourceIdResolver, getCacheNamesFromCacheClusterConfigs(this.annotationAttributes.getAnnotationArray("value")), cacheFactories); } else { return new ElastiCacheCacheConfigurer(amazonElastiCache, resourceIdResolver, getConfiguredCachesInStack(), cacheFactories); } }
@Bean public CachingConfigurer simpleCachingConfigurer(){ return new SimpleCachingConfigurer(); }