Search in sources :

Example 61 with Transaction

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));
    }
}
Also used : Transaction(redis.clients.jedis.Transaction)

Example 62 with Transaction

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"));
}
Also used : Transaction(redis.clients.jedis.Transaction)

Example 63 with Transaction

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;
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction)

Example 64 with Transaction

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);
}
Also used : Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 65 with Transaction

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());
}
Also used : Transaction(redis.clients.jedis.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

Transaction (redis.clients.jedis.Transaction)149 Test (org.junit.Test)90 Jedis (redis.clients.jedis.Jedis)57 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)15 Set (java.util.Set)14 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)8 List (java.util.List)8 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)7 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)6 Protocol (redis.clients.jedis.Protocol)4 Response (redis.clients.jedis.Response)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 Header (redis.clients.jedis.graph.Header)4 Record (redis.clients.jedis.graph.Record)4 ResultSet (redis.clients.jedis.graph.ResultSet)4 Node (redis.clients.jedis.graph.entities.Node)4 Property (redis.clients.jedis.graph.entities.Property)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3