首页 文章

无法在spring-data-redis事务中查询列表

提问于
浏览
0
template.setEnableTransactionSupport(true);
    template.multi();
    template.opsForValue().set("mykey", "Hello World");
    List<String> dataList = template.opsForList().range("mylist", 0, -1);
    template.exec();

嗨,大家好 . 我的redis中有一个名为“mylist”的列表,其大小为50 .

但是当我运行这段代码时,我无法得到我想要的东西 .

字段“dataList”为null,但是,“mykey”的值为“Hello World”已保留在我的redis中 .

那么如何在spring-data-redis事务中获取列表数据呢?非常感谢 .

1 回答

  • 1

    SD-Redis中的事务支持有助于参与正在进行的事务并允许自动提交( exec )/ rollback( discard ),因此它有助于使用相同的连接将命令包装到线程绑定的多个exec块中 .
    更常见的是redis transactions并且事务中的命令在服务器端排队并返回 exec 上的结果列表 .

    template.multi();
    
    // queue set command
    template.opsForValue().set("mykey", "Hello World"); 
    
    // queue range command
    List<String> dataList = template.opsForList().range("mylist", 0, -1);
    
    // execute queued commands
    // result[0] = OK
    // result[1] = {"item-1", "item-2", "item-", ...}
    List<Object> result = template.exec();
    

相关问题