Getting Started With Jedis

Hi,

These days I started looking into Redis. I’ve heard a lot about it so I decided to have a try.

Redis is defined in its websites as “an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets”.

We can find very good examples on where Redis is a good fit on the Shades of Gray blog.

In this post we will not focus on the features and capabilities of Redis, we will have a quick look at Jedis, a Java Redis Client.

The Redis commands are very simple to learn, the Jedis api, it is also very simple to learn.

We can download the Jedis on its github repository (https://github.com/xetorthio/jedis). It is a simple jar that we can just add it to our application and start using it.

Below we can see a simple example of the Jedis api:

import redis.clients.jedis.Jedis;
public class TestJedis {

public static void main(String[] args) {
  //Connecting to Redis on localhost
  Jedis jedis = new Jedis("localhost");
  //adding a new key
  jedis.set("key", "value");
  //getting the key value
  System.out.println(jedis.get("key"));

 }

}

On the example above, we create a connection to the redis server, once we connect we add a new key on the datastore, using the method set, this method will call the SET command on Redis. With that we created a new key called “key” with a value “value”.Once we have a value for that key, we can get its value using the method get which calls the GET command on Redis.

A very useful command is the INCR that increments a key value. We can see an example of using this command below:

import redis.clients.jedis.Jedis;
public class IncrTest {

public static void main(String[] args) {
 Jedis jedis = new Jedis("localhost");
 System.out.println(jedis.get("counter"));
 jedis.incr("counter");
 System.out.println(jedis.get("counter"));
 }

}

One of the good use cases for Redis is caching. We can use it as caching system, it is very good for that because we can set a expiration time for a given key through the EXPIRE commad. Also we can get the TTL for the key using the TTL command. Below we can see an example with Jedis api.

import redis.clients.jedis.Jedis;
public class TestJedis {

public static void main(String[] args) throws InterruptedException {
 String cacheKey = "cachekey";
 Jedis jedis = new Jedis("localhost");
 //adding a new key
 jedis.set(cacheKey, "cached value");
 //setting the TTL in seconds
 jedis.expire(cacheKey, 15);
 //Getting the remaining ttl
 System.out.println("TTL:" + jedis.ttl(cacheKey));
 Thread.sleep(1000);
 System.out.println("TTL:" + jedis.ttl(cacheKey));
 //Getting the cache value
 System.out.println("Cached Value:" + jedis.get(cacheKey));

 //Wait for the TTL finishs
 Thread.sleep(15000);

 //trying to get the expired key
 System.out.println("Expired Key:" + jedis.get(cacheKey));
 }

}

Also redis can store some other values like Lists, Hashs, set and others. Below we can see an example of using Sets in Redis.

import redis.clients.jedis.Jedis;
public class TestJedis {

public static void main(String[] args) {
 String cacheKey = "languages";
 Jedis jedis = new Jedis("localhost");
 //Adding a set as value
 jedis.sadd(cacheKey,"Java","C#","Python");//SADD

 //Getting all values in the set: SMEMBERS
 System.out.println("Languages: " + jedis.smembers(cacheKey));
 //Adding new values
 jedis.sadd(cacheKey,"Java","Ruby");
 //Getting the values... it doesn't allow duplicates
 System.out.println("Languages: " + jedis.smembers(cacheKey));

 }

}

There are many other use cases for Redis, this post was intended to give an little introduction to the Jedis library. See you in the next post