There are huge amount of clients for Redis written in different programming languages.
I've already written some thoughts about Redis and today we will talk about one of the most popular Java client for Redis - Jedis. Jedis is a blazingly small and sane Redis java client and was conceived to be EASY to use.
One of the main pros of Jedis is using all Redis command names as the names of the functions and it really simplifies working with Jedis.
There's no big deal to install Redis server. You just have to download the distributive, unarchive it and run the redis-server. More information you can find here and here.
So, let's assume that you've already installed Redis and able to ping it using redis-cli. It means you are ready to start working with Jedis.
Simple connection to Redis can be established in the following way:
private static final int PORT = 6379;
private static final String HOST_NAME = "localhost";
// single instance
public static Jedis JEDIS = new Jedis(HOST_NAME, PORT);
It's very simple, isn't it? Another way to establish connection is to use JedisPool - pool of connections:
private void init() {
GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
poolConfig.maxActive = 100;
poolConfig.maxIdle = 10;
poolConfig.minIdle = 2;
poolConfig.maxWait = 100;
poolConfig.testWhileIdle = true;
poolConfig.testOnBorrow = true;
poolConfig.testOnReturn = true;
poolConfig.minEvictableIdleTimeMillis = 10000;
poolConfig.timeBetweenEvictionRunsMillis = 5000;
poolConfig.numTestsPerEvictionRun = 10;
// create JEDIS pool
this.jedisPool = new JedisPool(poolConfig, HOST_NAME, PORT);
}
Frankly speaking using the pool is preferable and I'd recommend to use the pool in production. In order to obtain Jedis object from the pool you should write something like this:
public Jedis getCache() {
return this.getJedisPool().getResource();
}
public void returnCache(Jedis cache) {
this.getJedisPool().returnResource(cache);
}
Don't forget about returning the Jedis instance back to the pool after doing some staff with it.
You can write few simple tests to see how Jedis is working and try to play with some Redis commands:
@Test
public void testSet() {
Jedis jedis = connectionFactory.getCache();
jedis.set("abc", "cba");
Assert.assertEquals("cba", jedis.get("abc"));
connectionFactory.returnCache(jedis);
}
@Test
public void testDelete() {
Jedis jedis = connectionFactory.getCache();
jedis.set("abc", "cba");
jedis.del("abc");
Assert.assertNull(jedis.get("abc"));
connectionFactory.returnCache(jedis);
}
@Test
public void testAddToList() {
Jedis jedis = connectionFactory.getCache();
jedis.rpush("items", "111");
jedis.rpush("items", "222");
//check the list size
Assert.assertEquals(new Long(2), jedis.llen("items"));
connectionFactory.returnCache(jedis);
}
As you can see it's very easy to connect to the Redis server using Jedis client.
I've already written some thoughts about Redis and today we will talk about one of the most popular Java client for Redis - Jedis. Jedis is a blazingly small and sane Redis java client and was conceived to be EASY to use.
One of the main pros of Jedis is using all Redis command names as the names of the functions and it really simplifies working with Jedis.
There's no big deal to install Redis server. You just have to download the distributive, unarchive it and run the redis-server. More information you can find here and here.
So, let's assume that you've already installed Redis and able to ping it using redis-cli. It means you are ready to start working with Jedis.
Simple connection to Redis can be established in the following way:
private static final int PORT = 6379;
private static final String HOST_NAME = "localhost";
// single instance
public static Jedis JEDIS = new Jedis(HOST_NAME, PORT);
It's very simple, isn't it? Another way to establish connection is to use JedisPool - pool of connections:
private void init() {
GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
poolConfig.maxActive = 100;
poolConfig.maxIdle = 10;
poolConfig.minIdle = 2;
poolConfig.maxWait = 100;
poolConfig.testWhileIdle = true;
poolConfig.testOnBorrow = true;
poolConfig.testOnReturn = true;
poolConfig.minEvictableIdleTimeMillis = 10000;
poolConfig.timeBetweenEvictionRunsMillis = 5000;
poolConfig.numTestsPerEvictionRun = 10;
// create JEDIS pool
this.jedisPool = new JedisPool(poolConfig, HOST_NAME, PORT);
}
Frankly speaking using the pool is preferable and I'd recommend to use the pool in production. In order to obtain Jedis object from the pool you should write something like this:
public Jedis getCache() {
return this.getJedisPool().getResource();
}
public void returnCache(Jedis cache) {
this.getJedisPool().returnResource(cache);
}
Don't forget about returning the Jedis instance back to the pool after doing some staff with it.
You can write few simple tests to see how Jedis is working and try to play with some Redis commands:
@Test
public void testSet() {
Jedis jedis = connectionFactory.getCache();
jedis.set("abc", "cba");
Assert.assertEquals("cba", jedis.get("abc"));
connectionFactory.returnCache(jedis);
}
@Test
public void testDelete() {
Jedis jedis = connectionFactory.getCache();
jedis.set("abc", "cba");
jedis.del("abc");
Assert.assertNull(jedis.get("abc"));
connectionFactory.returnCache(jedis);
}
@Test
public void testAddToList() {
Jedis jedis = connectionFactory.getCache();
jedis.rpush("items", "111");
jedis.rpush("items", "222");
//check the list size
Assert.assertEquals(new Long(2), jedis.llen("items"));
connectionFactory.returnCache(jedis);
}
As you can see it's very easy to connect to the Redis server using Jedis client.
It's intuitive, clear and simple, but the most important thing is - it's fast. You might want to take a look at Demo project and try to run a test for bulk loading.
You will be impressed. Hopefully, you'll have a fun.



