春季 @Cacheable 用于具有相同签名的方法

huangapple go评论67阅读模式
英文:

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;
}

huangapple
  • 本文由 发表于 2020年9月7日 21:26:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63778524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定