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

The following examples show how to use redis.clients.jedis.Jedis#auth() . 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: ScriptingCommandsTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void scriptExistsWithBrokenConnection() {
  Jedis deadClient = new Jedis(jedis.getClient().getHost(), jedis.getClient().getPort());
  deadClient.auth("foobared");

  deadClient.clientSetname("DEAD");

  ClientKillerUtil.killClient(deadClient, "DEAD");

  // sure, script doesn't exist, but it's just for checking connection
  try {
    deadClient.scriptExists("abcdefg");
  } catch (JedisConnectionException e) {
    // ignore it
  }

  assertEquals(true, deadClient.getClient().isBroken());

  deadClient.close();
}
 
Example 2
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void getNumActiveReturnsTheCorrectNumber() {
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000);
  Jedis jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.set("foo", "bar");
  assertEquals("bar", jedis.get("foo"));

  assertEquals(1, pool.getNumActive());

  Jedis jedis2 = pool.getResource();
  jedis.auth("foobared");
  jedis.set("foo", "bar");

  assertEquals(2, pool.getNumActive());

  jedis.close();
  assertEquals(1, pool.getNumActive());

  jedis2.close();

  assertEquals(0, pool.getNumActive());

  pool.destroy();
}
 
Example 3
Source File: TransactionCommandsTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloseable() throws IOException {
  // we need to test with fresh instance of Jedis
  Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
  jedis2.auth("foobared");

  Transaction transaction = jedis2.multi();
  transaction.set("a", "1");
  transaction.set("b", "2");

  transaction.close();

  try {
    transaction.exec();
    fail("close should discard transaction");
  } catch (JedisDataException e) {
    assertTrue(e.getMessage().contains("EXEC without MULTI"));
    // pass
  }
}
 
Example 4
Source File: TransactionCommandsTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testResetStateWithFullyExecutedTransaction() {
  Jedis jedis2 = new Jedis(jedis.getClient().getHost(), jedis.getClient().getPort());
  jedis2.auth("foobared");

  Transaction t = jedis2.multi();
  t.set("mykey", "foo");
  t.get("mykey");

  List<Object> resp = t.exec();
  assertNotNull(resp);
  assertEquals(2, resp.size());

  jedis2.resetState();
  jedis2.close();
}
 
Example 5
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 6
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 7
Source File: JedisTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void startWithUrlString() {
  Jedis j = new Jedis("localhost", 6380);
  j.auth("foobared");
  j.select(2);
  j.set("foo", "bar");
  Jedis jedis = new Jedis("redis://:foobared@localhost:6380/2");
  assertEquals("PONG", jedis.ping());
  assertEquals("bar", jedis.get("foo"));
}
 
Example 8
Source File: JedisCommandTestBase.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
protected Jedis createJedis() {
  Jedis j = new Jedis(hnp.getHost(), hnp.getPort());
  j.connect();
  j.auth("foobared");
  j.flushAll();
  return j;
}
 
Example 9
Source File: TestSubscribe.java    From Redis_Learning with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubscribe() throws Exception{
	Jedis jedis = new Jedis("192.168.65.130", 6379); 
	jedis.auth("redis");
       RedisMsgPubSubListener listener = new RedisMsgPubSubListener();  
       jedis.subscribe(listener, "redisChatTest");  
       // other code
}
 
Example 10
Source File: RedisUtil.java    From RCT with Apache License 2.0 5 votes vote down vote up
public RedisUtil(String host, int port, String password) {
	hostAndPort = new HostAndPort(host, port);
	this.password = password;
	jedis = new Jedis(host, port);
	if (StringUtils.isNotBlank(password)) {
		jedis.auth(password);
	}
}
 
Example 11
Source File: PipeliningTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloseableWithMulti() throws IOException {
  // we need to test with fresh instance of Jedis
  Jedis jedis2 = new Jedis(hnp.getHost(), hnp.getPort(), 500);
  jedis2.auth("foobared");

  Pipeline pipeline = jedis2.pipelined();
  Response<String> retFuture1 = pipeline.set("a", "1");
  Response<String> retFuture2 = pipeline.set("b", "2");

  pipeline.multi();

  pipeline.set("a", "a");
  pipeline.set("b", "b");

  pipeline.close();

  try {
    pipeline.exec();
    fail("close should discard transaction");
  } catch (JedisDataException e) {
    assertTrue(e.getMessage().contains("EXEC without MULTI"));
    // pass
  }

  // it shouldn't meet any exception
  retFuture1.get();
  retFuture2.get();
}
 
