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 authored and fedinskiy committed Aug 8, 2024
1 parent 4341cd9 commit 89988dd
Show file tree
Hide file tree
Showing 14 changed files with 535 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,16 @@ It covers different usages:
3. from a blocking endpoint
4. from a reactive endpoint

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

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,52 @@
package io.quarkus.ts.cache.infinispan;

import java.util.concurrent.atomic.AtomicInteger;

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 final AtomicInteger counter = new AtomicInteger(0);

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

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

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

@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,57 @@
package io.quarkus.ts.cache.infinispan;

import java.util.concurrent.atomic.AtomicInteger;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;

import io.quarkus.cache.CacheInvalidate;
import io.quarkus.cache.CacheInvalidateAll;
import io.quarkus.cache.CacheKey;
import io.quarkus.cache.CacheResult;
import io.smallrye.common.annotation.Blocking;

@Blocking
@Path("/api/blocking")
public class BlockingWithCacheResource {

private static final String CACHE_NAME = "api-blocking-cache";

private final AtomicInteger counter = new AtomicInteger(0);

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

@POST
@Path("/invalidate-cache")
@CacheInvalidate(cacheName = CACHE_NAME)
public void invalidate() {
// do nothing
}

@GET
@Path("/using-prefix/{prefix}")
@CacheResult(cacheName = CACHE_NAME)
public String getValueWithPrefix(@PathParam("prefix") @CacheKey String prefix) {
return prefix + ": " + counter.getAndIncrement();
}

@POST
@Path("/using-prefix/{prefix}/invalidate-cache")
@CacheInvalidate(cacheName = CACHE_NAME)
public void invalidateWithPrefix(@PathParam("prefix") @CacheKey String prefix) {
// do nothing
}

@POST
@Path("/invalidate-cache-all")
@CacheInvalidateAll(cacheName = CACHE_NAME)
public void invalidateAll() {
// do nothing
}
}
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 = "expiring-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,58 @@
package io.quarkus.ts.cache.infinispan;

import java.util.concurrent.atomic.AtomicInteger;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;

import io.quarkus.cache.CacheInvalidate;
import io.quarkus.cache.CacheInvalidateAll;
import io.quarkus.cache.CacheKey;
import io.quarkus.cache.CacheResult;
import io.smallrye.common.annotation.NonBlocking;
import io.smallrye.mutiny.Uni;

@NonBlocking
@Path("/api/reactive")
public class ReactiveWithCacheResource {

private static final String CACHE_NAME = "api-reactive-cache";

private final AtomicInteger counter = new AtomicInteger(0);

@GET
@CacheResult(cacheName = CACHE_NAME)
public Uni<String> getValue() {
return Uni.createFrom().item("Value: " + counter.getAndIncrement());
}

@POST
@Path("/invalidate-cache")
@CacheInvalidate(cacheName = CACHE_NAME)
public Uni<Void> invalidate() {
return Uni.createFrom().nullItem();
}

@GET
@Path("/using-prefix/{prefix}")
@CacheResult(cacheName = CACHE_NAME)
public Uni<String> getValueWithPrefix(@PathParam("prefix") @CacheKey String prefix) {
return Uni.createFrom().item(prefix + ": " + counter.getAndIncrement());
}

@POST
@Path("/using-prefix/{prefix}/invalidate-cache")
@CacheInvalidate(cacheName = CACHE_NAME)
public Uni<Void> invalidateWithPrefix(@PathParam("prefix") @CacheKey String prefix) {
return Uni.createFrom().nullItem();
}

@POST
@Path("/invalidate-cache-all")
@CacheInvalidateAll(cacheName = CACHE_NAME)
public Uni<Void> invalidateAll() {
return Uni.createFrom().nullItem();
}
}
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.expiring-cache.lifespan=3s
quarkus.cache.infinispan.expiring-cache.max-idle=5s
Loading

0 comments on commit 89988dd

Please sign in to comment.