Skip to content

Instantly share code, notes, and snippets.

@stefanotroia
Created December 11, 2019 07:43
Show Gist options
  • Save stefanotroia/4470950b0debe0236571edec5daaa824 to your computer and use it in GitHub Desktop.
Save stefanotroia/4470950b0debe0236571edec5daaa824 to your computer and use it in GitHub Desktop.
base service with caching
@Service
@Slf4j
public class BaseService<E extends GenericEntity> {
@Autowired
protected HazlecastService hazlecastService;
public Mono<E> findCacheValue(String cacheName, List<String> keys, Mono<E> fallBackMono) {
return CacheMono
.lookup(k -> findCacheValue(cacheName, keys).map(Signal::next), keys)
.onCacheMissResume(fallBackMono)
.andWriteWith((k, sig) -> Mono.fromRunnable(() ->
writeCacheValue(cacheName,keys,sig.get())));
}
public Mono<E> writeCacheValue(String cacheName, List<String> keys, E data) {
if(data != null) {
hazlecastService.getHzInstance().getMap(cacheName).set(createKey(keys),data);
return Mono.just(data);
}
return Mono.empty();
}
public Flux<E> writeCacheValues(String cacheName, List<String> keys, List<E> data) {
hazlecastService.getHzInstance().getMap(cacheName).set(createKey(keys),data);
return Flux.fromIterable(data);
}
public void evictValue(String cacheName, List<String> keys) {
hazlecastService.getHzInstance().getMap(cacheName).evict(createKey(keys));
}
}
@arsalans
Copy link

Hi, is this method calling itself, can you please highlight what is going on here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment