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.
Related Whitepaper:

Professional NoSQL

A hands-on guide to leveraging NoSQL databases!

NoSQL databases are an efficient and powerful tool for storing and manipulating vast quantities of data. Most NoSQL databases scale well as data grows. In addition, they are often malleable and flexible enough to accommodate semi-structured and sparse data sets. This comprehensive hands-on guide presents fundamental concepts and practical solutions for getting you ready to use NoSQL databases. Expert author Shashank Tiwari begins with a helpful introduction on the subject of NoSQL, explains its characteristics and typical uses, and looks at where it fits in the application stack. Unique insights help you choose which NoSQL solutions are best for solving your specific data storage needs.

Get it Now!  

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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books