diff --git a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCache.java b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCache.java index 6b9faf1a9f102..0bec209e79f96 100644 --- a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCache.java +++ b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCache.java @@ -12,7 +12,9 @@ public interface RedisCache extends Cache { /** * When configured, gets the default type of the value stored in the cache. - * The configured type is used when no type is passed into the {@link #get(Object, Class, Function)}. + * The configured type is used in methods {@link #get(Object, Function)}, + * {@link #getAsync(Object, Function)}, {@link #getOrDefault(Object, Object)} + * and {@link #getOrNull(Object)}. * * @deprecated should have never been exposed publicly * @return the type, {@code null} if not configured or if not a {@code Class}. @@ -83,20 +85,89 @@ public interface RedisCache extends Cache { * @param the type of value * @return a Uni emitting {@code null} when the operation completes */ - default Uni put(K key, V value) { - return put(key, new Supplier() { - @Override - public V get() { - return value; - } - }); - } + Uni put(K key, V value); + /** + * Put a value in the cache. + * + * @param key the key + * @param supplier supplier of the value + * @param the type of key + * @param the type of value + * @return a Uni emitting {@code null} when the operation completes + */ Uni put(K key, Supplier supplier); + /** + * Returns {@link Uni} that completes with a value present in the cache under the given {@code key}. + * If there is no value in the cache under the key, the {@code Uni} completes with the given {@code defaultValue}. + * + * @param key the key + * @param defaultValue the default value + * @param the type of key + * @param the type of value + * @return a Uni emitting the value cached under {@code key}, or {@code defaultValue} if there is no cached value + */ Uni getOrDefault(K key, V defaultValue); + /** + * Returns {@link Uni} that completes with a value present in the cache under the given {@code key}. + * If there is no value in the cache under the key, the {@code Uni} completes with the given {@code defaultValue}. + * + * @param key the key + * @param clazz class of the value + * @param defaultValue the default value + * @param the type of key + * @param the type of value + * @return a Uni emitting the value cached under {@code key}, or {@code defaultValue} if there is no cached value + */ + Uni getOrDefault(K key, Class clazz, V defaultValue); + + /** + * Returns {@link Uni} that completes with a value present in the cache under the given {@code key}. + * If there is no value in the cache under the key, the {@code Uni} completes with the given {@code defaultValue}. + * + * @param key the key + * @param type type of the value + * @param defaultValue the default value + * @param the type of key + * @param the type of value + * @return a Uni emitting the value cached under {@code key}, or {@code defaultValue} if there is no cached value + */ + Uni getOrDefault(K key, TypeLiteral type, V defaultValue); + + /** + * Returns {@link Uni} that completes with a value present in the cache under the given {@code key}. + * If there is no value in the cache under the key, the {@code Uni} completes with {@code null}. + * + * @param key the key + * @param the type of key + * @param the type of value + * @return a Uni emitting the value cached under {@code key}, or {@code null} if there is no cached value + */ + Uni getOrNull(K key); + + /** + * Returns {@link Uni} that completes with a value present in the cache under the given {@code key}. + * If there is no value in the cache under the key, the {@code Uni} completes with {@code null}. + * + * @param key the key + * @param clazz the class of the value + * @param the type of key + * @param the type of value + * @return a Uni emitting the value cached under {@code key}, or {@code null} if there is no cached value + */ Uni getOrNull(K key, Class clazz); + /** + * Returns {@link Uni} that completes with a value present in the cache under the given {@code key}. + * If there is no value in the cache under the key, the {@code Uni} completes with {@code null}. + * + * @param key the key + * @param type the type of the value + * @param the type of key + * @param the type of value + * @return a Uni emitting the value cached under {@code key}, or {@code null} if there is no cached value + */ Uni getOrNull(K key, TypeLiteral type); } diff --git a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheImpl.java b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheImpl.java index 70d7764160c6d..8a67e3bb200c7 100644 --- a/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheImpl.java +++ b/extensions/redis-cache/runtime/src/main/java/io/quarkus/cache/redis/runtime/RedisCacheImpl.java @@ -126,11 +126,7 @@ public V get() { @Override public Uni get(K key, Function valueLoader) { - if (classOfValue == null) { - throw new UnsupportedOperationException("Cannot use `get` method without a default type configured. " - + "Consider using the `get` method accepting the type or configure the default type for the cache " - + getName()); - } + enforceDefaultType("get"); return get(key, classOfValue, valueLoader); } @@ -224,11 +220,7 @@ public Uni apply(Throwable e) { @Override public Uni getAsync(K key, Function> valueLoader) { - if (classOfValue == null) { - throw new UnsupportedOperationException("Cannot use `getAsync` method without a default type configured. " - + "Consider using the `getAsync` method accepting the type or configure the default type for the cache " - + getName()); - } + enforceDefaultType("getAsync"); return getAsync(key, classOfValue, valueLoader); } @@ -304,25 +296,46 @@ public Uni apply(RedisConnection connection) { }); } - private void enforceDefaultType() { + private void enforceDefaultType(String methodName) { if (classOfValue == null) { - throw new UnsupportedOperationException( - "Cannot execute the operation without the default type configured in cache " + cacheInfo.name); + throw new UnsupportedOperationException("Cannot use `" + methodName + "` method without a default type configured. " + + "Consider using the `" + methodName + "` method accepting the type or configure the default type for the cache " + + getName()); } } @Override public Uni getOrDefault(K key, V defaultValue) { - enforceDefaultType(); + enforceDefaultType("getOrDefault"); + return getOrDefault(key, classOfValue, defaultValue); + } + + @Override + public Uni getOrDefault(K key, Class clazz, V defaultValue) { + return getOrDefault(key, (Type) clazz, defaultValue); + } + + @Override + public Uni getOrDefault(K key, TypeLiteral type, V defaultValue) { + return getOrDefault(key, type.getType(), defaultValue); + } + + private Uni getOrDefault(K key, Type type, V defaultValue) { byte[] encodedKey = marshaller.encode(computeActualKey(encodeKey(key))); return withConnection(new Function>() { @Override public Uni apply(RedisConnection redisConnection) { - return doGet(redisConnection, encodedKey, classOfValue, marshaller); + return doGet(redisConnection, encodedKey, type, marshaller); } }).onItem().ifNull().continueWith(new StaticSupplier<>(defaultValue)); } + @Override + public Uni getOrNull(K key) { + enforceDefaultType("getOrNull"); + return getOrNull(key, classOfValue); + } + @Override public Uni getOrNull(K key, Class clazz) { return getOrNull(key, (Type) clazz); @@ -334,13 +347,11 @@ public Uni getOrNull(K key, TypeLiteral type) { } private Uni getOrNull(K key, Type type) { - enforceDefaultType(); byte[] encodedKey = marshaller.encode(computeActualKey(encodeKey(key))); return withConnection(new Function>() { @Override public Uni apply(RedisConnection redisConnection) { - // TODO maybe use `type` (if non-null?) instead of `classOfValue`? - return doGet(redisConnection, encodedKey, classOfValue, marshaller); + return doGet(redisConnection, encodedKey, type, marshaller); } }); }