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));
}
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());
}
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());
}
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");
}
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));
}
}
Aggregations