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”为空,但是,值为“Hello World”的“mykey”已保留在我的 redis 中。

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

4

1 回答 1

1

The transaction suppport in SD-Redis helps participating in ongoing transactions and allows automatic commit (exec) / rollback (discard), so it helps wrapping commands into thread bound multi exec blocks using the same connection.
More generally redis transactions and the commands within a transaction get queued on server side and return a list of results on 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();                                   
于 2016-04-25T13:19:28.407 回答