Create Spring Geode Sample application for the Look-Aside Caching pattern.

This commit is contained in:
John Blum
2019-05-06 12:47:55 -07:00
parent 938e247dc8
commit 97f17557ca
5 changed files with 235 additions and 0 deletions

View File

@@ -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'
}

View File

@@ -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);
}
}

View File

@@ -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[]

View File

@@ -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 = "<h1>%s</h1>";
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[]

View File

@@ -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<String, AtomicLong> 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[]