Redis Jedis – How to perform CRUD operations on key using jedis library

1 month ago Lalit Bhagtani 0

In this tutorial, we will learn about how to perform CRUD operation on key using Jedis library.

Jedis Library

Jedis is a Java client library for redis datastore. It is small and very easy to use, and fully compatible with redis 2.8.x, 3.x.x and above datastore. You can find here more information about jedis library.

Keys

In Redis, Keys are used as a unique identifier to store, manage and retrieved a value stored in the datastore. You can find here more information about Keys commands.

Project Setup

Create a simple maven project in your favorite IDE and add below mentioned dependency in your pom.xml file.

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>3.0.1</version>
</dependency>

For latest version of jedis library, check this page.

Redis Installation

You will need to install latest version of Redis. Check this page for more information about installing redis.

Jedis Connection

Create an object of Jedis ( redis.clients.jedis.Jedis ) class for connecting your java code to redis.

Jedis jedis = new Jedis();

If you have started one redis service in your local machine and on default port (6379) then default constructor will just work fine. Otherwise you have to pass correct host url and port no. as an argument into the constructor.

Set Timestamp on Key

There are four methods for setting expiration time on key. They are as as follows :-

  1. expire :- It set an expiration time on key in seconds.
    /* Set expiration time on <key-1> in seconds */
    jedis.expire("key-1", 300);
  2. pexpire :- It set an expiration time on key in milliseconds.
    /* Set expiration time of <key-1> in milliseconds */
    jedis.pexpire("key-1", 30000);
  3. expireat :- It set an expiration time on key in Unix Timestamp in seconds.
    /* Set expiration time of <key-1> in unix timestamp in seconds */
    jedis.expireAt("key-1", 1543622400);
  4. pexpireat :- It set an expiration time on key in Unix Timestamp in milliseconds.
    /* Set expiration time of <key-1> in unix timestamp in milliseconds */
    jedis.pexpireAt("key-1", 154362240000);

Get Timestamp of Key

There are two methods for getting expiration time of key. They are as as follows :-

  1. ttl :- It returns expiration time of key in seconds.
    /* Returns expiration time of <key-1> in seconds */
    jedis.ttl("key-1");
  2. pttl :- It returns expiration time of key in milliseconds.
    /* Returns expiration time of <key-1> in milliseconds */
    jedis.pttl("key-1");

Remove Timestamp 

persist method removes the expiration time of a key stored in redis datastore.

/* Removes timestamp of <key-1> */
jedis.persist("key-1");

Get All 

key method returns one or more keys that matches a specified pattern.

/* Returns all the keys stored in datastore */
jedis.keys("*");
		
/* Returns all the keys that starts with a and ends with b */
jedis.keys("a*b");

Delete 

del method is used to delete a key from redis datastore.

/* Deletes <key-1> from datastore */
jedis.del("key-1");
		
/* Deletes <key-1> <key-2> <key-3> from datastore */
jedis.del("key-1", "key-2", "key-3");

Exists

exists method is used to check, if a key exists in redis datastore or not.  

/* Check if <key-1> exist in datastore or not */
Boolean exist = jedis.exists("key-1");

/* Check if <key-1>, <key-2>, <key-3> exist in datastore or not */
Long exist = jedis.exists("key-1", "key-2", "key-3");
/* 1 is returned if any one of then is present otherwise 0 is returned. */

Type 

type method is used to get the datatype of a value store in the key.

/* Returns datatype of the value store at <key-1> */
jedis.type("key-1");

Dump 

dump method returns serialized version of value stored at the key.

/* Returns serialized value stored at <key-1> */
jedis.dump("key-1");

Rename

There are two methods for renaming the key stored in datastore. They are as as follows :-

  1. rename :- It rename the key from old name to new name. If a key with new name already exists, it will be overwritten and key with old name will be deleted otherwise key with old name will be changed to new name.
    /* Rename key from <old-key> to <new-key> */
    jedis.rename("old-key", "new-key");
  2. renamenx :- It rename the key from old name to new name only if key with new name does not exist.
    /* Rename key from <old-key> to <new-key> */
    jedis.renamenx("old-key", "new-key");

References :-

  1. Key Command Docs

That’s all for how to perform CRUD operation on key using Jedis library. If you liked it, please share your thoughts in comments section and share it with others too.