Skip to content

Commit

Permalink
Merge pull request #30316 from mkouba/cache-invalidate-predicate
Browse files Browse the repository at this point in the history
Cache programmatic API - invalidate entries whose keys match a predicate
  • Loading branch information
mkouba authored Jan 12, 2023
2 parents bb8a696 + 7162d83 commit f8f7c49
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.cache.test.runtime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
Expand Down Expand Up @@ -212,10 +213,22 @@ public void testPutShouldPopulateCache() {
}
}

@Test
public void testInvalidatePredicate() throws Exception {
String key = "bravo";
String val = cache.get(key, k -> "charlie").await().indefinitely();
assertEquals("charlie", val);
assertKeySetContains(key);
assertGetIfPresent(key, val);
cache.invalidateIf(k -> k.equals("bravo")).await().indefinitely();
assertFalse(cache.as(CaffeineCache.class).keySet().contains(key));
assertNull(cache.as(CaffeineCache.class).getIfPresent(key));
}

private void assertKeySetContains(Object... expectedKeys) {
Set<Object> expectedKeySet = new HashSet<>(Arrays.asList(expectedKeys));
Set<Object> actualKeySet = cache.as(CaffeineCache.class).keySet();
assertEquals(expectedKeySet, actualKeySet);
assertTrue(actualKeySet.containsAll(expectedKeySet));
}

private void assertGetIfPresent(Object key, Object value) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.function.Function;
import java.util.function.Predicate;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand Down Expand Up @@ -75,6 +76,11 @@ public Uni<Void> invalidateAll() {
throw new UnsupportedOperationException("This method is not tested here");
}

@Override
public Uni<Void> invalidateIf(Predicate<Object> predicate) {
throw new UnsupportedOperationException("This method is not tested here");
}

@Override
public <T extends Cache> T as(Class<T> cacheType) {
throw new UnsupportedOperationException("This method is not tested here");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.cache;

import java.util.function.Function;
import java.util.function.Predicate;

import io.smallrye.mutiny.Uni;

Expand Down Expand Up @@ -54,6 +55,13 @@ public interface Cache {
*/
Uni<Void> invalidateAll();

/**
* Removes all cache entries whose keys match the given predicate.
*
* @param predicate
*/
Uni<Void> invalidateIf(Predicate<Object> predicate);

/**
* Returns this cache as an instance of the provided type if possible.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.jboss.logging.Logger;
Expand Down Expand Up @@ -194,6 +195,17 @@ public Void get() {
});
}

@Override
public Uni<Void> invalidateIf(Predicate<Object> predicate) {
return Uni.createFrom().item(new Supplier<Void>() {
@Override
public Void get() {
cache.asMap().keySet().removeIf(predicate);
return null;
}
});
}

@Override
public Uni<Void> replaceUniValue(Object key, Object emittedValue) {
return Uni.createFrom().item(new Supplier<Void>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.cache.runtime.noop;

import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import io.quarkus.cache.runtime.AbstractCache;
Expand Down Expand Up @@ -39,6 +40,11 @@ public Uni<Void> invalidateAll() {
return Uni.createFrom().voidItem();
}

@Override
public Uni<Void> invalidateIf(Predicate<Object> predicate) {
return Uni.createFrom().voidItem();
}

@Override
public Uni<Void> replaceUniValue(Object key, Object emittedValue) {
return Uni.createFrom().voidItem();
Expand Down

0 comments on commit f8f7c49

Please sign in to comment.