Skip to content

Example : Implementing a data cache

johnmcclean-aol edited this page Jan 26, 2015 · 1 revision

A SimpleReact dataflow stages can be cached, forked and merged. SimpleReact can be used to perform expensive processing asynchronously. Rather than accessing the raw results, the SimpleReact stage (or the underlying Stream can be cached.

Cached Stages can then be forked and resused unlimited times.

Example of caching and forking a SimpleReact dataflow :-

Stage<String> stage1 = new SimpleReact().<Integer> react(() -> 1,
			() -> 2, () -> 3).then(it -> "*" + it);
	Stage<String> stage2 = new SimpleReact().<Integer> react(() -> 4,
			() -> 5, () -> 6).then(it -> "*" + it);

	List<String> result1 = stage1.merge(stage2).then(it -> it +"*").block();
	List<String> result2 = stage1.merge(stage2).then(it -> it +"-").block();
	
	result1.stream().forEach( it-> assertThat(it,endsWith("*")));
	result2.stream().forEach( it-> assertThat(it,endsWith("-")));

Example of converting to and from Stream

Stream<CompletableFuture<String>> stream = new SimpleReact()
												.<Integer> react(() -> 1, () -> 2, () -> 3)
												.then(it -> "*" + it).stream();
	
	List<String> strings = new SimpleReact()
							.<String>fromStream(stream)
							.then(it ->  it + "*")
							.block();

	assertThat(strings.size(), is(3));
	
	
	assertThat(strings,hasItem("*1*"));
Clone this wiki locally