這篇文章主要介紹了redis常用api操作有哪些,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站制作、成都網(wǎng)站設計與策劃設計,遠安網(wǎng)站建設哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設十載,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:遠安等地區(qū)。遠安做網(wǎng)站價格咨詢:18980820575
//設置鍵值對 //set key value [EX seconds] [PX milliseconds] [NX|XX] //獲取值 //get key //刪除鍵 //del key //清空數(shù)據(jù)庫 //flushdb //獲取list值 //lrange key start stop //map類型 //hget key field //hgetall key //sortedset類型 package hgs.redislearn; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.junit.After; import org.junit.Before; import org.junit.Test; import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.jedis.Jedis; /** * *Description:Redis
* @author guangshihao * @date 2018年9月28日 * */ public class RedisMainTest { Jedis jedis = null; @Before public void getConnect() { jedis = new Jedis("192.168.6.129", 6379); } @Test public void sysoutConnect() { System.out.println(jedis.ping()); } @After public void finaly() { jedis.close(); } //測試放入String類型的鍵值對,并進行獲取刪除修改等操作 @Test public void TestStringKeys() { jedis.set("hgs.name", "haoguangshi."); String name = jedis.get("hgs.name"); System.out.println(name); long affect = jedis.del("hgs.name"); System.out.println(affect); } @Test public void TestListKeys() { //從左邊插入 jedis.lpush("test_list", "1","2","100"); //從右邊插入 jedis.rpush("test_list", "100","2","1","this"); //獲取list Listlis = jedis.lrange("test_list", 0, -1); System.out.println(lis); //jedis.lpop lpush rpop rpush //在this出現(xiàn)的第一個位置的前面插入is jedis.linsert("test_list", LIST_POSITION.BEFORE, "this", "is"); } //map類型操作 @Test public void TestMapKeys() throws JsonGenerationException, JsonMappingException, IOException { /* Person p = new Person (); p.setName("hgs"); p.setAge(26); p.setWeight(65); Person p1 = new Person (); p1.setName("wd"); p1.setAge(23); p1.setWeight(60); ObjectMapper mapper = new ObjectMapper(); String sp = mapper.writer().writeValueAsString(p); String sp1 = mapper.writer().writeValueAsString(p1); Map ps = new HashMap (); ps.put("hgs", sp); ps.put("wd", sp1);*/ jedis.hset("pseron:hgs", "name", "hgs"); jedis.hset("pseron:hgs", "age", "24"); jedis.hset("pseron:hgs", "weigth", "65kg"); jedis.hset("pseron:wd", "name", "wd"); jedis.hset("pseron:wd", "age", "24"); jedis.hset("pseron:wd", "weight", "60kg"); } @Test public void loopMapKeys() { //遍歷一個map Map wd = jedis.hgetAll("pseron:wd"); for(String type : wd.keySet()) { System.out.println(type+" : " +wd.get(type)); } //原子操作自增2 jedis.hincrBy("pseron:hgs\"", "age", 2L); } @Test public void delMapKeys() { //刪除屬性 jedis.hdel("pseron:wd", "weight"); //打印集合的長度 System.out.println(jedis.hlen("pseron:hgs")); } //set類型數(shù)據(jù)結構,添加數(shù)據(jù) @Test public void setKeysTest() { String[] tmp = new String[] { "tianmao","dingding","alimama","zhifubao","feizhu" }; jedis.sadd("apps", tmp); String[] tmp1 = new String[] { "tianmao","dingdi","alima","zhifubao","feizhu" }; jedis.sadd("apps", tmp); jedis.sadd("apps1", tmp1); } //遍歷 @Test public void scanSetKeys() { //判斷某個記錄是否存在 System.out.println(jedis.sismember("apps", "tianmao")); System.out.println(jedis.scard("apps")); //計算交集 Set sets = jedis.sinter("apps","apps1"); for(String s : sets) { System.out.println(s); } //并集 Set sets1 = jedis.sunion("apps","apps1"); for(String s : sets1) { System.out.println(s); } System.out.println(); //差集 Set sets2 = jedis.sdiff("apps","apps1"); for(String s : sets2) { System.out.println(s); } //遍歷 Set sets3 = jedis.smembers("apps"); for(String s : sets3) { System.out.println(s); } } //SortedSet數(shù)據(jù)類型 //存數(shù)據(jù) @Test public void sortedSetAddTest() { Map scoremem = new HashMap (); scoremem.put("wd", (double) 88); scoremem.put("cm", (double) 87); scoremem.put("zz", (double) 90); scoremem.put("wzf", (double) 70); scoremem.put("xzh", (double) 66); scoremem.put("hgs", (double) 55); scoremem.put("hjh", (double) 88); scoremem.put("shk", (double) 100); jedis.zadd("roommeets_score", scoremem); } //sortedSet 遍歷 @Test public void scanSortedSet() { //正序遍歷 Set members = jedis.zrange("roommeets_score", 0, -1); for(String mem : members) { System.out.println("name:"+mem +","+" score:" +jedis.zscore("roommeets_score", mem)+","+" rank:"+(jedis.zrank("roommeets_score", mem)+1)); } System.out.println(); //正序遍歷 Set members1 = jedis.zrevrange("roommeets_score", 0, -1); for(String mem : members1) { System.out.println("name:"+mem +","+" score:" +jedis.zscore("roommeets_score", mem)+","+" rank:"+(jedis.zrevrank("roommeets_score", mem)+1L)); } } } class Person{ String name ; int age; double weight; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", weight=" + weight + "]"; } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“redis常用api操作有哪些”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!