Java Code Examples for redis.clients.jedis.Jedis#disconnect()

The following examples show how to use redis.clients.jedis.Jedis#disconnect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PipelinedGetSetBenchmark.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws UnknownHostException, IOException {
  Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
  jedis.connect();
  jedis.auth("foobared");
  jedis.flushAll();

  long begin = Calendar.getInstance().getTimeInMillis();

  Pipeline p = jedis.pipelined();
  for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
    String key = "foo" + n;
    p.set(key, "bar" + n);
    p.get(key);
  }
  p.sync();

  long elapsed = Calendar.getInstance().getTimeInMillis() - begin;

  jedis.disconnect();

  System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
 
Example 2
Source File: GetSetBenchmark.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws UnknownHostException, IOException {
  Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort());
  jedis.connect();
  jedis.auth("foobared");
  jedis.flushAll();

  long begin = Calendar.getInstance().getTimeInMillis();

  for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
    String key = "foo" + n;
    jedis.set(key, "bar" + n);
    jedis.get(key);
  }

  long elapsed = Calendar.getInstance().getTimeInMillis() - begin;

  jedis.disconnect();

  System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
 
Example 3
Source File: TestRedisServer.java    From redis-mock with MIT License 6 votes vote down vote up
@Test
public void testSlave() throws IOException {
    RedisServer master = RedisServer.newRedisServer();
    RedisServer slave = RedisServer.newRedisServer();
    master.setSlave(slave);
    master.start();
    slave.start();
    Jedis jedis1 = new Jedis(master.getHost(), master.getBindPort());
    Jedis jedis2 = new Jedis(slave.getHost(), slave.getBindPort());
    assertEquals(jedis1.set("ab", "cd"), "OK");
    assertEquals(jedis2.get("ab"), "cd");
    jedis1.disconnect();
    jedis2.disconnect();
    master.stop();
    slave.stop();
}
 
Example 4
Source File: TestRedisServer.java    From redis-mock with MIT License 6 votes vote down vote up
@Test
public void testDelInMasterSlave() throws IOException {
    RedisServer master = RedisServer.newRedisServer();
    RedisServer slave = RedisServer.newRedisServer();
    master.setSlave(slave);
    master.start();
    slave.start();
    Jedis jedis1 = new Jedis(master.getHost(), master.getBindPort());
    Jedis jedis2 = new Jedis(slave.getHost(), slave.getBindPort());
    assertEquals(jedis1.set("a", "b"), "OK");
    assertEquals(jedis1.del("a"), (Long) 1L);
    assertEquals(jedis2.exists("a"), false);
    jedis1.disconnect();
    jedis2.disconnect();
    master.stop();
    slave.stop();
}
 
Example 5
Source File: TestRedisServer.java    From redis-mock with MIT License 6 votes vote down vote up
@Test
public void testExpireInMasterSlave() throws IOException {
    RedisServer master = RedisServer.newRedisServer();
    RedisServer slave = RedisServer.newRedisServer();
    master.setSlave(slave);
    master.start();
    slave.start();
    Jedis jedis1 = new Jedis(master.getHost(), master.getBindPort());
    Jedis jedis2 = new Jedis(slave.getHost(), slave.getBindPort());
    assertEquals(jedis1.set("a", "b"), "OK");
    assertEquals(jedis1.expire("a", 1), (Long) 1L);
    assertEquals(jedis2.ttl("a"), (Long) 1L);
    jedis1.disconnect();
    jedis2.disconnect();
    master.stop();
    slave.stop();
}
 
Example 6
Source File: TestRedisServer.java    From redis-mock with MIT License 6 votes vote down vote up
@Test
public void testExpireAtInMasterSlave() throws IOException {
    RedisServer master = RedisServer.newRedisServer();
    RedisServer slave = RedisServer.newRedisServer();
    master.setSlave(slave);
    master.start();
    slave.start();
    Jedis jedis1 = new Jedis(master.getHost(), master.getBindPort());
    Jedis jedis2 = new Jedis(slave.getHost(), slave.getBindPort());
    assertEquals(jedis1.set("a", "b"), "OK");
    long now = System.currentTimeMillis() / 1000;
    assertEquals(jedis1.expireAt("a", now + 5), (Long) 1L);
    assertEquals(jedis2.ttl("a"), (Long) 5L);
    jedis1.disconnect();
    jedis2.disconnect();
    master.stop();
    slave.stop();
}
 
