java中操作redis

  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     // 数据库备份
 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 }

 

posted @ 2017-11-16 19:41  lvlin241  阅读(994)  评论(0编辑  收藏  举报