java中操作redis

來源:互聯網
上載者:User

標籤:存在   int   computer   row   cal   package   company   pac   public   

  1 package com.redis;  2   3 import java.io.IOException;  4 import java.util.concurrent.ConcurrentHashMap;  5   6 import redis.clients.jedis.Jedis;  7 import redis.clients.jedis.Transaction;  8 /**  9  * java中操作redis 10  *  11  * @author Administrator 12  * 13  */ 14 public class RedisTest { 15      16     // 串連redis 17     private static synchronized Jedis getJedis(String ip){ 18         final Jedis jr = new Jedis(ip); 19         jr.auth("@admin123"); 20         System.out.println("串連成功: \t"+jr.ping()); 21         return jr; 22     } 23     // 關閉redis串連 24     private static synchronized void closeJedis(Jedis jr){ 25         jr.close(); 26     } 27     // 判斷是否存在key  28     private static boolean keyIsExist(Jedis jd,String key){ 29         return jd.exists(key); 30     } 31     // 刪除元素 32     private static void delKey(Jedis jd, String key){ 33         jd.del(key); 34     } 35     // 操作String 36     private static synchronized void opString(Jedis jr){ 37         if(keyIsExist(jr,"company")) 38             delKey(jr,"company"); 39         jr.set("company", "oracle"); 40         System.out.println(jr.get("company")); 41     } 42     // 操作hash 43     private static void opHash(Jedis jr){ 44         if(keyIsExist(jr,"user1")) 45             delKey(jr,"user1"); 46         ConcurrentHashMap<String,String> chm = new ConcurrentHashMap<String, String>(); 47         chm.put("name", "Tom"); 48         chm.put("age", "26"); 49         chm.put("major", "computer"); 50         jr.hmset("user1", chm); 51         System.out.println(jr.hgetAll("user1").get("name")); 52         System.out.println(jr.hgetAll("user1").get("age")); 53         System.out.println(jr.hgetAll("user1").get("major")); 54     } 55     // 操作list 56     private static synchronized void opList(Jedis jr){ 57         if(keyIsExist(jr,"colorSet")) 58             delKey(jr,"colorSet"); 59         jr.lpush("colorSet", "red","blue","yellow","red"); 60         System.out.println(jr.lrange("colorSet", 0, 10)); 61     } 62     // 操作Set 63     private static synchronized void opSet(Jedis jr){ 64         if(keyIsExist(jr,"hobby")) 65             delKey(jr,"hobby"); 66         jr.sadd("hobby", "running","basketball","volleyball","volleyball"); 67         System.out.println(jr.smembers("hobby")); 68         System.out.println(jr.smembers("hobby").size()); 69     } 70     // 操作ZSet 71     private static synchronized void opZset(Jedis jr){ 72         if(keyIsExist(jr,"hobby2")) 73             delKey(jr,"hobby2"); 74         jr.zadd("hobby2",2.0, "running"); 75         jr.zadd("hobby2",2.0, "basketball"); 76         jr.zadd("hobby2",2.0, "volleyball"); 77         jr.zadd("hobby2",2.0, "volleyball"); 78         for(String s:jr.zrangeByScore("hobby2", 0, 4)) 79             System.out.println(s); 80     } 81     // Database Backup 82     private static synchronized void save(Jedis jr){ 83         jr.save(); 84     } 85     // 事務控制 86     private static void tranController(Jedis jr) throws IOException{ 87         System.out.println("**************************************開啟事務**************************************"); 88         Transaction trans = jr.multi(); 89         System.out.println("*********java Redis 開始執行事務*********"); 90         trans.exec(); 91         System.out.println("*********java Redis operating String*********"); 92         opString(jr); 93         System.out.println("*********java Redis operating String over*********"); 94         System.out.println("*********java Redis operating hash*********"); 95         opHash(jr); 96         System.out.println("*********java Redis operating hash over*********"); 97         System.out.println("*********java Redis operating List*********"); 98         opList(jr); 99         System.out.println("*********java Redis operating List over*********");100         System.out.println("*********java Redis operating Set*********");101         opSet(jr);102         System.out.println("*********java Redis operating Set over*********");103         System.out.println("*********java Redis operating zSet*********");104         opZset(jr);105         System.out.println("*********java Redis operating zSet over*********");106         System.out.println("*********java Redis save redis db*********");107         save(jr);108         System.out.println("*********java Redis save redis db over*********");109         trans.close();110         System.out.println("**************************************關閉事務**************************************");111         System.out.println("*********java Redis close redis db connection*********");112         closeJedis(jr);113         System.out.println("*********java Redis close redis db connection over*********");114     }115     public static void main(String[] args) throws IOException{116         Jedis jr = getJedis("localhost");117         tranController(jr);118     }119 }

 

java中操作redis

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.