title: Alternatives to @Cacheable with spring webflux
body: <p>I need to cache data coming from a <code>ReactiveMongoRepository</code>. The data gets updated roughly twice a year, so I don't care about expiring the cache. </p>
  
  <p>Since we <a href="https://stackoverflow.com/questions/48156424/spring-webflux-and-cacheable-proper-way-of-caching-result-of-mono-flux-type">can't use @Cacheable</a> with a flux, I'd like to find a straightforward, easy way to store the data coming from Mongo to redis, and use that data if present, else store it and serve the original data.</p>
  
  <p>Is there are more straightforward way to do so than</p>
  
  <pre><code>  @GetMapping
    public Flux&lt;AvailableInspection&gt; getAvailableInspectionsRedis() {
      AtomicInteger ad = new AtomicInteger();
      return availableInspectionReactiveRedisOperations.opsForZSet().range("availableInspections", Range.&lt;Long&gt;from(Range.Bound.inclusive(0L)).to(Range.Bound.inclusive(-1L)))
          .switchIfEmpty(availableInspectionMongoRepository.findAll().map(e -&gt; {
            availableInspectionReactiveRedisOperations.opsForZSet().add("availableInspections", e, ad.getAndIncrement()).block();
            return e;
          }));
    }
  </code></pre>
  
  <p>What I'm looking for explicitly is an option that lets me cache the data just as the @Cacheable annotation would do it. I'm looking for a generic solution to be able to cache any kind of flux. </p>