-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
caffeine: register CacheLoader and implementors for reflection #12074
- Loading branch information
1 parent
e887e43
commit 46c3d0e
Showing
4 changed files
with
106 additions
and
0 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
64 changes: 64 additions & 0 deletions
64
integration-tests/cache/src/main/java/io/quarkus/it/cache/CaffeineResource.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,64 @@ | ||
package io.quarkus.it.cache; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.json.Json; | ||
import javax.json.JsonObject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import com.github.benmanes.caffeine.cache.CacheLoader; | ||
|
||
@ApplicationScoped | ||
@Path("/caffeine") | ||
public class CaffeineResource { | ||
/* | ||
* Replicates the behavior of com.github.benmanes.caffeine.cache.LocalLoadingCache.java | ||
* | ||
* https://github.com/ben-manes/caffeine/blob/37f6ad303eb4474cd8a644551d528dfda37c5bfc/caffeine/src/main/java/com/github/ | ||
* benmanes/caffeine/cache/LocalLoadingCache.java#L176-L185 | ||
*/ | ||
public static boolean hasLoadAll(CacheLoader<String, String> cl) { | ||
try { | ||
Method classLoadAll = cl.getClass().getMethod("loadAll", Iterable.class); | ||
Method defaultLoadAll = CacheLoader.class.getMethod("loadAll", Iterable.class); | ||
return !classLoadAll.equals(defaultLoadAll); | ||
} catch (NoSuchMethodException | SecurityException e) { | ||
return false; | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/hasLoadAll") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public JsonObject hasLoadAll() { | ||
return Json.createObjectBuilder() | ||
.add("loader", hasLoadAll(new MyCacheLoader())) | ||
.add("bulk-loader", hasLoadAll(new MyBulkCacheLoader())) | ||
.build(); | ||
} | ||
|
||
public static class MyCacheLoader implements CacheLoader<String, String> { | ||
@Override | ||
public String load(String unused) throws Exception { | ||
return null; | ||
} | ||
} | ||
|
||
public static class MyBulkCacheLoader implements CacheLoader<String, String> { | ||
@Override | ||
public String load(String unused) throws Exception { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Map<String, String> loadAll(Iterable<? extends String> unused) throws Exception { | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
integration-tests/cache/src/test/java/io/quarkus/it/cache/CaffeineITCase.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.it.cache; | ||
|
||
import io.quarkus.test.junit.NativeImageTest; | ||
|
||
@NativeImageTest | ||
public class CaffeineITCase extends CaffeineTestCase { | ||
} |
19 changes: 19 additions & 0 deletions
19
integration-tests/cache/src/test/java/io/quarkus/it/cache/CaffeineTestCase.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,19 @@ | ||
package io.quarkus.it.cache; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class CaffeineTestCase { | ||
|
||
@Test | ||
public void test() { | ||
given().when().get("/caffeine/hasLoadAll").then().statusCode(200).body( | ||
"loader", is(false), | ||
"bulk-loader", is(true)); | ||
} | ||
} |