Created
June 23, 2021 12:13
-
-
Save vedraiyani/f9c91bbcc2fe5873a2fdb3d4b07a3e72 to your computer and use it in GitHub Desktop.
Use Redis in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import redis.clients.jedis.Jedis; | |
public class RedisJava { | |
public static void main(String[] args) { | |
// Connecting to Redis server on localhost | |
Jedis jedis = new Jedis("localhost", 8083); | |
System.out.println("Connection to server sucessfully"); | |
// check whether server is running or not | |
System.out.println("Server is running: " + jedis.ping()); | |
// store data in redis list | |
jedis.lpush("tutorial-list", "Redis"); | |
jedis.lpush("tutorial-list", "Mongodb"); | |
jedis.lpush("tutorial-list", "Mysql"); | |
// Get the stored data and print it | |
List<String> list = jedis.lrange("tutorial-list", 0, 5); | |
for (int i = 0; i < list.size(); i++) { | |
System.out.println("Stored string in redis:: " + list.get(i)); | |
} | |
// store data in redis list | |
// Get the stored data and print it | |
List<String> keylist = new ArrayList<>(jedis.keys("*")); | |
for (int i = 0; i < keylist.size(); i++) { | |
System.out.println("List of stored keys:: " + keylist.get(i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment