Working with Redis in Java

Source: Internet
Author: User

1  PackageCom.redis;2 3 Importjava.io.IOException;4 ImportJava.util.concurrent.ConcurrentHashMap;5 6 ImportRedis.clients.jedis.Jedis;7 Importredis.clients.jedis.Transaction;8 /**9 * Operating Redis in JavaTen  *  One  * @authorAdministrator A  * -  */ -  Public classRedistest { the      -     //Connect to Redis -     Private Static synchronizedJedis getjedis (String IP) { -         FinalJedis JR =NewJedis (IP); +Jr.auth ("@admin123"); -SYSTEM.OUT.PRINTLN ("Connection succeeded: \ t" +jr.ping ()); +         returnJr; A     } at     //Turn off Redis connections -     Private Static synchronized voidClosejedis (Jedis Jr) { - jr.close (); -     } -     //determine if a key exists -     Private Static Booleankeyisexist (Jedis jd,string key) { in         returnjd.exists (key); -     } to     //Delete Element +     Private Static voidDelkey (Jedis JD, String key) { - Jd.del (key); the     } *     //Action String $     Private Static synchronized voidOpstring (Jedis Jr) {Panax Notoginseng         if(Keyisexist (Jr, "Company"))) -Delkey (Jr, "Company")); theJr.set ("Company", "Oracle"); +System.out.println (Jr.get ("Company"))); A     } the     //Operation Hash +     Private Static voidOphash (Jedis Jr) { -         if(Keyisexist (Jr, "user1"))) $Delkey (Jr, "user1"); $concurrenthashmap<string,string> CHM =NewConcurrenthashmap<string, string>(); -Chm.put ("name", "Tom"); -Chm.put ("Age", "26"); theChm.put ("Major", "Computer"); -Jr.hmset ("user1", CHM);WuyiSystem.out.println (Jr.hgetall ("user1"). Get ("name"))); theSystem.out.println (Jr.hgetall ("user1"). Get ("Age")); -System.out.println (Jr.hgetall ("user1"). Get ("major")); Wu     } -     //Action list About     Private Static synchronized voidOplist (Jedis Jr) { $         if(Keyisexist (Jr, "Colorset"))) -Delkey (Jr, "Colorset"); -Jr.lpush ("Colorset", "Red", "blue", "yellow", "red"); -System.out.println (Jr.lrange ("Colorset", 0, 10)); A     } +     //Operation Set the     Private Static synchronized voidOpset (Jedis Jr) { -         if(Keyisexist (JR, "hobby"))) $Delkey (JR, "hobby"); theJr.sadd ("Hobby", "Running", "basketball", "volleyball", "volleyball"); theSystem.out.println (Jr.smembers ("hobby")); theSystem.out.println (Jr.smembers ("hobby"). Size ()); the     } -     //Operation Zset in     Private Static synchronized voidOpzset (Jedis Jr) { the         if(Keyisexist (Jr, "Hobby2"))) theDelkey (Jr, "Hobby2"); AboutJr.zadd ("Hobby2", 2.0, "Running"); theJr.zadd ("Hobby2", 2.0, "basketball"); theJr.zadd ("Hobby2", 2.0, "Volleyball"); theJr.zadd ("Hobby2", 2.0, "Volleyball"); +          for(String s:jr.zrangebyscore ("Hobby2", 0, 4)) - System.out.println (s); the     }Bayi     //Database Backup the     Private Static synchronized voidSave (Jedis Jr) { the Jr.save (); -     } -     //Transaction Control the     Private Static voidTrancontroller (Jedis Jr)throwsioexception{ theSystem.out.println ("************************************** Open transaction **************************************"); theTransaction trans =Jr.multi (); theSystem.out.println ("*********java Redis begins execution of transaction *********"); - trans.exec (); theSystem.out.println ("*********java Redis operating string*********"); the Opstring (JR); theSystem.out.println ("*********java Redis operating String over*********");94System.out.println ("*********java Redis operating hash*********"); the Ophash (JR); theSystem.out.println ("*********java Redis operating hash over*********"); theSystem.out.println ("*********java Redis operating list*********");98 Oplist (JR); AboutSystem.out.println ("*********java Redis operating List over*********"); -System.out.println ("*********java Redis operating set*********");101 Opset (JR);102System.out.println ("*********java Redis operating Set over*********");103System.out.println ("*********java Redis operating zset*********");104 Opzset (JR); theSystem.out.println ("*********java Redis operating Zset over*********");106System.out.println ("*********java Redis save Redis db*********");107 Save (JR);108System.out.println ("*********java Redis save Redis DB over*********");109 trans.close (); theSystem.out.println ("************************************** Close transaction **************************************");111System.out.println ("*********java Redis close Redis db connection*********"); the Closejedis (JR);113System.out.println ("*********java Redis close Redis db connection over*********"); the     } the      Public Static voidMain (string[] args)throwsioexception{ theJedis Jr = Getjedis ("localhost");117 Trancontroller (JR);118     }119}

Working with Redis in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.