Redis 5大基本型別的用法
阿新 • • 發佈:2018-11-29
redis儲存格式
大家都知道redis支援的儲存型別(String/List/Hash/Set/SortedSet ),但是不一定在工作中都用到過,希望通過整理的這篇文章,讓大家都能知道在java中如何使用redis以及redis對這幾種資料型別的操作。
基本用法
通過Jedis(封裝了redis的Java客戶端)對redis進行操作。
Jedis工具類
public class JedisPoolUtil { private static JedisPool pool = null; static { //載入配置檔案 InputStream in = JedisPoolUtil.class.getClassLoader().getResourceAsStream("redis.properties"); Properties pro = new Properties(); try { pro.load(in); } catch (IOException e) { e.printStackTrace(); System.out.println("載入檔案失敗"); } JedisPoolConfig poolConfig = new JedisPoolConfig(); //最大連線數 poolConfig.setMaxTotal(Integer.parseInt( pro.get("redis.maxTotal").toString())); //最大空閒連線數 poolConfig.setMaxIdle(Integer.parseInt( pro.get("redis.maxIdle").toString())); //最小空閒連線數 poolConfig.setMinIdle(Integer.parseInt( pro.get("redis.minIdle").toString())); pool = new JedisPool(poolConfig, pro.get("redis.url").toString(),Integer.parseInt( pro.get("redis.port") .toString())); } public static Jedis getJedis(){ return pool.getResource(); } public static void release(Jedis jedis){ if(null != jedis){ jedis.close(); } } }
redis配置檔案
redis.maxTotal=100
redis.maxIdle=30
redis.minIdle=10
redis.url=192.168.202.200
redis.port=6379
String測試
public class StringTest { public Jedis jedis = JedisPoolUtil.getJedis(); @Test //新增和獲取 public void fun(){ jedis.set("num","1"); System.out.println(jedis.get("num")); } @Test //刪除值 public void fun1(){ jedis.del("num"); System.out.println(jedis.get("num")); } @Test //自減和自減 public void fun2(){ jedis.set("num","1"); System.out.println(jedis.get("num")); jedis.decr("num"); System.out.println(jedis.get("num")); jedis.incr("num"); jedis.incr("num"); System.out.println(jedis.get("num")); } @Test //加上/減去 一個數 //incrBy 返回的是修改之後的值如果原值是字串不是數字,則會丟擲異常 public void fun3(){ Long num = jedis.incrBy("num", 3); System.out.println(num); jedis.decrBy("num",10); System.out.println(jedis.get("num")); jedis.set("name","caopengfei"); //jedis.decrBy("name",1); } @Test //字串拼接 public void fun4(){ Long len = jedis.append("name", "123"); System.out.println(len); System.out.println(jedis.get("name")); } }
Hash測試
public class HashTest { public Jedis jedis = JedisPoolUtil.getJedis(); // hash 操作的是map物件 // 適合儲存鍵值物件的資訊 @Test //存值 引數第一個變數的名稱, map鍵名(key), map鍵值(value) // 呼叫hset public void fun() { Long num = jedis.hset("hash1", "username", "caopengfei"); System.out.println(num); String hget = jedis.hget("hash1", "username"); System.out.println(hget); } @Test //也可以存多個key // 呼叫hmset public void fun1() { Map<String, String> map = new HashMap<String, String>(); map.put("username", "caopengfei"); map.put("age", "25"); map.put("sex", "男"); String res = jedis.hmset("hash2", map); System.out.println(res);//ok } @Test //獲取hash中所有的值 public void fun2() { Map<String, String> map2 = new HashMap<String, String>(); map2 = jedis.hgetAll("hash2"); System.out.println(map2); } @Test // 刪除hash中的鍵 可以刪除一個也可以刪除多個,返回的是刪除的個數 public void fun3() { Long num = jedis.hdel("hash2", "username", "age"); System.out.println(num); Map<String, String> map2 = new HashMap<String, String>(); map2 = jedis.hgetAll("hash2"); System.out.println(map2); } @Test //增加hash中的鍵值對 public void fun4() { Map<String, String> map2 = new HashMap<String, String>(); map2 = jedis.hgetAll("hash2"); System.out.println(map2); jedis.hincrBy("hash2", "age", 10); map2 = jedis.hgetAll("hash2"); System.out.println(map2); } @Test //判斷hash是否存在某個值 public void fun5() { System.out.println(jedis.hexists("hash2", "username")); System.out.println(jedis.hexists("hash2", "age")); } @Test //獲取hash中鍵值對的個數 public void fun6() { System.out.println(jedis.hlen("hash2")); } // 獲取一個hash中所有的key值 @Test public void fun7() { Set<String> hash2 = jedis.hkeys("hash2"); System.out.println(hash2); } // 獲取所有的value值 @Test public void fun8() { List<String> hash2 = jedis.hvals("hash2"); System.out.println(hash2); } }
List測試
public void testList()
{
jedis.flushDB();
System.out.println("===========新增一個list===========");
jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
jedis.lpush("collections", "HashSet");
jedis.lpush("collections", "TreeSet");
jedis.lpush("collections", "TreeMap");
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));//-1代表倒數第一個元素,-2代表倒數第二個元素
System.out.println("collections區間0-3的元素:"+jedis.lrange("collections",0,3));
System.out.println("===============================");
// 刪除列表指定的值 ,第二個引數為刪除的個數(有重複時),後add進去的值先被刪,類似於出棧
System.out.println("刪除指定元素個數:"+jedis.lrem("collections", 2, "HashMap"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("刪除下表0-3區間之外的元素:"+jedis.ltrim("collections", 0, 3));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出棧(左端):"+jedis.lpop("collections"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections新增元素,從列表右端,與lpush相對應:"+jedis.rpush("collections", "EnumMap"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出棧(右端):"+jedis.rpop("collections"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("修改collections指定下標1的內容:"+jedis.lset("collections", 1, "LinkedArrayList"));
System.out.println("collections的內容:"+jedis.lrange("collections", 0, -1));
System.out.println("===============================");
System.out.println("collections的長度:"+jedis.llen("collections"));
System.out.println("獲取collections下標為2的元素:"+jedis.lindex("collections", 2));
System.out.println("===============================");
jedis.lpush("sortedList", "3","6","2","0","7","4");
System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1));
System.out.println(jedis.sort("sortedList"));
System.out.println("sortedList排序後:"+jedis.lrange("sortedList", 0, -1));
}
Set測試
/*
* Set集合,和List類的區別就是
* set中不會出現重複的資料
* 他可以進行聚合操作效率比較高
* 其餘的操作基本上和list相同
*
* */
public class SetTest {
public Jedis jedis = JedisPoolUtil.getJedis();
@Test
/*新增元素刪除元素*/
public void fun(){
Long num = jedis.sadd("myset", "a", "a", "b","abc");
System.out.println(num);
}
@Test
/*獲得元素*/
public void fun1(){
Set<String> myset = jedis.smembers("myset");
System.out.println(myset);
}
@Test
/*移除元素*/
public void fun2(){
jedis.srem("myset","a","b");
Set<String> myset = jedis.smembers("myset");
System.out.println(myset);
}
@Test
//判斷是否這個set中存在某個值
public void fun3(){
Boolean sismember = jedis.sismember("myset", "a");
System.out.println(sismember);
}
@Test
//獲得A-B 獲得差集合
public void fun4(){
jedis.sadd("myset1","123","32","abc","def","123456","sdfasd");
jedis.sadd("myset2","abc","345","123","fda");
Set<String> sdiff = jedis.sdiff("myset1", "myset2");
System.out.println(sdiff);
}
@Test
//獲得交集
public void fun5(){
Set<String> sinter = jedis.sinter("myset1", "myset2");
System.out.println(sinter);
}
@Test
// 獲得並集合
public void fun6(){
Set<String> sunion = jedis.sunion("myset1", "myset2");
System.out.println(sunion);
}
@Test
// 成員數量
public void fun7(){
System.out.println(jedis.scard("myset1"));
}
@Test
// 獲得隨機的一個成員
public void fun8(){
System.out.println(jedis.srandmember("myset1"));
}
@Test
// 將相差的成員放到一個新的set中同理交集和並集都可以後面均
// 加上一個store即可
// 並返回新的長度
public void fun9(){
System.out.println(jedis.sdiffstore("myset3","myset1","myset2"));
System.out.println(jedis.smembers("myset3"));
}
}
SortedSet 測試
/*
和set極為的類似,他們是字串的集合,沒有重複的資料
差別是sortedset每個成員中都會有一個分數(score)與之關聯
,redis正是通過分數來為集合中的成員進行從小到大的排序
sortedset中資料必須單一但是他的score可以是重複的
*/
public class SortedsetTest {
public Jedis jedis = JedisPoolUtil.getJedis();
// 新增元素
@Test
public void fun(){
jedis.zadd("mysort",100.0, "zhangsan");
jedis.zadd("mysort",200.0,"lisi");
jedis.zadd("mysort",50.0,"wangwu");
Map<String ,Double>map = new HashMap<String ,Double>();
map.put("mutouliu",70.0);
jedis.zadd("mysort",map);
Set<String> mysort = jedis.zrange("mysort", 0, -1);
System.out.println(mysort);
Set<String> mysort1 = jedis.zrange("mysort", 1, 2);
System.out.println(mysort1);
}
}