1

我正在使用 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
4

1 回答 1

0

这很荒谬,但是...尝试更改缓存名称。

我刚刚遇到了同样的问题,不知道我的配置有什么问题。所以我改变了缓存的名称——它神奇地起作用了。真蠢...

于 2019-11-15T00:23:05.647 回答