在Redis的官網上,咱們能夠看到Redis的Java客戶端衆多html
其中,Jedis是Redis官方推薦,也是使用用戶最多的Java客戶端。java
繼續以前,請務必閱讀過Redis入門教程(二)—基本數據類型,這將對你有很大的幫助。redis
import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.junit.Before; import org.junit.Test; import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.jedis.Jedis; /** * @author dotleo * */ public class Jedis_Test { //Java中操做Redis的對象 private Jedis jedis ; @Before public void connection() { //鏈接Redis服務器,參數1爲ip,參數2爲端口號,請根據本身實際狀況賦值 jedis = new Jedis("192.168.1.233",6379); } /** * redis字符串操做 */ @Test public void stringTest() { //爲避免屢次運行Redis中存在鍵,致使結果不符合預期,每次執行時清理當前庫 //特別注意:在開發中請勿如此操做 jedis.flushDB(); //添加字符串 jedis.set("age", "101"); System.out.println("age = " + jedis.get("age")); //批量添加字符串 jedis.mset("age1","1","age2","2"); System.out.println("age1 = " + jedis.get("age1")); System.out.println("age2 = " + jedis.get("age2")); //添加字符串(僅在不存在時) jedis.setnx("price", "101"); System.out.println("price = " + jedis.get("price")); //加1操做 jedis.incr("price"); System.out.println("price = " + jedis.get("price")); //拼接字符串 jedis.append("age", "years"); System.out.println("age = " + jedis.get("age")); //截取字符串 String str = jedis.getrange("age", 0L, 5L); System.out.println("age[0,5] = " + str); //獲取字符串長度 Long len = jedis.strlen("age"); System.out.println("age length = " + len); //刪除字符串 jedis.del("age"); System.out.print("age = " + jedis.get("age")); } /** * redis哈希操做 */ @Test public void hashTest() { //爲避免屢次運行Redis中存在鍵,致使結果不符合預期,每次執行時清理當前庫 //特別注意:在開發中請勿如此操做 jedis.flushDB(); //添加值 jedis.hset("student", "name", "zhangsan"); //添加值(僅在不存在時) jedis.hsetnx("student", "age", "12"); //批量添加值 Map<String,String> map = new HashMap<String,String>(); map.put("sex", "boy"); map.put("address", "beijing"); jedis.hmset("student", map); //獲取值 String str = jedis.hget("student", "name"); System.out.println("student:name = " + str); //批量獲取值 List<String> list = jedis.hmget("student", "name","age"); System.out.println("student:name = " + list.get(0)); System.out.println("student:age = " + list.get(1)); //獲取key Set<String> set = jedis.hkeys("student"); System.out.println("student keys:"); for (String string : set) { System.out.println(string); } //獲取值 list = jedis.hvals("student"); System.out.println("student vals:"); for (String string : list) { System.out.println(string); } //獲取key和值 map = jedis.hgetAll("student"); System.out.println("student keys vals:"); for (String key:map.keySet()) { System.out.println(key + " = " + map.get(key)); } //刪除值 jedis.hdel("student", "sex"); System.out.println("student:sex = " + jedis.hget("student", "sex")); //獲取長度 Long len = jedis.hlen("student"); System.out.println("student length = " + len); } /** * redis列表操做 */ @Test public void listTest() { //爲避免屢次運行Redis中存在鍵,致使結果不符合預期,每次執行時清理當前庫 //特別注意:在開發中請勿如此操做 jedis.flushDB(); //表頭添加 jedis.lpush("student", "xiaoli","xiaowang","xiaoliu","xiaozhang"); //表尾添加 jedis.rpush("student", "zhangsan","lisi","wangwu"); //經過索引獲取元素 String str = jedis.lindex("student", 1L); System.out.println("student 1st = " + str); //在元素前或者後插入元素 jedis.linsert("student", LIST_POSITION.BEFORE, "xiaozhang", "zhangsan"); //從表頭彈出 str = jedis.lpop("student"); System.out.println("student first = " + str); //從表尾彈出 str = jedis.rpop("student"); System.out.println("student end = " + str); //刪除 jedis.lrem("student", 2, "zhangsan"); //獲取全部值 List<String> list = jedis.lrange("student", 0L, -1L); System.out.println("student vals:"); for (String string : list) { System.out.println(string); } } /** * redis集合操做 */ @Test public void setTest() { //爲避免屢次運行Redis中存在鍵,致使結果不符合預期,每次執行時清理當前庫 //特別注意:在開發中請勿如此操做 jedis.flushDB(); //添加成員 jedis.sadd("student", "zhangsan","lisi"); jedis.sadd("monitor","wangwu","zhangsan"); //獲取成員數 Long count = jedis.scard("student"); System.out.println("student count = " + count); //獲取成員 Set<String> set = jedis.smembers("student"); System.out.println("student members:"); for (String string : set) { System.out.println(string); } //是否爲該集合的成員 boolean bool = jedis.sismember("student", "zhangsan"); System.out.println("member zhangsan exist?\n" + bool); //交集 set = jedis.sinter("student","monitor"); System.out.println("student inter monitor members:"); for (String string : set) { System.out.println(string); } //並集 set = jedis.sunion("student","monitor"); System.out.println("student union monitor members:"); for (String string : set) { System.out.println(string); } //補集 set = jedis.sdiff("student","monitor"); System.out.println("student diff monitor members:"); for (String string : set) { System.out.println(string); } //隨機返回成員 String str = jedis.srandmember("student"); System.out.println("random member:\n" + str); //移動成員 jedis.smove("student", "monitor", "lisi"); //刪除成員 jedis.srem("monitor", "zhangsan"); //顯示 set = jedis.smembers("student"); System.out.println("student members:"); for (String string : set) { System.out.println(string); } set = jedis.smembers("monitor"); System.out.println("monitor members:"); for (String string : set) { System.out.println(string); } } /** * redis有序集合操做 */ @Test public void sortSetTest() { //爲避免屢次運行Redis中存在鍵,致使結果不符合預期,每次執行時清理當前庫 //特別注意:在開發中請勿如此操做 jedis.flushDB(); //添加成員 Map<Double,String> map = new HashMap<Double,String>(); map.put(50d, "zhangsan"); map.put(60d, "lisi"); map.put(70d, "wangwu"); map.put(80d, "zhaoliu"); map.put(90d, "yangqi"); map.put(120d, "xiaoming"); map.put(130d, "xiaozhang"); map.put(140d, "xiaoli"); map.put(150d, "xiaoliu"); jedis.zadd("score", map); //更新分數 jedis.zadd("score", 100d, "zhangsan"); //獲取成員的分數值 Double dou = jedis.zscore("score", "zhangsan"); System.out.println("zhangsan score :\n" + dou); //按照成員分數小到大返回索引區間的成員 Set<String> set = jedis.zrange("score", 0, -1); System.out.println("score member order:"); for (String string : set) { System.out.println(string); } //按照成員分數大到小返回索引區間的成員 set = jedis.zrevrange("score", 0, -1); System.out.println("score member reverse:"); for (String string : set) { System.out.println(string); } //按照成員分數小到大返回分數值區間的成員 set = jedis.zrangeByScore("score", 50d, 80d); System.out.println("score member order:"); for (String string : set) { System.out.println(string); } //返回有序集合中指定成員的索引 Long index = jedis.zrank("score", "xiaoliu"); System.out.println("xiaoliu index is:\n" + index); } }
若是你讀過個人Redis入門教程(二)—基本數據類型,你會發現上面這些命令在其中都有說起,只是諸如參數傳遞的形式、返回值的類型等有少量不一樣,但方法名和文中的指令徹底相同,其餘方面也大同小異。所以在上面的代碼中並無像文章中同樣儘量多的列出全部的函數,還需你本身勤於練習、摸索。服務器
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * @author dotleo * */ public class RedisUtil { //Redis服務器ip地址 private static final String IP_ADDRESS = "192.168.1.233"; //Redis端口號 private static final int PORT = 6379; //可用鏈接實例的最大數目,默認值爲8; //若是賦值爲-1,則表示不限制;若是pool已經分配了maxActive個jedis實例,則此時pool的狀態爲exhausted(耗盡)。 private static final int MAX_ACTIVE = 1024; //控制一個pool最多有多少個狀態爲idle(空閒的)的jedis實例,默認值也是8。 private static final int MAX_IDLE = 200; //等待可用鏈接的最大時間,單位毫秒,默認值爲-1,表示永不超時。若是超過等待時間,則直接拋出JedisConnectionException; private static final int MAX_WAIT = 10000; //在borrow一個jedis實例時,是否提早進行validate操做;若是爲true,則獲得的jedis實例均是可用的; private static boolean TEST_ON_BORROW = true; //Jedis鏈接池對象 private static JedisPool jedisPool = null; /** * 初始化鏈接池 */ static { try { JedisPoolConfig conf = new JedisPoolConfig(); conf.setMaxActive(MAX_ACTIVE); conf.setMaxIdle(MAX_IDLE); conf.setMaxWait(MAX_WAIT); conf.setTestOnBorrow(TEST_ON_BORROW); jedisPool = new JedisPool(conf, IP_ADDRESS, PORT); } catch(Exception ex) { ex.printStackTrace(); } } /** * 獲取Jedis實例 * @return Jedis實例 */ public synchronized static Jedis getJedis() { try { if (jedisPool != null) { Jedis resource = jedisPool.getResource(); return resource; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } /** * 釋放jedis資源 * @param jedis Jedis對象 */ public static void returnResource(final Jedis jedis) { if (jedis != null) { jedisPool.returnResourceObject(jedis); } } }