Search in sources :

Example 56 with Transaction

use of redis.clients.jedis.Transaction in project new-cloud by xie-summer.

the class TransactionCommandsTest method unwatch.

@Test
public void unwatch() throws UnknownHostException, IOException {
    jedis.watch("mykey");
    String val = jedis.get("mykey");
    val = "foo";
    String status = jedis.unwatch();
    assertEquals("OK", status);
    Transaction t = jedis.multi();
    nj.connect();
    nj.auth("foobared");
    nj.set("mykey", "bar");
    nj.disconnect();
    t.set("mykey", val);
    List<Object> resp = t.exec();
    assertEquals(1, resp.size());
    assertEquals("OK", resp.get(0));
    // Binary
    jedis.watch(bmykey);
    byte[] bval = jedis.get(bmykey);
    bval = bfoo;
    status = jedis.unwatch();
    assertEquals(Keyword.OK.name(), status);
    t = jedis.multi();
    nj.connect();
    nj.auth("foobared");
    nj.set(bmykey, bbar);
    nj.disconnect();
    t.set(bmykey, bval);
    resp = t.exec();
    assertEquals(1, resp.size());
    assertEquals("OK", resp.get(0));
}
Also used : Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 57 with Transaction

use of redis.clients.jedis.Transaction in project new-cloud by xie-summer.

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<Set<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 : Set(java.util.Set) Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 58 with Transaction

use of redis.clients.jedis.Transaction in project new-cloud by xie-summer.

the class TransactionCommandsTest method transactionResponseBinary.

@Test
public void transactionResponseBinary() {
    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<byte[]> string = t.get("string".getBytes());
    Response<byte[]> list = t.lpop("list".getBytes());
    Response<byte[]> hash = t.hget("hash".getBytes(), "foo".getBytes());
    Response<Set<byte[]>> zset = t.zrange("zset".getBytes(), 0, -1);
    Response<byte[]> set = t.spop("set".getBytes());
    t.exec();
    assertArrayEquals("foo".getBytes(), string.get());
    assertArrayEquals("foo".getBytes(), list.get());
    assertArrayEquals("bar".getBytes(), hash.get());
    assertArrayEquals("foo".getBytes(), zset.get().iterator().next());
    assertArrayEquals("foo".getBytes(), set.get());
}
Also used : Set(java.util.Set) Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 59 with Transaction

use of redis.clients.jedis.Transaction in project new-cloud by xie-summer.

the class TransactionCommandsTest method transactionResponseWithError.

@Test
public void transactionResponseWithError() {
    Transaction t = jedis.multi();
    t.set("foo", "bar");
    Response<Set<String>> error = t.smembers("foo");
    Response<String> r = t.get("foo");
    List<Object> l = t.exec();
    assertEquals(JedisDataException.class, l.get(1).getClass());
    try {
        error.get();
        fail("We expect exception here!");
    } catch (JedisDataException e) {
    // that is fine we should be here
    }
    assertEquals(r.get(), "bar");
}
Also used : Set(java.util.Set) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Example 60 with Transaction

use of redis.clients.jedis.Transaction in project leshan by eclipse.

the class SingleInstanceJedisLock 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
 */
@Override
public void release(Jedis j, byte[] lockKey, byte[] lockValue) {
    if (lockValue != null) {
        // Watch the key to remove.
        j.watch(lockKey);
        byte[] previousLockValue = j.get(lockKey);
        // Delete the key if needed.
        if (Arrays.equals(previousLockValue, 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 {}ms (expiration at {}ms)", new String(lockKey), Hex.encodeHexString(lockValue), System.currentTimeMillis() - extractTime(lockValue), expiration);
            }
        } 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 {}ms (expiration at {}ms)", new String(lockKey), Hex.encodeHexString(lockValue), System.currentTimeMillis() - extractTime(lockValue), expiration);
            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)

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