From 97f17557cac68c1f8cb4009e3d1799c63613ec3f Mon Sep 17 00:00:00 2001 From: John Blum Date: Mon, 6 May 2019 12:47:55 -0700 Subject: [PATCH] Create Spring Geode Sample application for the Look-Aside Caching pattern. --- ...ing-geode-samples-caching-lookaside.gradle | 24 +++++++ .../BootGeodeLookAsideCachingApplication.java | 39 +++++++++++ .../lookaside/config/GeodeConfiguration.java | 36 ++++++++++ .../contoller/CounterController.java | 69 +++++++++++++++++++ .../lookaside/service/CounterService.java | 67 ++++++++++++++++++ 5 files changed, 235 insertions(+) create mode 100644 spring-geode-samples/caching/look-aside/spring-geode-samples-caching-lookaside.gradle create mode 100644 spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/BootGeodeLookAsideCachingApplication.java create mode 100644 spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/config/GeodeConfiguration.java create mode 100644 spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/contoller/CounterController.java create mode 100644 spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/service/CounterService.java diff --git a/spring-geode-samples/caching/look-aside/spring-geode-samples-caching-lookaside.gradle b/spring-geode-samples/caching/look-aside/spring-geode-samples-caching-lookaside.gradle new file mode 100644 index 00000000..c2767a1d --- /dev/null +++ b/spring-geode-samples/caching/look-aside/spring-geode-samples-caching-lookaside.gradle @@ -0,0 +1,24 @@ +apply plugin: 'io.spring.convention.spring-sample-boot' + +description = "Spring Geode Sample demonstrating Spring's Cache Abstraction using Apache Geode as the caching provider." + +dependencies { + + compile project(":spring-geode-starter") + + compile ("org.springframework.boot:spring-boot-starter-web") { + exclude group: "org.apache.logging.log4j", module: "log4j-to-slf4j" + } + + runtime "javax.cache.cache-api" + runtime "org.springframework.shell:spring-shell" + + runtime ("org.springframework.boot:spring-boot-starter-jetty") { + exclude group: "org.apache.logging.log4j", module: "log4j-to-slf4j" + } + +} + +bootJar { + mainClassName = 'example.app.temp.geode.client.BootGeodeClientApplication' +} diff --git a/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/BootGeodeLookAsideCachingApplication.java b/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/BootGeodeLookAsideCachingApplication.java new file mode 100644 index 00000000..d60efef3 --- /dev/null +++ b/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/BootGeodeLookAsideCachingApplication.java @@ -0,0 +1,39 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package example.app.caching.lookaside; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +/** + * The BootGeodeLookAsideCachingApplication class... + * + * @author John Blum + * @since 1.0.0 + */ +@SpringBootApplication +public class BootGeodeLookAsideCachingApplication { + + public static void main(String[] args) { + + new SpringApplicationBuilder(BootGeodeLookAsideCachingApplication.class) + .web(WebApplicationType.SERVLET) + .build() + .run(args); + } +} diff --git a/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/config/GeodeConfiguration.java b/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/config/GeodeConfiguration.java new file mode 100644 index 00000000..09a69a7e --- /dev/null +++ b/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/config/GeodeConfiguration.java @@ -0,0 +1,36 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package example.app.caching.lookaside.config; + +import org.apache.geode.cache.client.ClientRegionShortcut; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.gemfire.config.annotation.EnableCachingDefinedRegions; +import org.springframework.data.gemfire.config.annotation.EnableLogging; + +/** + * The GeodeConfiguration class... + * + * @author John Blum + * @since 1.0.0 + */ +@SuppressWarnings("unused") +// tag::class[] +@Configuration +@EnableLogging(logLevel = "error") +@EnableCachingDefinedRegions(clientRegionShortcut = ClientRegionShortcut.LOCAL) +public class GeodeConfiguration { } +// end::class[] diff --git a/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/contoller/CounterController.java b/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/contoller/CounterController.java new file mode 100644 index 00000000..b046cc84 --- /dev/null +++ b/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/contoller/CounterController.java @@ -0,0 +1,69 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package example.app.caching.lookaside.contoller; + +import org.springframework.util.Assert; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import example.app.caching.lookaside.service.CounterService; + +/** + * The CounterController class... + * + * @author John Blum + * @since 1.0.0 + */ +@SuppressWarnings("unused") +// tag::class[] +@RestController +public class CounterController { + + private static final String HEADER_ONE = "

%s

"; + + private final CounterService counterService; + + public CounterController(CounterService counterService) { + + Assert.notNull(counterService, "CounterService is required"); + + this.counterService = counterService; + } + + @GetMapping("/ping") + public String ping() { + return String.format(HEADER_ONE, "PONG"); + } + + @GetMapping("counter/{name}") + public String getCount(@PathVariable("name") String counterName) { + return String.format(HEADER_ONE, this.counterService.getCount(counterName)); + } + + @GetMapping("counter/{name}/cached") + public String getCachedCount(@PathVariable("name") String counterName) { + return String.format(HEADER_ONE, this.counterService.getCachedCount(counterName)); + } + + @GetMapping("counter/{name}/reset") + public String resetCounter(@PathVariable("name") String counterName) { + this.counterService.resetCounter(counterName); + return String.format(HEADER_ONE, "0"); + } +} +// end::class[] diff --git a/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/service/CounterService.java b/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/service/CounterService.java new file mode 100644 index 00000000..315e2af4 --- /dev/null +++ b/spring-geode-samples/caching/look-aside/src/main/java/example/app/caching/lookaside/service/CounterService.java @@ -0,0 +1,67 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package example.app.caching.lookaside.service; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicLong; + +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +/** + * The CounterService class... + * + * @author John Blum + * @since 1.0.0 + */ +// tag::class[] +@Service +public class CounterService { + + private ConcurrentMap namedCounterMap = new ConcurrentHashMap<>(); + + @Cacheable("Counters") + public long getCachedCount(String counterName) { + return getCount(counterName); + } + + @CachePut("Counters") + public long getCount(String counterName) { + + AtomicLong counter = this.namedCounterMap.get(counterName); + + if (counter == null) { + + counter = new AtomicLong(0L); + + AtomicLong existingCounter = this.namedCounterMap.putIfAbsent(counterName, counter); + + counter = existingCounter != null ? existingCounter : counter; + } + + return counter.incrementAndGet(); + } + + @CacheEvict("Counters") + public void resetCounter(String counterName) { + this.namedCounterMap.remove(counterName); + } +} +// end::class[]