Example 12
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void checkConnectionWithDefaultPort() {
  JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
  Jedis jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.set("foo", "bar");
  assertEquals("bar", jedis.get("foo"));
  jedis.close();
  pool.destroy();
  assertTrue(pool.isClosed());
}
 
Example 13
Source File: RedisServerClientConnectionManager.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Override
public ParamServerClientConnection getConnection()
{
    Jedis redisClient = new Jedis(host, port, ssl);
    if (password.isPresent()) {
        redisClient.auth(password.get());
    }

    redisClient.connect();
    return new RedisServerClientConnection(redisClient);
}
 
Example 14
Source File: VisageDistributor.java    From Visage with MIT License 5 votes vote down vote up
public Jedis getJedis() {
	Jedis j = pool.getResource();
	if (password != null) {
		j.auth(password);
	}
	return j;
}
 
Example 15
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 16
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void startWithUrlString() {
  Jedis j = new Jedis("localhost", 6380);
  j.auth("foobared");
  j.select(2);
  j.set("foo", "bar");
  JedisPool pool = new JedisPool("redis://:foobared@localhost:6380/2");
  Jedis jedis = pool.getResource();
  assertEquals("PONG", jedis.ping());
  assertEquals("bar", jedis.get("foo"));
}
 
Example 17
Source File: JedisPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void startWithUrl() throws URISyntaxException {
  Jedis j = new Jedis("localhost", 6380);
  j.auth("foobared");
  j.select(2);
  j.set("foo", "bar");
  JedisPool pool = new JedisPool(new URI("redis://:foobared@localhost:6380/2"));
  Jedis jedis = pool.getResource();
  assertEquals("PONG", jedis.ping());
  assertEquals("bar", jedis.get("foo"));
}
 
Example 18
Source File: RedisRangeManager.java    From magic-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void init() {
	checkParam();
	jedis = new Jedis(ip, port);
	if (StrUtil.isNotBlank(password)) {
		jedis.auth(password);
	}
}
 
Example 19
Source File: RedisBitSetTest.java    From bloomfilter with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {

        //Don't forget auth password, you better use the configured redis client connection.
        //It should be noted that bloomfilter is not responsible for closing and returning redis connection resources.

        //(falsePositiveProbability, expectedNumberOfElements)
        BloomFilter<String> filter = new BloomFilter<String>(0.0001, 10000);
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.auth("1234");
        filter.bind(new RedisBitSet(jedis, "bloomfilter:key:name"));

        //if you have a redis cluster
        //Set<HostAndPort> nodes = new HashSet<>();
        //nodes.add(new HostAndPort("127.0.0.1", 6379));

        //filter.bind(new RedisBitSet(new JedisCluster(nodes), "bloomfilter:key:name"));

        //you can also use jedispool
        //JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
        //Jedis jedis = jedisPool.getResource();
        //filter.bind(new RedisBitSet(jedis, "bloomfilter:key:name"));

        filter.add("filter");
        System.out.println(filter.contains("filter"));
        System.out.println(filter.contains("bloom"));
        filter.add("bitset");
        filter.add("redis");
        System.out.println(filter.contains("bitset"));
        System.out.println(filter.contains("redis"));
        System.out.println(filter.contains("mysql"));
        System.out.println(filter.contains("linux"));
        System.out.println(filter.count());
        System.out.println(filter.isEmpty());
        filter.clear();
        System.out.println(filter.isEmpty());
        System.out.println(filter.contains("filter"));

        /**
         Test results:
         true
         false
         true
         true
         false
         false
         3
         false
         true
         false
         */
    }
 
Example 20
Source File: JedisTest.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
@Test
public void useWithoutConnecting() {
  Jedis jedis = new Jedis("localhost");
  jedis.auth("foobared");
  jedis.dbSize();
}