Skip to content

Instantly share code, notes, and snippets.

@mmimica
Created April 5, 2018 12:18
Show Gist options
  • Save mmimica/058132ee7dfc81842320908591eba575 to your computer and use it in GitHub Desktop.
Save mmimica/058132ee7dfc81842320908591eba575 to your computer and use it in GitHub Desktop.
cached data provider
interface DataProvider<I, R> {
T calculate(I input);
}
@RequiredArgsConstructor
public class DataProviderCached<I, R> implements DataProvider<I, R> {
private final DataProvider delegate;
private final Map<I, R> cache = new ConcurrentHashMap<>();
@Override
public R calculate(I input) {
return cache.computeIfAbsent(input, delegate::calculate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment