Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Boot-Cacheable似乎每次都会进入该方法_Spring_Spring Boot_Kotlin - Fatal编程技术网

Spring Boot-Cacheable似乎每次都会进入该方法

Spring Boot-Cacheable似乎每次都会进入该方法,spring,spring-boot,kotlin,Spring,Spring Boot,Kotlin,我正在使用GraphQL和Spring引导(以及数据mongodb) 我向Mongo提出了一个特定的查询,我希望缓存该查询,这样它就不会每次都执行 我遇到了Spring的cacheable特性,我觉得我已经按照文档实现了,但它似乎不起作用。我已经在cacheable方法中添加了一个断点——第一次进行查询时,我希望它能进入,但第二次我不会 我的上述理解是否正确,或者我的配置是否错误: 在我的Gradle dependencies中,我有: dependencies { implementa

我正在使用GraphQL和Spring引导(以及数据mongodb)

我向Mongo提出了一个特定的查询,我希望缓存该查询,这样它就不会每次都执行

我遇到了Spring的
cacheable
特性,我觉得我已经按照文档实现了,但它似乎不起作用。我已经在cacheable方法中添加了一个断点——第一次进行查询时,我希望它能进入,但第二次我不会

我的上述理解是否正确,或者我的配置是否错误:

在我的Gradle dependencies中,我有:

dependencies {
    implementation(group = "org.springframework.boot", name = "spring-boot-starter-cache")
...
}
我启用了缓存,如下所示:

@EnableCaching
@SpringBootApplication
class DashboardGraphQLService

fun main(args: Array<String>) {
    runApplication<DashboardGraphQLService>(*args)
}
@EnableCaching
@SpringBoot应用程序
类仪表板GraphQLService
趣味主线(args:Array){
运行应用程序(*args)
}
然后是我想要缓存的函数:

@Component
class SGQueryResolver : GraphQLQueryResolver {
   fun sg(options: Options): List<Sg> {

        ....

        val results = mongoTemplate.find(query, Sg::class.java)

        results.forEach {
            heavyMethod("abc")
        }

        return results
    }
}

@Cacheable("test")
    fun heavyMethod(id: String): String {

        println("SOMETHING")

        return "Again"
}
@组件
SGQueryResolver类:GraphQLQueryResolver{
趣味sg(选项:选项):列表{
....
val results=mongoTemplate.find(查询,Sg::class.java)
结果:forEach{
重型方法(“abc”)
}
返回结果
}
}
@可缓存(“测试”)
趣味重量级方法(id:String):String{
println(“某物”)
返回“再次”
}
所以我希望println(“SOMETHING”)只会第一次出现,但每次都会出现,这让我觉得有些地方不对劲

任何想法都值得赞赏


谢谢。

终于明白了。基本上,
heavyMethod
是由我调用的,而不是由Spring调用的。因此,我所做的是创建一个新组件,将
heavyMethod
放入其中,然后自动连接该新组件并使用
heavyMethod
。是的@userMod2,这种情况下,本地调用不适用于注释。但是,您也可以直接注入CacheManager并手动从方法内部的缓存获取/放置,因此不需要为其创建新组件。