System.out.println(redisTemplate.opsForValue()。get("redisKey"));
boolean redisKeyBoolean = redisTemplate.opsForValue()。setIfAbsent("redisKey","666");
System.out.println("第一次redisKey没有值,则:"+redisKeyBoolean);
System.out.println(redisTemplate.opsForValue()。get("redisKey"));
redisKeyBoolean = redisTemplate.opsForValue()。setIfAbsent("redisKey","999");
System.out.println("第二次redisKey有值,则:"+redisKeyBoolean);
System.out.println(redisTemplate.opsForValue()。get("redisKey"));
if(!redisKeyBoolean){
Object redisKey = redisTemplate.opsForValue()。getAndSet("redisKey", "000");
System.out.println(redisKey);
System.out.println(redisTemplate.opsForValue()。get("redisKey"));
}
输出:
null
第一次redisKey没有值,则:true
666
第二次redisKey有值,则:false
666
666
000
备注:
原程序代码,value的值是一个时间戳,有是否过期的逻辑判断;
setIfAbsent()有关于过期时间的设置
|