mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
CE-604 expose fetching MemoizedResult (so nulls can be cached and differentiated from not-founds)
This commit is contained in:
@ -67,7 +67,26 @@ public class Memoization<K, V>
|
||||
{
|
||||
if(result.getTime().isAfter(Instant.now().minus(timeout)))
|
||||
{
|
||||
return (Optional.of(result.getResult()));
|
||||
return (Optional.ofNullable(result.getResult()));
|
||||
}
|
||||
}
|
||||
|
||||
return (Optional.empty());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public Optional<MemoizedResult<V>> getMemoizedResult(K key)
|
||||
{
|
||||
MemoizedResult<V> result = map.get(key);
|
||||
if(result != null)
|
||||
{
|
||||
if(result.getTime().isAfter(Instant.now().minus(timeout)))
|
||||
{
|
||||
return (Optional.ofNullable(result));
|
||||
}
|
||||
}
|
||||
|
||||
@ -122,6 +141,16 @@ public class Memoization<K, V>
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public void clear()
|
||||
{
|
||||
this.map.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for timeoutSeconds
|
||||
**
|
||||
|
@ -29,7 +29,7 @@ import java.time.Instant;
|
||||
** Object stored in the Memoization class. Shouldn't need to be visible outside
|
||||
** its package.
|
||||
*******************************************************************************/
|
||||
class MemoizedResult<T>
|
||||
public class MemoizedResult<T>
|
||||
{
|
||||
private T result;
|
||||
private Instant time;
|
||||
|
@ -26,6 +26,7 @@ import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@ -36,6 +37,9 @@ import com.kingsrook.qqq.backend.core.utils.SleepUtils;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@ -84,6 +88,40 @@ class MemoizationTest extends BaseTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testCanStoreNull()
|
||||
{
|
||||
Memoization<String, Integer> memoization = new Memoization<>();
|
||||
memoization.storeResult("null", null);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// note - we can't tell a stored null apart from a non-stored value by calling getResult //
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
Optional<Integer> optionalNull = memoization.getResult("null");
|
||||
assertNotNull(optionalNull);
|
||||
assertTrue(optionalNull.isEmpty());
|
||||
|
||||
////////////////////////////////////////////
|
||||
// instead, we must use getMemoizedResult //
|
||||
////////////////////////////////////////////
|
||||
Optional<MemoizedResult<Integer>> optionalMemoizedResult = memoization.getMemoizedResult("null");
|
||||
assertNotNull(optionalMemoizedResult);
|
||||
assertTrue(optionalMemoizedResult.isPresent());
|
||||
assertNull(optionalMemoizedResult.get().getResult());
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// make sure getMemoizedResult returns empty for an un-set key //
|
||||
/////////////////////////////////////////////////////////////////
|
||||
optionalMemoizedResult = memoization.getMemoizedResult("never-stored");
|
||||
assertNotNull(optionalMemoizedResult);
|
||||
assertTrue(optionalMemoizedResult.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
Reference in New Issue
Block a user