title: Spring Boot Kotlin Coroutine Caching of Http Calls
body: <p>i am trying to use the spring-boot caching mechanism to cache a http response which i get. Currently i have an http client which makes an 
  rest call for me. I want the result to be cached.</p>
  
  <p>So i created a service which adds a cachable layer:</p>
  
  <pre><code>@Cacheable("logicalTime", sync = true)
  open fun getLogicalTimeById(dsId: String, idLogicalTimes: String): LogicalTime {
      return logicalTimeResource.getById(dsId, idLogicalTimes)
  }
  </code></pre>
  
  <p>logicalTimeResource is a proxy to the rest api.</p>
  
  <p>The application framework looks like this:</p>
  
  <ul>
  <li>a sheduler which is called every second</li>
  <li>iterates through a List with entries</li>
  <li><p>Calls getLogicalTimeById for each entry in a Coroutine:</p>
  
  <p><code>GlobalScope.launch {
  getLogicalTime(element.dsId, element.idLogicalTime
  }</code></p></li>
  </ul>
  
  <p>i'd expect that the cache is looked up for the dsId and idLogicalTimes, and when there is an entry for this pair of values, it is returned from the cache. Otherwise <code>logicalTimeResource.getById(dsId, idLogicalTimes)</code></p>
  
  <p>Now the problem is that <code>logicalTimeResource.getById(dsId, idLogicalTimes)</code> is called for every element in the List.
  Yes i have <code>@EnableCaching</code> at my Applicaiton.kt</p>
  
  <p>Edit:</p>
  
  <p>I created a small gist which shows that the cache is not working:
  <a href="https://gist.github.com/H3npi/799df85d4570e3cbe8b02ede24bea5a8" rel="nofollow noreferrer">https://gist.github.com/H3npi/799df85d4570e3cbe8b02ede24bea5a8</a></p>
  
  <p>The main Application looks like this:</p>
  
  <pre><code>package com.example.cachetest
  
  import org.springframework.boot.autoconfigure.SpringBootApplication
  import org.springframework.boot.runApplication
  import org.springframework.cache.annotation.EnableCaching
  
  @SpringBootApplication
  @EnableCaching
  class CachetestApplication
  
  fun main(args: Array&lt;String&gt;) {
      runApplication&lt;CachetestApplication&gt;(*args)
  }
  </code></pre>
  
  <p>I see both requests hitting my api service on localhost.</p>
  
  <p>Edit v2:</p>
  
  <p>Thanks to Stephane Nicoll pointing out that caching in the same class doesn't work, i updated the gist and moved all calls to a repo class. This looks like this: <a href="https://gist.github.com/H3npi/af37ea97ea3450deeca2ab8933072c94" rel="nofollow noreferrer">https://gist.github.com/H3npi/af37ea97ea3450deeca2ab8933072c94</a></p>