Skip to content

Instantly share code, notes, and snippets.

@vedraiyani
Created June 23, 2021 12:13
Show Gist options
  • Save vedraiyani/f9c91bbcc2fe5873a2fdb3d4b07a3e72 to your computer and use it in GitHub Desktop.
Save vedraiyani/f9c91bbcc2fe5873a2fdb3d4b07a3e72 to your computer and use it in GitHub Desktop.
Use Redis in java
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