我是Redis的新手,我正在试验Redis事务,我想在下面的场景中使用Redis事务。需要知道这样做是否可行:
我尝试了以下代码,但在执行事务之前,它无法工作,因为我正在执行<Response>.get()。
final Transaction tx = jedis.multi();
final Response<Set<Tuple>> tuples = tx.zrangeByScoreWithScores("randomKey", 0d, 100, 0, 10);
for (final Tuple tuple : tuples.get()) {
jedis.incr(tuple);
}
tx.exec(); //In a hope that get and sets happen in a single transaction.有办法绕过这件事吗?
发布于 2017-03-29 07:58:01
你可以用雷迪森代替。使用Lock对象而不是事务。例如,您的案例:
RLock lock = redisson.getLock("myLock");
lock.lock();
try {
Collection<ScoredEntry<V>> entries = redisson.getScoredSortedSet("randomKey").entryRange(true, 0, true, 100, 0, 10);
for (final ScoredEntry<V> entry : entries) {
redisson.getAtomicLong(entry.getValue()).incrementAndGet();
}
} finally {
lock.unlock();
}https://stackoverflow.com/questions/42121967
复制相似问题