Example 7
Source File: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testNormal() {// 12.526秒
	Jedis jedis = new Jedis("120.25.241.144", 6379);
	jedis.auth("b840fc02d52404542994");

	long start = System.currentTimeMillis();
	for (int i = 0; i < 1000; i++) {
		jedis.set("n" + i, "n" + i);
		System.out.println(i);
	}
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	jedis.disconnect();
	try {
		Closeables.close(jedis, true);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example 8
Source File: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testTrans() {// 0.304秒
	Jedis jedis = new Jedis("120.25.241.144", 6379);
	jedis.auth("b840fc02d52404542994");

	long start = System.currentTimeMillis();
	Transaction tx = jedis.multi();
	for (int i = 0; i < 1000; i++) {
		tx.set("n" + i, "n" + i);
		System.out.println(i);
	}
	tx.exec();
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	jedis.disconnect();
	try {
		Closeables.close(jedis, true);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 9
Source File: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testPipelined() {// 0.076秒
	Jedis jedis = new Jedis("120.25.241.144", 6379);
	jedis.auth("b840fc02d52404542994");

	long start = System.currentTimeMillis();
	Pipeline pipeline = jedis.pipelined();
	for (int i = 0; i < 1000; i++) {
		pipeline.set("n" + i, "n" + i);
		System.out.println(i);
	}
	pipeline.syncAndReturnAll();
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	jedis.disconnect();
	try {
		Closeables.close(jedis, true);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 10
Source File: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testCombPipelineTrans() {// 0.099秒
	Jedis jedis = new Jedis("120.25.241.144", 6379);
	jedis.auth("b840fc02d52404542994");

	long start = System.currentTimeMillis();
	Pipeline pipeline = jedis.pipelined();
	pipeline.multi();
	for (int i = 0; i < 1000; i++) {
		pipeline.set("n" + i, "n" + i);
		System.out.println(i);
	}
	pipeline.exec();
	pipeline.syncAndReturnAll();
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	jedis.disconnect();
	try {
		Closeables.close(jedis, true);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 11
Source File: SingleRedis.java    From ns4_frame with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] brpop(final String queueName, final int timeout) throws MQRedisException {
    boolean runable = true;
    final Jedis jedis = this.jedisPool.getResource();
    List<byte[]> result = null;
    try {
        FutureTask<List<byte[]>> brpopTask = new FutureTask<>(new Callable<List<byte[]>>() {
            @Override
            public List<byte[]> call() throws Exception {
                return jedis.brpop(timeout, SafeEncoder.encode(queueName));
            }
        });
        executor.execute(brpopTask);
        result = brpopTask.get(timeout + 2, TimeUnit.SECONDS);
    } catch (Exception e) {
        if (e instanceof TimeoutException || e instanceof InterruptedException) {
            if (jedis != null) {
                jedis.disconnect();
                runable = false;
                jedisPool.returnBrokenResource(jedis);
            }
        }
        commandLog.error("mq brpop error", e);
        throw new MQRedisException(e);
    } finally {
        if (jedis != null && runable) {
            jedis.close();
        }
    }
    if (result != null) {
        return result.get(1);
    } else {
        return null;
    }
}
 
Example 12
Source File: PoolBenchmark.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  Jedis j = new Jedis(hnp.getHost(), hnp.getPort());
  j.connect();
  j.auth("foobared");
  j.flushAll();
  j.quit();
  j.disconnect();
  long t = System.currentTimeMillis();
  // withoutPool();
  withPool();
  long elapsed = System.currentTimeMillis() - t;
  System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
}
 
Example 13
Source File: RedisContainer.java    From pay-publicapi with MIT License 5 votes vote down vote up
public void clearRedisCache() {
    Jedis jedis = new Jedis(host, port);
    String response = jedis.flushAll();
    if (!response.equals("OK")) {
        logger.warn("Unexpected response from redis flushAll command: " + response);
    }
    jedis.disconnect();
}
 
Example 14
Source File: JedisSupport.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
public void writeData(String key, String val) throws Exception {
    Jedis client = getRedisClient(redis);
    try {
        client.set(key, val);
    } finally {
        client.disconnect();
    }
}
 
Example 15
Source File: JedisSupport.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
public String readData(String key) throws Exception {
    Jedis client = getRedisClient(redis);
    try {
        return client.get(key);
    } finally {
        client.disconnect();
    }
}
 
Example 16
Source File: RedisDatabaseHandler.java    From netphony-topology with Apache License 2.0 5 votes vote down vote up
public boolean write(String key, String json){
   	
   	//System.out.println("WRITING IN "+host+" port "+port);

   	Jedis jedis = new Jedis(host,port);
    jedis.connect();
    
    String ret = jedis.set(key, json);
    jedis.sadd("TEDB",key);
    jedis.disconnect();
    return true;
}