Search in sources :

Example 6 with Transaction

use of redis.clients.jedis.Transaction in project cachecloud by sohutv.

the class TransactionCommandsTest method select.

@Test
public void select() {
    jedis.select(1);
    jedis.set("foo", "bar");
    jedis.watch("foo");
    Transaction t = jedis.multi();
    t.select(0);
    t.set("bar", "foo");
    Jedis jedis2 = createJedis();
    jedis2.select(1);
    jedis2.set("foo", "bar2");
    List<Object> results = t.exec();
    assertNull(results);
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 7 with Transaction

use of redis.clients.jedis.Transaction in project cachecloud by sohutv.

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 8 with Transaction

use of redis.clients.jedis.Transaction in project gitblit by gitblit.

the class RedisTicketService method deleteTicketImpl.

/**
	 * Deletes a ticket.
	 *
	 * @param ticket
	 * @return true if successful
	 */
@Override
protected boolean deleteTicketImpl(RepositoryModel repository, TicketModel ticket, String deletedBy) {
    boolean success = false;
    if (ticket == null) {
        throw new RuntimeException("must specify a ticket!");
    }
    Jedis jedis = pool.getResource();
    if (jedis == null) {
        return false;
    }
    try {
        // atomically remove ticket
        Transaction t = jedis.multi();
        t.del(key(repository, KeyType.ticket, ticket.number));
        t.del(key(repository, KeyType.journal, ticket.number));
        t.exec();
        success = true;
        log.debug("deleted ticket {} from Redis @ {}", "" + ticket.number, getUrl());
    } catch (JedisException e) {
        log.error("failed to delete ticket from Redis @ " + getUrl(), e);
        pool.returnBrokenResource(jedis);
        jedis = null;
    } finally {
        if (jedis != null) {
            pool.returnResource(jedis);
        }
    }
    return success;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisException(redis.clients.jedis.exceptions.JedisException) Transaction(redis.clients.jedis.Transaction)

Example 9 with Transaction

use of redis.clients.jedis.Transaction in project gitblit by gitblit.

the class RedisTicketService method commitChangeImpl.

/**
	 * Commit a ticket change to the repository.
	 *
	 * @param repository
	 * @param ticketId
	 * @param change
	 * @return true, if the change was committed
	 */
@Override
protected boolean commitChangeImpl(RepositoryModel repository, long ticketId, Change change) {
    Jedis jedis = pool.getResource();
    if (jedis == null) {
        return false;
    }
    try {
        List<Change> changes = getJournal(jedis, repository, ticketId);
        changes.add(change);
        // build a new effective ticket from the changes
        TicketModel ticket = TicketModel.buildTicket(changes);
        String object = TicketSerializer.serialize(ticket);
        String journal = TicketSerializer.serialize(change);
        // atomically store ticket
        Transaction t = jedis.multi();
        t.set(key(repository, KeyType.ticket, ticketId), object);
        t.rpush(key(repository, KeyType.journal, ticketId), journal);
        t.exec();
        log.debug("updated ticket {} in Redis @ {}", "" + ticketId, getUrl());
        return true;
    } catch (JedisException e) {
        log.error("failed to update ticket cache in Redis @ " + getUrl(), e);
        pool.returnBrokenResource(jedis);
        jedis = null;
    } finally {
        if (jedis != null) {
            pool.returnResource(jedis);
        }
    }
    return false;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisException(redis.clients.jedis.exceptions.JedisException) Transaction(redis.clients.jedis.Transaction) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change)

Example 10 with Transaction

use of redis.clients.jedis.Transaction in project gitblit by gitblit.

the class RedisTicketService method deleteAllImpl.

/**
	 *  Deletes all Tickets for the rpeository from the Redis key-value store.
	 *
	 */
@Override
protected boolean deleteAllImpl(RepositoryModel repository) {
    Jedis jedis = pool.getResource();
    if (jedis == null) {
        return false;
    }
    boolean success = false;
    try {
        Set<String> keys = jedis.keys(repository.name + ":*");
        if (keys.size() > 0) {
            Transaction t = jedis.multi();
            t.del(keys.toArray(new String[keys.size()]));
            t.exec();
        }
        success = true;
    } catch (JedisException e) {
        log.error("failed to delete all tickets in Redis @ " + getUrl(), e);
        pool.returnBrokenResource(jedis);
        jedis = null;
    } finally {
        if (jedis != null) {
            pool.returnResource(jedis);
        }
    }
    return success;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisException(redis.clients.jedis.exceptions.JedisException) Transaction(redis.clients.jedis.Transaction)

Aggregations

Transaction (redis.clients.jedis.Transaction)42 Test (org.junit.Test)32 Jedis (redis.clients.jedis.Jedis)19 Set (java.util.Set)7 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)6 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 Response (redis.clients.jedis.Response)3 ArrayList (java.util.ArrayList)2 JedisPool (redis.clients.jedis.JedisPool)2 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)2 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)2 TicketModel (com.gitblit.models.TicketModel)1 Change (com.gitblit.models.TicketModel.Change)1 ResourceMetadata (org.opennms.newts.cassandra.search.ResourceMetadata)1