Spring数据redis原子整数变量键名

use*_*066 2 java spring spring-data-redis

我想我可能错过了一些大的东西,但是如何使用Spring Data redis对给定的密钥进行原子减量?

RedisAtomicLongRedisAtomicInteger绑定到我们在实例他们指定的关键.

如何在我选择的任何键上进行原子减量?

我是否必须求助于多个执行官?在vanilla redis中,我可以通过简单地使用DECR命令原子地减少任何键,而无需使用多个exec.我在这里错过了什么吗?

谢谢,理查德.

sha*_*zin 6

如果要通过动态键递减,可以执行以下操作

// 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();
}
Run Code Online (Sandbox Code Playgroud)