RedisTemplate setnx -Spring Boot

Spring RedisTemplate setnx方法的替代者和用法。

@Autowired
private RedisTemplate redis;

Boolean nx = redis.opsForValue().setIfAbsent("key", "value", 20, TimeUnit.MINUTES)

setIfAbsent方法:

@Nullable
Boolean setIfAbsent(V value)
Set the bound key to hold the string value if the bound key is absent.
Parameters:
value - must not be null.
Returns:
null when used in pipeline / transaction.
See Also:
Redis Documentation: SETNX

setIfAbsent提供多个重载参数,具体参考官方文档