【问题标题】:can't query a list in a spring-data-redis transaction无法在 spring-data-redis 事务中查询列表
【发布时间】:2016-04-25 21:19:28
【问题描述】:
    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”字段为空,但是,值为“Hello World”的“mykey”已在我的 redis 中持续存在。

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

【问题讨论】:

    标签: redis spring-data-redis


    【解决方案1】:

    SD-Redis 中的事务支持有助于参与正在进行的事务并允许自动提交 (exec) / 回滚 (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();                                   
    

    【讨论】:

    • 感谢您的分析,您的回答对我帮助很大。
    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 2019-11-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 2018-01-14
    • 2021-09-13
    • 2016-06-29
    相关资源
    最近更新 更多