java中Redis5大基本類型的用法

存儲格式

 

 

基本用法

經過Jedis(封裝了redis的Java客戶端)對redis進行操做。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);
    }
} 
相關文章
相關標籤/搜索