forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redis Cache: support more complex types
This includes generic types, like `List<String>`, array types, like `int[]`, and more. The type parser in this commit is reasonably complete and might be moved to the `core` module and made `public` if needed. Omitting type variables should not be an issue; nested types might, but adding support for them should be doable later.
- Loading branch information
Showing
10 changed files
with
620 additions
and
60 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
28 changes: 28 additions & 0 deletions
28
...ache/deployment/src/test/java/io/quarkus/cache/redis/deployment/ComplexCachedService.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,28 @@ | ||
package io.quarkus.cache.redis.deployment; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import io.quarkus.cache.CacheResult; | ||
|
||
@ApplicationScoped | ||
public class ComplexCachedService { | ||
static final String CACHE_NAME_GENERIC = "test-cache-generic"; | ||
static final String CACHE_NAME_ARRAY = "test-cache-array"; | ||
|
||
@CacheResult(cacheName = CACHE_NAME_GENERIC) | ||
public List<String> genericReturnType(String key) { | ||
return List.of(UUID.randomUUID().toString(), UUID.randomUUID().toString()); | ||
} | ||
|
||
@CacheResult(cacheName = CACHE_NAME_ARRAY) | ||
public int[] arrayReturnType(String key) { | ||
int[] result = new int[2]; | ||
result[0] = ThreadLocalRandom.current().nextInt(); | ||
result[1] = ThreadLocalRandom.current().nextInt(); | ||
return result; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...eployment/src/test/java/io/quarkus/cache/redis/deployment/ComplexTypesRedisCacheTest.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,78 @@ | ||
package io.quarkus.cache.redis.deployment; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.redis.datasource.RedisDataSource; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ComplexTypesRedisCacheTest { | ||
private static final String KEY_1 = "1"; | ||
private static final String KEY_2 = "2"; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.withApplicationRoot(jar -> jar.addClasses(ComplexCachedService.class, TestUtil.class)); | ||
|
||
@Inject | ||
ComplexCachedService cachedService; | ||
|
||
@Test | ||
public void testGeneric() { | ||
RedisDataSource redisDataSource = Arc.container().select(RedisDataSource.class).get(); | ||
List<String> allKeysAtStart = TestUtil.allRedisKeys(redisDataSource); | ||
|
||
// STEP 1 | ||
// Action: @CacheResult-annotated method call. | ||
// Expected effect: method invoked and result cached. | ||
// Verified by: STEP 2. | ||
List<String> value1 = cachedService.genericReturnType(KEY_1); | ||
List<String> newKeys = TestUtil.allRedisKeys(redisDataSource); | ||
assertEquals(allKeysAtStart.size() + 1, newKeys.size()); | ||
assertThat(newKeys).contains(expectedCacheKey(ComplexCachedService.CACHE_NAME_GENERIC, KEY_1)); | ||
|
||
// STEP 2 | ||
// Action: same call as STEP 1. | ||
// Expected effect: method not invoked and result coming from the cache. | ||
// Verified by: same object reference between STEPS 1 and 2 results. | ||
List<String> value2 = cachedService.genericReturnType(KEY_1); | ||
assertEquals(value1, value2); | ||
assertEquals(allKeysAtStart.size() + 1, TestUtil.allRedisKeys(redisDataSource).size()); | ||
} | ||
|
||
@Test | ||
public void testArray() { | ||
RedisDataSource redisDataSource = Arc.container().select(RedisDataSource.class).get(); | ||
List<String> allKeysAtStart = TestUtil.allRedisKeys(redisDataSource); | ||
|
||
// STEP 1 | ||
// Action: @CacheResult-annotated method call. | ||
// Expected effect: method invoked and result cached. | ||
// Verified by: STEP 2. | ||
int[] value1 = cachedService.arrayReturnType(KEY_2); | ||
List<String> newKeys = TestUtil.allRedisKeys(redisDataSource); | ||
assertEquals(allKeysAtStart.size() + 1, newKeys.size()); | ||
assertThat(newKeys).contains(expectedCacheKey(ComplexCachedService.CACHE_NAME_ARRAY, KEY_2)); | ||
|
||
// STEP 2 | ||
// Action: same call as STEP 1. | ||
// Expected effect: method not invoked and result coming from the cache. | ||
// Verified by: same object reference between STEPS 1 and 2 results. | ||
int[] value2 = cachedService.arrayReturnType(KEY_2); | ||
assertArrayEquals(value1, value2); | ||
assertEquals(allKeysAtStart.size() + 1, TestUtil.allRedisKeys(redisDataSource).size()); | ||
} | ||
|
||
private static String expectedCacheKey(String cacheName, String key) { | ||
return "cache:" + cacheName + ":" + key; | ||
} | ||
} |
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
Oops, something went wrong.