About Francisco Ribeiro Junior

Francisco is a senior software engineer working in the telecom/banking domain focused on Web Content Management. He has been technical lead on many projects for different Brazilian and worldwide clients.

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!

 

Reference: Getting Started With Jedis from our JCG partner Francisco Ribeiro Junior at the XICO JUNIOR’S WEBLOG blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Getting Started With Jedis"

  1. shubhada M says:

    nice post to begin with jedis…Thanx:)

Leave a Reply


seven − 5 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close