Skip to content

Commit

Permalink
Add coverage for infinispan cache extension
Browse files Browse the repository at this point in the history
  • Loading branch information
gtroitsk committed Aug 2, 2024
1 parent 8a998cb commit e1fa736
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,14 @@ It covers different usages:
3. from a blocking endpoint
4. from a reactive endpoint

### `cache/infinispan`
Verifies the `quarkus-cache` extension using `@CacheResult`, `@CacheInvalidate`, `@CacheInvalidateAll` and `@CacheKey`.
It covers different usages:
1. from an application scoped service
2. from a request scoped service

Also test POJOs as cache value and cache expiration.

### `cache/redis`

Verifies the `quarkus-redis-cache` extension using `@CacheResult`, `@CacheInvalidate`, `@CacheInvalidateAll` and `@CacheKey`.
Expand Down
28 changes: 28 additions & 0 deletions cache/infinispan/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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-infinispan</artifactId>
<packaging>jar</packaging>
<name>Quarkus QE TS: Cache: Infinispan</name>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-infinispan-cache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus.qe</groupId>
<artifactId>quarkus-test-service-infinispan</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.ts.cache.infinispan;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class ApplicationScopeService extends BaseServiceWithCache {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkus.ts.cache.infinispan;

import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.annotations.Proto;
import org.infinispan.protostream.annotations.ProtoSchema;

import io.quarkus.cache.CacheInvalidate;
import io.quarkus.cache.CacheInvalidateAll;
import io.quarkus.cache.CacheKey;
import io.quarkus.cache.CacheResult;

public abstract class BaseServiceWithCache {

private static final String CACHE_NAME = "service-cache";

private static int counter = 0;

@CacheResult(cacheName = CACHE_NAME)
public String getValue() {
return "Value: " + counter++;
}

@CacheInvalidate(cacheName = CACHE_NAME)
public void invalidate() {
// do nothing
}

@CacheResult(cacheName = CACHE_NAME)
public ExpensiveResponse getValueWithPrefix(@CacheKey String prefix) {
return new ExpensiveResponse(prefix + ": " + counter++);
}

@CacheInvalidate(cacheName = CACHE_NAME)
public void invalidateWithPrefix(@CacheKey String prefix) {
// do nothing
}

@CacheInvalidateAll(cacheName = CACHE_NAME)
public void invalidateAll() {
// do nothing
}

@Proto
public record ExpensiveResponse(String result) {
}

@ProtoSchema(includeClasses = { ExpensiveResponse.class })
interface Schema extends GeneratedSchema {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.ts.cache.infinispan;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import io.quarkus.cache.CacheKey;
import io.quarkus.cache.CacheResult;

@Path("/cache")
public class CacheExpirationResource {

@GET
@Path("/{key}")
@Produces(MediaType.TEXT_PLAIN)
@CacheResult(cacheName = "my-cache")
public String getCachedValue(@CacheKey String key) {
return "Value for key " + key + " at " + System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.ts.cache.infinispan;

import jakarta.enterprise.context.RequestScoped;

@RequestScoped
public class RequestScopeService extends BaseServiceWithCache {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.ts.cache.infinispan;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/services")
public class ServiceWithCacheResource {

public static final String APPLICATION_SCOPE_SERVICE_PATH = "application-scope";
public static final String REQUEST_SCOPE_SERVICE_PATH = "request-scope";

@Inject
ApplicationScopeService applicationScopeService;

@Inject
RequestScopeService requestScopeService;

@GET
@Path("/{service}")
@Produces(MediaType.TEXT_PLAIN)
public String getValueFromService(@PathParam("service") String service) {
return lookupServiceByPathParam(service).getValue();
}

@POST
@Path("/{service}/invalidate-cache")
public void invalidateCacheFromService(@PathParam("service") String service) {
lookupServiceByPathParam(service).invalidate();
}

@POST
@Path("/{service}/invalidate-cache-all")
public void invalidateCacheAllFromService(@PathParam("service") String service) {
lookupServiceByPathParam(service).invalidateAll();
}

@GET
@Path("/{service}/using-prefix/{prefix}")
@Produces(MediaType.TEXT_PLAIN)
public BaseServiceWithCache.ExpensiveResponse getValueUsingPrefixFromService(@PathParam("service") String service,
@PathParam("prefix") String prefix) {
return lookupServiceByPathParam(service).getValueWithPrefix(prefix);
}

@POST
@Path("/{service}/using-prefix/{prefix}/invalidate-cache")
public void invalidateCacheUsingPrefixFromService(@PathParam("service") String service,
@PathParam("prefix") String prefix) {
lookupServiceByPathParam(service).invalidateWithPrefix(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");
}
}
3 changes: 3 additions & 0 deletions cache/infinispan/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quarkus.infinispan-client.hosts=localhost:11222
quarkus.cache.infinispan.my-cache.lifespan=3s
quarkus.cache.infinispan.my-cache.max-idle=5s
Loading

0 comments on commit e1fa736

Please sign in to comment.