英文:
Spring @Cacheable for methods with same signature
问题
我有一个 Spring Bean,其中有几个具有相同签名的查找方法,就像这样:
@Cacheable
public Player findById(int id) {
// 根据 id 查询玩家
return player;
}
@Cacheable
public Player findByRank(int rank) {
// 根据排名查询玩家
return player;
}
@Cacheable
注解将这两个方法视为相同,这会混淆缓存。有没有办法告诉 Spring 这些方法的键不相同?
英文:
I have a Spring bean that has a couple of finders with the same signature, like this:
@Cacheable
public Player findById(int id) {
// query player by id
return player;
}
@Cacheable
public Player findByRank(int rank) {
// query player by rank
return player;
}
The @Cacheable annotation treats both methods equal so this messes up the cache. Is there a way to tell Spring that the keys for those methods are not the same?
答案1
得分: 4
也许你可以尝试这样做?不确定它是否有效..
@Cacheable(value = "playerId")
public Player findById(int id) {
// 通过id查询玩家
return player;
}
@Cacheable(value = "playerRank")
public Player findByRank(int rank) {
// 通过排名查询玩家
return player;
}
英文:
Maybe you can try this? Not sure whether it works..
@Cacheable(value = "playerId")
public Player findById(int id) {
// query player by id
return player;
}
@Cacheable(value = "playerRank")
public Player findByRank(int rank) {
// query player by rank
return player;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论