2

おそらく何か大きなものが欠けていると思いますが、Spring Data redis を使用して特定のキーでアトミックなデクリメントを行うにはどうすればよいですか?

RedisAtomicLongとはRedisAtomicInteger、インスタンス化中に指定したキーにバインドされます。

選択したキーでアトミックデクリメントを行うにはどうすればよいですか?

マルチ実行に頼る必要がありますか? バニラの redis では、DECR コマンドを使用するだけで、マルチ exec に頼ることなく任意のキーをアトミックにデクリメントできます。ここで何か不足していますか?

ありがとう、リチャード。

4

1 に答える 1

7

動的キーでデクリメントしたい場合は、次のことができます

// inject the actual template
@Autowired
private RedisTemplate<String, Integer> template; // This can be RedisTemplate<String, Long> also based on your need

// inject the template as ValueOperations
@Resource(name="redisTemplate")
private ValueOperations<String, Integer> valueOps;

public Integer decrement(String key) {
    return ((Long)valueOps.increment(key, -1l)).intValue();
}
于 2015-03-19T01:30:27.020 に答える