【发布时间】:2021-08-22 23:49:37
【问题描述】:
我只想在结果的属性包含特定值时才缓存方法的结果。例如
Class APIOutput(code: Int, message: String)
sealed class Response<out T : Any> : Serializable {
data class Success<out T : Any>(val data: T) : Response<T>()
data class Error(val errorText: String, val errorCode: Int) : Response<Nothing>()
}
@Cacheable(
key = "api-key",
unless = "do something here"
)
fun doApicall(uniqueId: Long): Response<APIOutput> {
//make API call
val output = callAPI(uniqueId)
return Response.Success(output)
}
在上述方法中,我只想在 Response.Success.data.code ==(长代码列表)时缓存响应。 请注意,在上一行中,data 只不过是 APIOutput 对象。我如何使用除非或任何其他方法来实现它。我正在考虑编写一个函数,该函数将 doApicall 方法结果作为输入,并返回 true 或 false 并将该方法调用为除非 =“调用方法”。但我不知道该怎么做。非常感谢任何帮助。
【问题讨论】:
标签: spring spring-boot kotlin caching spring-cache