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

About these ads

7 thoughts on “Getting Started With Jedis

  1. Pingback: Simple CRUD using Servlet 3.0, Redis/Jedis and CDI – Part 1 | Xico Junior's Weblog

  2. Thank you.
    Would have been better if you linked the word “Next Post” to that post so that we can read them in a sequence.

    • Hi Sushil,

      Thanks for your comment. You can find the links to next and previous post in the bar before the comments section. It contains the title linked to the next and previous posts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s