我正在使用 Spring Boot 1.5.12.RELEASE
,并将咖啡因2.6.2
作为缓存提供程序。
我的一项服务中有一种方法:
@Cacheable(cacheNames = [CacheService.MY_CACHE_NAME], sync = true)
fun fetchThing(id: Int, at: OffsetDateTime?): Thing? {
LOGGER.debug("################### $id $at #############")
// some network operation
LOGGER.debug("################### $id $at IS DONE #############")
return thing
}
我希望只看到第一个日志一次,但如果我fetchThing
在第一个调用解决之前再次调用,则该值被计算两次:
09:18:34.657 [XNIO-2 task-11] DEBUG c.a.n.i.thing.ThingService - ################### 3140 null #############
09:18:34.673 [XNIO-2 task-12] DEBUG c.a.n.i.thing.ThingService - ################### 3140 null #############
09:18:36.025 [XNIO-2 task-11] DEBUG c.a.n.i.thing.ThingService - ################### 3140 null IS DONE #############
09:18:36.030 [XNIO-2 task-12] DEBUG c.a.n.i.thing.ThingService - ################### 3140 null IS DONE #############
如果我再次调用此函数,我看不到任何日志,因此缓存正在工作。
所以这似乎sync
不起作用。我错过了什么吗?
编辑:主类注释:
@SpringBootApplication(exclude = [ElastiCacheAutoConfiguration::class])
@EnableSwagger2
@EnableScheduling
@EnableCaching
@EnableTransactionManagement
class Application