Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implementation for HazelcastCache #26434

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

import static com.hazelcast.test.HazelcastTestSupport.sleepMillis;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;


/**
Expand Down Expand Up @@ -158,6 +161,31 @@ void testCacheGetSynchronized() throws Exception {
}
}

@Test
void testCacheRetrieveWithNull() {
assertThrows(NullPointerException.class, () -> cache.retrieve(null));
}

@Test
void testCacheRetrieveWithRandomKey() throws ExecutionException, InterruptedException {
String key = createRandomKey();
CompletableFuture<?> resultFuture = cache.retrieve(key);

assertNotNull(resultFuture);
assertNull(resultFuture.get());
}

@Test
void testCacheRetrieveWithExistingKey() throws ExecutionException, InterruptedException {
String key = createRandomKey();
cache.put(key, "test");

CompletableFuture<?> resultFuture = cache.retrieve(key);

assertNotNull(resultFuture);
assertThat(resultFuture.get()).isEqualTo("test");
}

private String createRandomKey() {
return UUID.randomUUID().toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.cache.support.SimpleValueWrapper;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

Expand Down Expand Up @@ -99,6 +100,11 @@ public <T> T get(Object key, Callable<T> valueLoader) {
}
}

@Override
public CompletableFuture<?> retrieve(Object key) {
JamesHazelcast marked this conversation as resolved.
Show resolved Hide resolved
return this.map.getAsync(key).toCompletableFuture();
}

private <T> T loadValue(Object key, Callable<T> valueLoader) {
T value;
try {
Expand Down Expand Up @@ -152,7 +158,7 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
private Object lookup(Object key) {
if (readTimeout > 0) {
try {
return this.map.getAsync(key).toCompletableFuture().get(readTimeout, TimeUnit.MILLISECONDS);
return retrieve(key).get(readTimeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException te) {
throw new OperationTimeoutException(te.getMessage());
} catch (InterruptedException e) {
Expand Down
Loading