use of redis.clients.jedis.Transaction in project leshan by eclipse.
the class RedisLock method release.
/**
* Releases a lock for a given key and value.
*
* @param j a Redis connection
* @param lockKey the locked key
* @param lockValue the value returned when the lock was acquired
*/
public static void release(Jedis j, byte[] lockKey, byte[] lockValue) {
if (lockValue != null) {
// Watch the key to remove.
j.watch(lockKey);
byte[] prevousLockValue = j.get(lockKey);
// Delete the key if needed.
if (Arrays.equals(prevousLockValue, lockValue)) {
// Try to delete the key
Transaction transaction = j.multi();
transaction.del(lockKey);
boolean succeed = transaction.exec() != null;
if (!succeed) {
LOG.warn("Failed to release lock for key {}/{}, meaning the key probably expired because of acquiring the lock for too long (more than {}ms)", new String(lockKey), Hex.encodeHexString(lockValue), LOCK_EXP);
}
} else {
// the key must not be deleted.
LOG.warn("Nothing to release for key {}/{}, meaning the key probably expired because of acquiring the lock for too long (more than {}ms)", new String(lockKey), Hex.encodeHexString(lockValue), LOCK_EXP);
j.unwatch();
}
} else {
LOG.warn("Trying to release a lock for {} with a null value", new String(lockKey));
}
}
use of redis.clients.jedis.Transaction in project ru102j by redislabs-training.
the class CompareAndUpdateScriptTest method updateIfLess.
@Test
public void updateIfLess() {
Transaction t1 = jedis.multi();
cu.updateIfLess(t1, key, "n", 0.0);
t1.exec();
assertThat(jedis.hget(key, field), is("0.0"));
Transaction t2 = jedis.multi();
cu.updateIfLess(t2, key, "n", 2.0);
t2.exec();
assertThat(jedis.hget(key, field), is("0.0"));
}
use of redis.clients.jedis.Transaction in project datashare by ICIJ.
the class UsersInRedis method saveOrUpdate.
@Override
public boolean saveOrUpdate(User user) {
try (Jedis jedis = redis.getResource()) {
Transaction transaction = jedis.multi();
transaction.set(user.login(), JsonUtils.serialize(((DatashareUser) user).details));
transaction.expire(user.login(), this.ttl);
List<Object> exec = transaction.exec();
return exec.size() == 2;
}
}
use of redis.clients.jedis.Transaction in project jedis by redis.
the class TransactionCommandsTest method discard.
@Test
public void discard() {
Transaction t = jedis.multi();
String status = t.discard();
assertEquals("OK", status);
}
use of redis.clients.jedis.Transaction in project jedis by redis.
the class TransactionCommandsTest method transactionResponse.
@Test
public void transactionResponse() {
jedis.set("string", "foo");
jedis.lpush("list", "foo");
jedis.hset("hash", "foo", "bar");
jedis.zadd("zset", 1, "foo");
jedis.sadd("set", "foo");
Transaction t = jedis.multi();
Response<String> string = t.get("string");
Response<String> list = t.lpop("list");
Response<String> hash = t.hget("hash", "foo");
Response<List<String>> zset = t.zrange("zset", 0, -1);
Response<String> set = t.spop("set");
t.exec();
assertEquals("foo", string.get());
assertEquals("foo", list.get());
assertEquals("bar", hash.get());
assertEquals("foo", zset.get().iterator().next());
assertEquals("foo", set.get());
}
Aggregations