Spring data redis原子整型变量键名
Spring data redis atomic integer variable key name
我想我可能遗漏了一些重要的东西,但是你如何使用 Spring Data redis 对给定的键进行原子递减?
RedisAtomicLong
和 RedisAtomicInteger
绑定到我们在实例化它们时指定的键。
我如何对我选择的任何键进行原子递减?
我必须求助于 multi-exec 吗?在 vanilla redis 中,我可以通过简单地使用 DECR 命令自动递减任何键而无需求助于 multi exec。我在这里遗漏了什么吗?
谢谢,
理查德.
如果你想通过动态键递减,你可以执行以下操作
// 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();
}
我想我可能遗漏了一些重要的东西,但是你如何使用 Spring Data redis 对给定的键进行原子递减?
RedisAtomicLong
和 RedisAtomicInteger
绑定到我们在实例化它们时指定的键。
我如何对我选择的任何键进行原子递减?
我必须求助于 multi-exec 吗?在 vanilla redis 中,我可以通过简单地使用 DECR 命令自动递减任何键而无需求助于 multi exec。我在这里遗漏了什么吗?
谢谢, 理查德.
如果你想通过动态键递减,你可以执行以下操作
// 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();
}