title: How to make reactor.cache.CacheMono not to call calculation twice?
body: <p>I'm trying to implement caching mechanism for my settings and I can't really make it working.</p>
  
  <p>I have realization of my repository like this:</p>
  
  <pre><code>@AllArgsConstructor
  public class CachedSettingsRepository&lt;E extends SettingsEntity&gt; implements SettingsRepository&lt;E&gt; {
  
      private final String key;
      private final SettingsRepository&lt;E&gt; settingsRepository;
      private final Cache&lt;String, Signal&lt;? extends E&gt;&gt; cache;
  
      @Trace
      @Override
      public Mono&lt;E&gt; get() {
          return CacheMono.lookup(cache.asMap(), key)
                          .onCacheMissResume(settingsRepository::get);
  
      }
  
      @Trace
      @Override
      public Mono&lt;Void&gt; delete() {
          return settingsRepository.delete()
                                   .doOnNext(empty -&gt; cache.invalidate(key));
      }
  
      @Trace
      @Override
      public Mono&lt;E&gt; cas(Function&lt;Mono&lt;E&gt;, Publisher&lt;E&gt;&gt; function) {
          return settingsRepository.cas(function)
                                   .doOnNext(result -&gt; cache.invalidate(key));
      }
  
  }
  </code></pre>
  
  <p>I've configured it like this:</p>
  
  <pre><code>@Configuration
  public class CacheContext {
  
      @Bean
      @ConfigurationProperties("settings.cache")
      public CacheSettingsBean paymentCacheSettingsBean() {
          return new CacheSettingsBean();
      }
  
      @Bean
      public Cache&lt;String, Signal&lt;? extends PaymentSettingsEntity&gt;&gt; paymentSettingsCache() {
          return Caffeine.newBuilder()
                         .maximumSize(paymentCacheSettingsBean().getMaximumSize())
                         .expireAfterWrite(paymentCacheSettingsBean().getExpireAfterWrite())
                         .build();
      }
  
      @Bean
      public CachedSettingsRepository&lt;PaymentSettingsEntity&gt; cachedPaymentSettingsRepository(
              SettingsRepository&lt;PaymentSettingsEntity&gt; paymentSettingsRepository,
              Cache&lt;String, Signal&lt;? extends PaymentSettingsEntity&gt;&gt; paymentSettingsCache) {
          return new CachedSettingsRepository&lt;&gt;(paymentCacheSettingsBean().getKey(), paymentSettingsRepository, paymentSettingsCache);
      }
  
  }
  </code></pre>
  
  <p>And I'm trying to test it:</p>
  
  <pre><code>@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
          classes = [CacheContext, TestContext])
  @ActiveProfiles("settings-cache")
  @EnableConfigurationProperties
  class CachedSettingsRepositoryTest extends Specification {
  
      @Autowired
      CachedSettingsRepository&lt;PaymentSettingsEntity&gt; cachedPaymentSettingsRepository
      @Autowired
      SettingsRepository&lt;PaymentSettingsEntity&gt; paymentSettingsRepository
  
      def "test getting from cache"() {
          given:
              def paymentSettings = paymentSettingsEntity()
          when:
              def result1 = cachedPaymentSettingsRepository.get().block()
              def result2 = cachedPaymentSettingsRepository.get().block()
          then:
              1 * paymentSettingsRepository.get() &gt;&gt; Mono.just(paymentSettings)
              0 * _
  
              result1 == paymentSettings
              result1 == result2
      }
  
      def paymentSettingsEntity() {
          PaymentSettingsEntity.of([PaymentRangeSetting.of(0.0, 100.0)])
      }
  
      @Configuration
      static class TestContext {
          private DetachedMockFactory factory = new DetachedMockFactory()
  
          @Bean
          SettingsRepository&lt;PaymentSettingsEntity&gt; paymentSettingsRepository() {
              factory.Mock(SettingsRepository)
          }
      }
  
  }
  </code></pre>
  
  <p>I expect that it will call my paymentSettingsRepository on the first try and then will return cached on the second, but test is failing all the time.</p>