Java Use Redis
Installation
Before we start using Redis in Java, we need to make sure that we have the Redis service and the Java Redis driver installed and that Java is working properly on your machine. Java installation configuration can refer to our Java Development environment configuration next let's install the Java Redis driver:
- First you need to download the driver package, download the Jedis.jar, and make sure to download the latest driver package.
- Include the driver package in your classpath.
ImportRedis.clients.jedis.Jedis;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Set;/*** Created by Sherry on 16/9/2.*/ Public classredisclient { Public Static voidMain (string[] args) {//connecting to a serverJedis Jedis =NewJedis ("127.0.0.1", 6379); //Verify PasswordJedis.auth ("123456"); System.out.println ("Connection to Server sucessfully"); System.out.println ("Server is running:" +jedis.ping ());//Redis Java String (string) instance//setting up Redis string DataJedis.set ("W3ckey", "Redis Tutorial"); //get the stored data and outputSystem.out.println ("Stored string in Redis::" + jedis.get ("W3ckey"));//Redis Java List (list) instance//storing data in a listJedis.lpush ("Tutorial-list", "Redis"); Jedis.lpush ("Tutorial-list", "Mongodb"); Jedis.lpush ("Tutorial-list", "Mysql"); //get the stored data and outputlist<string> list = Jedis.lrange ("Tutorial-list", 0, 5); for(inti=0; I<list.size (); i++) {System.out.println ("Stored string in Redis::" +List.get (i)); }//Redis Java Keys instance//get the data and outputset<string> sets = Jedis.keys ("*"); for(Iterator<string> Iterator =sets.iterator (); Iterator.hasnext ();) {System.out.println ("Keys:" +Iterator.next ()); } }}
Using Redis for Java