Hack way
For now, there is no fluent integration of @Cacheable
with Reactor 3.
However, you may bypass that thing by adding .cache()
operator to returned Mono
@Repository
interface TaskRepository : ReactiveMongoRepository<Task, String>
@Service
class TaskService(val taskRepository: TaskRepository) {
@Cacheable("tasks")
fun get(id: String): Mono<Task> = taskRepository.findById(id).cache()
}
That hack cache and share returned from taskRepository
data. In turn, spring cacheable will cache a reference of returned Mono
and then, will return that reference. In other words, it is a cache of mono which holds the cache :).
Reactor Addons Way
There is an addition to Reactor 3 which allows fluent integration with modern in-memory caches like caffeine, jcache, etc. Using that technique you will be capable to cache your data easily:
@Repository
interface TaskRepository : ReactiveMongoRepository<Task, String>
@Service
class TaskService(val taskRepository: TaskRepository) {
@Autowire
CacheManager manager;
fun get(id: String): Mono<Task> = CacheMono.lookup(reader(), id)
.onCacheMissResume(() -> taskRepository.findById(id))
.andWriteWith(writer());
fun reader(): CacheMono.MonoCacheReader<String, Task> = key -> Mono.<Signal<Task>>justOrEmpty((Signal) manager.getCache("tasks").get(key).get())
fun writer(): CacheMono.MonoCacheWriter<String, Task> = (key, value) -> Mono.fromRunnable(() -> manager.getCache("tasks").put(key, value));
}
Note: Reactor addons caching own abstraction which is Signal<T>
, so, do not worry about that and following that convention