-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module
cache/spring
to cover the quarkus-spring-cache
extension
Verifies the `quarkus-spring-cache` extension using `@Cacheable`, `@CacheEvict` and `@CachePut`. It covers different usages: 1. from an application scoped service 2. from a request scoped service 3. from a REST controller endpoint (using `@RestController) More information about this extension in https://quarkus.io/guides/spring-cache.
- Loading branch information
Showing
12 changed files
with
446 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.quarkus.ts.qe</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<artifactId>cache-spring</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Quarkus QE TS: Cache: Spring</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-spring-cache</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-spring-web</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
7 changes: 7 additions & 0 deletions
7
cache/spring/src/main/java/io/quarkus/ts/cache/spring/ApplicationScopeService.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.quarkus.ts.cache.spring; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class ApplicationScopeService extends BaseServiceWithCache { | ||
} |
48 changes: 48 additions & 0 deletions
48
cache/spring/src/main/java/io/quarkus/ts/cache/spring/BaseServiceWithCache.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.quarkus.ts.cache.spring; | ||
|
||
import org.springframework.cache.annotation.CacheEvict; | ||
import org.springframework.cache.annotation.CachePut; | ||
import org.springframework.cache.annotation.Cacheable; | ||
|
||
public abstract class BaseServiceWithCache { | ||
|
||
public static final String DEFAULT_CACHE_VALUE = "reset"; | ||
private static final String CACHE_NAME = "service-cache"; | ||
|
||
private static int counter = 0; | ||
|
||
@Cacheable(CACHE_NAME) | ||
public String getValue() { | ||
return "Value: " + counter++; | ||
} | ||
|
||
@CacheEvict(CACHE_NAME) | ||
public void invalidate() { | ||
// do nothing | ||
} | ||
|
||
@Cacheable(CACHE_NAME) | ||
public String getValueWithPrefix(String prefix) { | ||
return prefix + ": " + counter++; | ||
} | ||
|
||
@CacheEvict(CACHE_NAME) | ||
public void invalidateWithPrefix(String prefix) { | ||
// do nothing | ||
} | ||
|
||
@CacheEvict(value = CACHE_NAME, allEntries = true) | ||
public void invalidateAll() { | ||
// do nothing | ||
} | ||
|
||
@CachePut(CACHE_NAME) | ||
public String resetCache() { | ||
return DEFAULT_CACHE_VALUE; | ||
} | ||
|
||
@CachePut(CACHE_NAME) | ||
public String resetCacheWithPrefix(String prefix) { | ||
return DEFAULT_CACHE_VALUE; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
cache/spring/src/main/java/io/quarkus/ts/cache/spring/RequestScopeService.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.quarkus.ts.cache.spring; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.context.annotation.RequestScope; | ||
|
||
@Service | ||
@RequestScope | ||
public class RequestScopeService extends BaseServiceWithCache { | ||
} |
61 changes: 61 additions & 0 deletions
61
cache/spring/src/main/java/io/quarkus/ts/cache/spring/RestControllerWithCacheResource.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.quarkus.ts.cache.spring; | ||
|
||
import org.springframework.cache.annotation.CacheEvict; | ||
import org.springframework.cache.annotation.CachePut; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api") | ||
public class RestControllerWithCacheResource { | ||
|
||
private static final String CACHE_NAME = "api-cache"; | ||
|
||
private static int counter = 0; | ||
|
||
@GetMapping | ||
@Cacheable(CACHE_NAME) | ||
public String getValue() { | ||
return "Value: " + counter++; | ||
} | ||
|
||
@PostMapping("/invalidate-cache") | ||
@CacheEvict(CACHE_NAME) | ||
public void invalidate() { | ||
// do nothing | ||
} | ||
|
||
@PostMapping("/cache/reset") | ||
@CachePut(CACHE_NAME) | ||
public String resetValueInCache() { | ||
return BaseServiceWithCache.DEFAULT_CACHE_VALUE; | ||
} | ||
|
||
@GetMapping("/using-prefix/{prefix}") | ||
@Cacheable(CACHE_NAME) | ||
public String getValueWithPrefix(@PathVariable("prefix") String prefix) { | ||
return prefix + ": " + counter++; | ||
} | ||
|
||
@PostMapping("/using-prefix/{prefix}/invalidate-cache") | ||
@CacheEvict(CACHE_NAME) | ||
public void invalidateWithPrefix(@PathVariable("prefix") String prefix) { | ||
// do nothing | ||
} | ||
|
||
@PostMapping("/using-prefix/{prefix}/cache/reset") | ||
@CachePut(CACHE_NAME) | ||
public String resetValueInCacheWithPrefix(@PathVariable("prefix") String prefix) { | ||
return BaseServiceWithCache.DEFAULT_CACHE_VALUE; | ||
} | ||
|
||
@PostMapping("/invalidate-cache-all") | ||
@CacheEvict(value = CACHE_NAME, allEntries = true) | ||
public void invalidateAll() { | ||
// do nothing | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
cache/spring/src/main/java/io/quarkus/ts/cache/spring/ServiceWithCacheResource.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.quarkus.ts.cache.spring; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/services") | ||
public class ServiceWithCacheResource { | ||
|
||
public static final String APPLICATION_SCOPE_SERVICE_PATH = "application-scope"; | ||
public static final String REQUEST_SCOPE_SERVICE_PATH = "request-scope"; | ||
|
||
@Autowired | ||
ApplicationScopeService applicationScopeService; | ||
|
||
@Autowired | ||
RequestScopeService requestScopeService; | ||
|
||
@GetMapping("/{service}") | ||
public String getValueFromService(@PathVariable("service") String service) { | ||
return lookupServiceByPathParam(service).getValue(); | ||
} | ||
|
||
@PostMapping("/{service}/cache/reset") | ||
public String resetCacheFromService(@PathVariable("service") String service) { | ||
return lookupServiceByPathParam(service).resetCache(); | ||
} | ||
|
||
@PostMapping("/{service}/invalidate-cache") | ||
public void invalidateCacheFromService(@PathVariable("service") String service) { | ||
lookupServiceByPathParam(service).invalidate(); | ||
} | ||
|
||
@PostMapping("/{service}/invalidate-cache-all") | ||
public void invalidateCacheAllFromService(@PathVariable("service") String service) { | ||
lookupServiceByPathParam(service).invalidateAll(); | ||
} | ||
|
||
@GetMapping("/{service}/using-prefix/{prefix}") | ||
public String getValueUsingPrefixFromService(@PathVariable("service") String service, | ||
@PathVariable("prefix") String prefix) { | ||
return lookupServiceByPathParam(service).getValueWithPrefix(prefix); | ||
} | ||
|
||
@PostMapping("/{service}/using-prefix/{prefix}/invalidate-cache") | ||
public void invalidateCacheUsingPrefixFromService(@PathVariable("service") String service, | ||
@PathVariable("prefix") String prefix) { | ||
lookupServiceByPathParam(service).invalidateWithPrefix(prefix); | ||
} | ||
|
||
@PostMapping("/{service}/using-prefix/{prefix}/cache/reset") | ||
public String resetCacheUsingPrefixFromService(@PathVariable("service") String service, | ||
@PathVariable("prefix") String prefix) { | ||
return lookupServiceByPathParam(service).resetCacheWithPrefix(prefix); | ||
} | ||
|
||
private BaseServiceWithCache lookupServiceByPathParam(String service) { | ||
if (APPLICATION_SCOPE_SERVICE_PATH.equals(service)) { | ||
return applicationScopeService; | ||
} else if (REQUEST_SCOPE_SERVICE_PATH.equals(service)) { | ||
return requestScopeService; | ||
} | ||
|
||
throw new IllegalArgumentException("Service " + service + " is not recognised"); | ||
} | ||
} |
Empty file.
7 changes: 7 additions & 0 deletions
7
.../spring/src/test/java/io/quarkus/ts/cache/spring/OpenShiftServiceWithCacheResourceIT.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.quarkus.ts.cache.spring; | ||
|
||
import io.quarkus.test.scenarios.OpenShiftScenario; | ||
|
||
@OpenShiftScenario | ||
public class OpenShiftServiceWithCacheResourceIT extends ServiceWithCacheResourceIT { | ||
} |
Oops, something went wrong.