code:
public Object testRedisMulti() { Object o = stringRedisTemplate.execute(new SessionCallback() { @Override public Object execute(RedisOperations operations) throws DataAccessException { // operations.watch("testRedisMulti"); operations.multi(); operations.opsForValue().set("testRedisMulti", "0"); String now = (String) operations.opsForValue().get("testRedisMulti"); System.out.println(now); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } now = (String) operations.opsForValue().get("testRedisMulti"); System.out.println(now); Object rs = operations.exec(); return rs; } }); System.out.println(o); return o; }
初始值:
127.0.0.1:6389> get testRedisMulti
"initial"
代码执行:
operations.multi(); operations.opsForValue().set("testRedisMulti", "0"); String now = (String) operations.opsForValue().get("testRedisMulti"); System.out.println(now); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); }
客户端:
127.0.0.1:6389> get testRedisMulti
"initial",意味着multi中的命令还未发送
System.out输出:
null
注意在multi中的get是娶不到值的
过5秒。。。
代码执行:
now = (String) operations.opsForValue().get("testRedisMulti"); System.out.println(now); Object rs = operations.exec(); return rs;
System.out输出:
null
客户端:
127.0.0.1:6389> get testRedisMulti
"0",multi命令提交后修改了值
最终输出
[0, 0]
可以看到两次get的值返回给了execute函数,而且是修改后的值,符合原理
初始化 initial
redis写0
redis读 null
redis提交0
redis读 null
回调读[0,0]
隐患?!