Skip to content

Commit

Permalink
Merge pull request #27488 from gastaldi/cache_put
Browse files Browse the repository at this point in the history
Introduce `CaffeineCache#put(Object,CompletableFuture)`
  • Loading branch information
gastaldi authored Aug 26, 2022
2 parents 0926e68 + 8a7ddf2 commit a1e8eb3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/src/main/asciidoc/cache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,36 @@ public class CacheKeysService {
}
----

=== Populating a `CaffeineCache`

You can populate a `CaffeineCache` using the `CaffeineCache#put(Object, CompletableFuture)` method.
This method associates the `CompletableFuture` with the given key in the cache. If the cache previously contained a value associated with the key, the old value is replaced by this `CompletableFuture`. If the asynchronous computation fails, the entry will be automatically removed.

[source,java]
----
package org.acme.cache;
import javax.enterprise.context.ApplicationScoped;
import io.quarkus.cache.Cache;
import io.quarkus.cache.CacheName;
import io.quarkus.cache.CaffeineCache;
import java.util.concurrent.CompletableFuture;
@ApplicationScoped
public class CacheService {
@CacheName("my-cache")
Cache cache;
@PostConstruct
public void initialize() {
cache.as(CaffeineCache.class).put("foo", CompletableFuture.completedFuture("bar"));
}
}
----

=== Retrieving a value if a key is present from a `CaffeineCache`

The cache value from a specific `CaffeineCache` can be retrieved if present as shown below.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import javax.enterprise.context.Dependent;
import javax.inject.Inject;
Expand Down Expand Up @@ -199,6 +200,18 @@ public void testExceptionInValueLoader() throws Exception {
assertGetIfPresentMissingKey(key);
}

@Test
public void testPutShouldPopulateCache() {
CaffeineCache caffeineCache = cache.as(CaffeineCache.class);
try {
caffeineCache.put("foo", CompletableFuture.completedFuture("bar"));
assertEquals("bar", caffeineCache.get("foo", Function.identity()).await().indefinitely());
} finally {
// invalidate to remove side effects in other tests
cache.invalidate("foo").await().indefinitely();
}
}

private void assertKeySetContains(Object... expectedKeys) {
Set<Object> expectedKeySet = new HashSet<>(Arrays.asList(expectedKeys));
Set<Object> actualKeySet = cache.as(CaffeineCache.class).keySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public interface CaffeineCache extends Cache {

Expand All @@ -26,4 +27,18 @@ public interface CaffeineCache extends Cache {
* @see com.github.benmanes.caffeine.cache.AsyncCache#getIfPresent(Object)
*/
<V> CompletableFuture<V> getIfPresent(Object key);

/**
* Associates {@code value} with {@code key} in this cache. If the cache previously contained a
* value associated with {@code key}, the old value is replaced by {@code value}. If the
* asynchronous computation fails, the entry will be automatically removed.
* <p>
* Prefer {@link #get(Object, Function)} when using the conventional "if cached, return; otherwise
* create, cache and return" pattern.
*
* @param key key with which the specified value is to be associated
* @param valueFuture value to be associated with the specified key
* @throws NullPointerException if the specified key or value is null
*/
<V> void put(Object key, CompletableFuture<V> valueFuture);
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ public Set<Object> keySet() {
return Collections.unmodifiableSet(new HashSet<>(cache.asMap().keySet()));
}

@Override
public <V> void put(Object key, CompletableFuture<V> valueFuture) {
cache.put(key, (CompletableFuture<Object>) valueFuture);
}

// For testing purposes only.
public CaffeineCacheInfo getCacheInfo() {
return cacheInfo;
Expand Down

0 comments on commit a1e8eb3

Please sign in to comment.