通过四组测试数据来对比表达,更为直观
1-1
访问结果:
1-2
访问结果:
1-3
访问结果:
1-4
访问结果:
通过以上四组测试结果可出结论
redisTemplate.boundHashOps(“test”).increment(key, value);
第一次使用increment时,键值对对应的value是初始化的值
第二次使用increment时,键值对对应的value是自增或者自减的值;**
测试得出的结果和总结的结论才是王道,其他都是浮云。
若你get到,请点赞+评论
源码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class TestRedisController {
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("/redis")
public Map getRedis(){
Map map = new HashMap();
Long num1 = redisTemplate.boundHashOps("test").increment("james", 5);
Long num2 = redisTemplate.boundHashOps("test").increment("james",-2);
map.put("num1",num1);
map.put("num2",num2);
return map;
}
}