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.
So i created a service which adds a cachable layer:
@Cacheable("logicalTime", sync = true)
open fun getLogicalTimeById(dsId: String, idLogicalTimes: String): LogicalTime {
return logicalTimeResource.getById(dsId, idLogicalTimes)
}
logicalTimeResource is a proxy to the rest api.
The application framework looks like this:
Calls getLogicalTimeById for each entry in a Coroutine:
GlobalScope.launch {
getLogicalTime(element.dsId, element.idLogicalTime
}
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 logicalTimeResource.getById(dsId, idLogicalTimes)
Now the problem is that logicalTimeResource.getById(dsId, idLogicalTimes)
is called for every element in the List.
Yes i have @EnableCaching
at my Applicaiton.kt
Edit:
I created a small gist which shows that the cache is not working: https://gist.github.com/H3npi/799df85d4570e3cbe8b02ede24bea5a8
The main Application looks like this:
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<String>) {
runApplication<CachetestApplication>(*args)
}
I see both requests hitting my api service on localhost.
Edit v2:
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: https://gist.github.com/H3npi/af37ea97ea3450deeca2ab8933072c94
The problem was: My Component was not autowired. Autowiring fixed the issue.