Skip to content

Commit

Permalink
Add module cache/springto cover the quarkus-spring-cache extension
Browse files Browse the repository at this point in the history
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
Sgitario committed Aug 17, 2021
1 parent 8473b11 commit 235b2e4
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 18 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,13 @@ It covers different usages:
2. from a request scoped service
3. from a blocking endpoint
4. from a reactive endpoint

### `cache/spring`

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.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class ServiceWithCacheResourceIT {
private static final String RESOURCE_BLOCKING_API_PATH = "/api/blocking";
private static final String RESOURCE_REACTIVE_API_PATH = "/api/reactive";

private static final String PREFIX_ONE = "prefix1";
private static final String PREFIX_TWO = "prefix2";

/**
* Check whether the `@CacheResult` annotation works when used in a service.
*/
Expand Down Expand Up @@ -59,13 +62,13 @@ public void shouldGetDifferentValueWhenInvalidateCacheFromPath(String path) {
RESOURCE_REACTIVE_API_PATH })
public void shouldGetTheSameValueForSamePrefixesWhenGettingValueFromPath(String path) {
// We call the service endpoint
String value = getValueFromPathUsingPrefix(path, "prefix1");
String value = getValueFromPathUsingPrefix(path, PREFIX_ONE);

// At this point, the cache is populated and we should get the same value from the cache
assertEquals(value, getValueFromPathUsingPrefix(path, "prefix1"),
assertEquals(value, getValueFromPathUsingPrefix(path, PREFIX_ONE),
"Value was different which means cache is not working");
// But different value using another prefix
assertNotEquals(value, getValueFromPathUsingPrefix(path, "prefix2"),
assertNotEquals(value, getValueFromPathUsingPrefix(path, PREFIX_TWO),
"Value was equal which means @CacheKey didn't work");
}

Expand All @@ -77,16 +80,15 @@ public void shouldGetTheSameValueForSamePrefixesWhenGettingValueFromPath(String
RESOURCE_REACTIVE_API_PATH })
public void shouldGetTheSameValuesEvenAfterCallingToCacheInvalidateFromPath(String path) {
// We call the service endpoints
String valueOfPrefix1 = getValueFromPathUsingPrefix(path, "prefix1");
String valueOfPrefix2 = getValueFromPathUsingPrefix(path, "prefix2");
String valueOfPrefix1 = getValueFromPathUsingPrefix(path, PREFIX_ONE);
String valueOfPrefix2 = getValueFromPathUsingPrefix(path, PREFIX_TWO);

// invalidate the cache: this should not invalidate all the keys
invalidateCacheFromPath(path);

// At this point, the cache is populated and we should get the same value from the cache
assertEquals(valueOfPrefix1, getValueFromPathUsingPrefix(path, "prefix1"));
// But different value using another prefix
assertEquals(valueOfPrefix2, getValueFromPathUsingPrefix(path, "prefix2"));
// At this point, the cache is populated and we should get the same value for both prefixes
assertEquals(valueOfPrefix1, getValueFromPathUsingPrefix(path, PREFIX_ONE));
assertEquals(valueOfPrefix2, getValueFromPathUsingPrefix(path, PREFIX_TWO));
}

/**
Expand All @@ -97,16 +99,16 @@ public void shouldGetTheSameValuesEvenAfterCallingToCacheInvalidateFromPath(Stri
RESOURCE_REACTIVE_API_PATH })
public void shouldGetDifferentValueWhenInvalidateCacheOnlyForOnePrefixFromPath(String path) {
// We call the service endpoints
String valueOfPrefix1 = getValueFromPathUsingPrefix(path, "prefix1");
String valueOfPrefix2 = getValueFromPathUsingPrefix(path, "prefix2");
String valueOfPrefix1 = getValueFromPathUsingPrefix(path, PREFIX_ONE);
String valueOfPrefix2 = getValueFromPathUsingPrefix(path, PREFIX_TWO);

// invalidate the cache: this should not invalidate all the keys
invalidateCacheWithPrefixFromPath(path, "prefix1");
invalidateCacheWithPrefixFromPath(path, PREFIX_ONE);

// The cache was invalidated only for prefix1, so the value should be different
assertNotEquals(valueOfPrefix1, getValueFromPathUsingPrefix(path, "prefix1"));
assertNotEquals(valueOfPrefix1, getValueFromPathUsingPrefix(path, PREFIX_ONE));
// The cache was not invalidated for prefix2, so the value should be the same
assertEquals(valueOfPrefix2, getValueFromPathUsingPrefix(path, "prefix2"));
assertEquals(valueOfPrefix2, getValueFromPathUsingPrefix(path, PREFIX_TWO));
}

/**
Expand All @@ -118,16 +120,16 @@ public void shouldGetDifferentValueWhenInvalidateCacheOnlyForOnePrefixFromPath(S
public void shouldGetDifferentValueWhenInvalidateAllTheCacheFromPath(String path) {
// We call the service endpoints
String value = getFromPath(path);
String valueOfPrefix1 = getValueFromPathUsingPrefix(path, "prefix1");
String valueOfPrefix2 = getValueFromPathUsingPrefix(path, "prefix2");
String valueOfPrefix1 = getValueFromPathUsingPrefix(path, PREFIX_ONE);
String valueOfPrefix2 = getValueFromPathUsingPrefix(path, PREFIX_TWO);

// invalidate all the cache
invalidateCacheAllFromPath(path);

// Then, all the values should be different:
assertNotEquals(value, getFromPath(path));
assertNotEquals(valueOfPrefix1, getValueFromPathUsingPrefix(path, "prefix1"));
assertNotEquals(valueOfPrefix2, getValueFromPathUsingPrefix(path, "prefix2"));
assertNotEquals(valueOfPrefix1, getValueFromPathUsingPrefix(path, PREFIX_ONE));
assertNotEquals(valueOfPrefix2, getValueFromPathUsingPrefix(path, PREFIX_TWO));
}

private void invalidateCacheAllFromPath(String path) {
Expand Down
23 changes: 23 additions & 0 deletions cache/spring/pom.xml
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>
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 {
}
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;
}
}
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 {
}
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
}
}
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.
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 {
}
Loading

0 comments on commit 235b2e4

Please sign in to comment.