org.apache.geode.cache.LoaderHelper Java Examples

The following examples show how to use org.apache.geode.cache.LoaderHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuoteLoader.java    From geode-examples with Apache License 2.0 6 votes vote down vote up
@Override
public String load(LoaderHelper<String, String> helper) throws CacheLoaderException {

  log.info("Loading quote for {}", helper.getKey());
  String quote = quotes.get(helper.getKey());

  try {
    // simulate network delay for a REST call or a database query
    Thread.sleep(100);
    return quote;

  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new CacheLoaderException(e);
  }
}
 
Example #2
Source File: ExampleTest.java    From geode-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testExample() throws Exception {
  QuoteLoader loader = new QuoteLoader();
  Map<String, String> region = Mockito.spy(new HashMap<>());

  when(region.get(any())).then(inv -> {
    String key = inv.getArgument(0);
    LoaderHelper<String, String> helper = mock(LoaderHelper.class);
    when(helper.getKey()).thenReturn(key);

    return loader.load(helper);
  });

  printQuotes(region);

  assertThat(systemOutRule.getLog()).contains("Anton Chekhov");
  assertThat(systemOutRule.getLog()).contains("Loaded 20 definitions");
  assertThat(systemOutRule.getLog()).contains("Fetched 20 cached definitions");
}
 
Example #3
Source File: QuoteLoader.java    From geode-examples with Apache License 2.0 6 votes vote down vote up
@Override
public String load(LoaderHelper<String, String> helper) throws CacheLoaderException {

  log.info("Loading quote for {}", helper.getKey());
  String quote = quotes.get(helper.getKey());

  try {
    // simulate network delay for a REST call or a database query
    Thread.sleep(100);
    return quote;

  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new CacheLoaderException(e);
  }
}
 
Example #4
Source File: ExampleTest.java    From geode-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testExample() throws Exception {
  QuoteLoader loader = new QuoteLoader();
  Map<String, String> region = Mockito.spy(new HashMap<>());

  when(region.get(any())).then(inv -> {
    String key = inv.getArgument(0);
    LoaderHelper<String, String> helper = mock(LoaderHelper.class);
    when(helper.getKey()).thenReturn(key);

    return loader.load(helper);
  });

  printQuotes(region);

  assertThat(systemOutRule.getLog()).contains("Anton Chekhov");
  assertThat(systemOutRule.getLog()).contains("Loaded 20 definitions");
  assertThat(systemOutRule.getLog()).contains("Fetched 20 cached definitions");
}
 
Example #5
Source File: RepositoryCacheLoader.java    From spring-boot-data-geode with Apache License 2.0 5 votes vote down vote up
@Override
public T load(LoaderHelper<ID, T> helper) throws CacheLoaderException {

  try {
    return getRepository().findById(helper.getKey()).orElse(null);
  }
  catch (Exception cause) {
    throw newCacheRuntimeException(() -> String.format(CACHE_LOAD_EXCEPTION_MESSAGE,
        helper.getKey(), getRepository().getClass().getName()), cause);
  }
}
 
Example #6
Source File: RepositoryCacheLoaderWriterSupport.java    From spring-boot-data-geode with Apache License 2.0 4 votes vote down vote up
@Override
public T load(LoaderHelper<ID, T> helper) throws CacheLoaderException {
	return null;
}
 
Example #7
Source File: EchoCacheLoader.java    From spring-boot-data-geode with Apache License 2.0 4 votes vote down vote up
@Override
public String load(LoaderHelper<String, String> helper) throws CacheLoaderException {
	return helper.getKey();
}
 
Example #8
Source File: ProductCacheLoader.java    From spring-data-examples with Apache License 2.0 4 votes vote down vote up
@Override
public Product load(LoaderHelper loaderHelper) throws CacheLoaderException {
	return new Product((long) loaderHelper.getKey(), randomStringName(), randomPrice(), "");
}
 
Example #9
Source File: RepositoryCacheLoaderWriterSupportUnitTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
@Test
public void loadReturnsNull() {

	LoaderHelper<?, ?> mockLoadHelper = mock(LoaderHelper.class);

	assertThat(new TestRepositoryCacheLoaderWriterSupport<>(this.mockCrudRepository).load(mockLoadHelper)).isNull();

	verifyZeroInteractions(mockLoadHelper);
}