jedis操作

package jedis;

import redis.clients.jedis.*;

import java.util.HashMap;
import java.util.Map;

public class JedisDemo {
    private static final JedisPool JEDIS_POOL;

    static {
        JedisPoolConfig config = new JedisPoolConfig();//redis連接池配置對象
        config.setMaxTotal(32);//最大連接數
        config.setMaxIdle(6);//閒置最大連接數
        config.setMinIdle(0);//閒置最小連接數
        config.setMaxWaitMillis(15000);//到達最大連接數後,調用者阻塞時間
        config.setMinEvictableIdleTimeMillis(300000);//連接空閒的最小時間,可能被移除
        config.setSoftMinEvictableIdleTimeMillis(-1);//連接空閒的最小時間,多餘最小閒置連接的將被移除
        config.setNumTestsPerEvictionRun(3);//設置每次檢查閒置的個數
        config.setTestOnBorrow(false);//申請連接時,是否檢查連接有效
        config.setTestOnReturn(false);//返回連接時,是否檢查連接有效
        config.setTestWhileIdle(false);//空閒超時,是否執行檢查有效
        config.setTimeBetweenEvictionRunsMillis(60000);//空閒檢查時間
        config.setBlockWhenExhausted(true);//當連接數耗盡,是否阻塞

        //連接池配置對象+ip+port+timeout+password+dbname
        JEDIS_POOL = new JedisPool(config,"127.0.0.1",6379,15000,null,1);
    }

    /**
     * redis中String類型操作:字符串+數字+bit
     */
    public static void stringCmdTest(Jedis jedis){
        System.out.println("----------------redis-String-----------------");
        //set:返回操作結果
        System.out.println("name=>wsy:"+jedis.set("name","wsy"));

        //get:value
        System.out.println("name:"+jedis.get("name"));

        //append:字符串長度
        System.out.println("append:"+jedis.append("name","_ss"));

        //strlen:字符串長度
        System.out.println("strlen:"+jedis.strlen("name"));

        //getrange:返回不包括起始座標的值
        System.out.println("getrange:"+jedis.getrange("name", 10, 13));

        //setrange:從起始座標考試替換,未替換的保持
        System.out.println("setrange:"+jedis.setrange("name", 10, "#"));

        //mset:批量設置,返回批量設置結果
        System.out.println("mset:"+jedis.mset("name","wsy","age", "29"));

        //mget:返回數組
        System.out.println("mget:"+jedis.mget("name","age"));

        //incr:value自增1後,返回value
        System.out.println("incr:"+jedis.incr("age"));

        //incr:value自增傳參值後,返回value
        System.out.println("incrBy:"+jedis.incrBy("age",3));

        //decr:value自減1,返回value
        System.out.println("decr:"+jedis.decr("age"));

        //decrBy:value自減入參值,返回value
        System.out.println("decrBy:"+jedis.decrBy("age",3));

        //setex:設置key值+有效時間,如果key存在則覆蓋value
        System.out.println("setex:"+jedis.setex("phone",10,"13600000001"));

        //setnx:當key不存在時,設置才成功
        System.out.println("setnx:"+jedis.setnx("address","china"));

        //del:刪除對應key
        System.out.println("del:"+jedis.del("address1"));

        System.out.println("----------------redis-String-----------------\n");

    }

    /**
     * redis中hash類型常用操作
     * @param jedis
     */
    public static void hashMapCmdTest(Jedis jedis){
        System.out.println("----------------redis-HashMap-----------------");
        //hset:返回值爲key爲新返回1,爲舊覆蓋舊值返回0
        System.out.println("hset:"+jedis.hset("user","name","wangshaoyi"));

        Map map = new HashMap();
        map.put("name","wsy");
        map.put("age","29");
        //hmset:map對象
        System.out.println("hmset:" + jedis.hmset("user", map));

        //hexists:判斷hashmap中key是否存在
        System.out.println("hexists:"+jedis.hexists("user","age"));

        //hget:獲取map中key對應的value
        System.out.println("hget:"+jedis.hget("user","name"));

        //hgetAll:獲取map中所有對象
        System.out.println("hgetAll:"+jedis.hgetAll("user"));

        //hkeys:獲取map中所有key
        System.out.println("hkeys:"+jedis.hkeys("user"));

        //hvals:獲取map中所有value
        System.out.println("hvals:"+jedis.hvals("user"));


        //hmget:批量獲取keys的對象,返回List
        System.out.println("hmget:"+jedis.hmget("user","age","name"));

        //hlen:map的大小
        System.out.println("hlen:"+jedis.hlen("user"));

        //hdel:刪除map中對應key,正確刪除返回1
        System.out.println("hdel:"+jedis.hdel("user","age0"));

        System.out.println("----------------redis-HashMap-----------------\n");

    }

    public static void listCmdTest(Jedis jedis){
        System.out.println("----------------redis-List-----------------");
        //
        jedis.del("contacts");
        jedis.del("contacts_old");


        //lpush:批量頭部插入,返回List的size
        System.out.println("lpush:"+jedis.lpush("contacts","xx","yy","zz"));

        //lpushx:單個頭部插入,返回List的size
        System.out.println("lpushx:"+jedis.lpushx("contacts","aa"));

        //linsert:指定對象位置(前or後)插入
        System.out.println("linsert:"+jedis.linsert("contacts", BinaryClient.LIST_POSITION.BEFORE,"zz","bb"));

        //lset:將指定的位置設置值(替換舊值)
        System.out.println("lset:"+jedis.lset("contacts",2,"cc"));

        //lpop:鏈表頭的對象
        System.out.println("lpop:"+jedis.lpop("contacts"));

        //lrange:獲取list指定start、end位置value
        System.out.println("lrange:"+jedis.lrange("contacts",1,3));

        //ltrim:只剩start\end中list值,其餘刪除
        System.out.println("ltrim:"+jedis.ltrim("contacts",1,3));

        //lrem:刪除list指定值(次數指定),返回刪除個數
        System.out.println("lrem:"+jedis.lrem("contacts",2,"yy"));

        //rpoplpush:將源list尾部對象移到目標list對象頭部
        System.out.println("rpoplpush:"+jedis.rpoplpush("contacts","contacts_old"));

        //rpush:在list尾部對象添加值
        System.out.println("rpush:"+jedis.rpush("contacts","aa","bb"));

        //rpop:移除在list尾部值,返回移除的對象
        System.out.println("rpop:"+jedis.rpop("contacts"));

        //brpop:阻塞尾部對象拋出,指定超時時間,返回拋出值
        System.out.println("brpop:"+jedis.brpop(1,"contacts"));

        System.out.println("blpop:"+jedis.blpop(1, "contacts"));

        System.out.println("blpop(阻塞1秒返回):"+jedis.blpop(1, "contacts"));

        System.out.println("----------------redis-List-----------------\n");


    }

    public static void setCmdTest(Jedis jedis){
        System.out.println("----------------redis-Set-----------------");
        jedis.del("phones");
        jedis.del("phones_old");
        jedis.del("phones_old_1");
        jedis.del("phones_new");


        //sadd:集合添加元素,返回添加成功後數據
        System.out.println("sadd:"+jedis.sadd("phones","13600000001","13300000001"));
        System.out.println("sadd:"+jedis.sadd("phones","13600000002","13300000002"));

        //scard:返回集合中元素數
        System.out.println("scard:"+jedis.scard("phones"));

        jedis.sadd("phones_old","13600000002");
        jedis.sadd("phones_old_1","13300000001");

        //sdiff:首set與其他set之間的差集,返回差集值
        System.out.println("sdiff:"+jedis.sdiff("phones","phones_old","phones_old_1"));

        //sdiffstore:首set與其他set之間的差集保存至新set,返回差集數
        System.out.println("sdiffstore:"+jedis.sdiffstore("phones_new","phones","phones_old"));

        //sinter:返回集合的交集
        System.out.println("sinter:"+jedis.sinter("phones","phones_new"));

        //sismember:判斷value是否爲set的值
        System.out.println("sismember:"+jedis.sismember("phones","13600000001"));

        //smembers:返回集合中成員
        System.out.println("smembers:"+jedis.smembers("phones"));

        //smove:將首源set中元素移動目標set,返回移動數
        System.out.println("smove:"+jedis.smove("phones","phones_new","13600000002"));

        //spop:隨機移除set的一元素,返回移除元素
        System.out.println("spop:"+jedis.spop("phones"));

        //srandmember:隨機取出集合中一個元素
        System.out.println("srandmember:"+jedis.srandmember("phones_new"));

        //srem:刪除集合中指定元素
        System.out.println("srem:"+jedis.srem("phones_new","13600000002"));

        //sunion:集合中並集
        System.out.println("sunion:"+jedis.sunion("phones","phones_new","phones_old"));

        System.out.println("----------------redis-Set-----------------\n");

    }

    public static void sortedSetTest(Jedis jedis){

        System.out.println("----------------redis-SortedSet-----------------");
        jedis.del("scores");
        jedis.del("scores_1");
        jedis.del("scores_total");
        jedis.del("score_inter");
        jedis.del("score_max");



        //zadd:sortedSet添加元素
        System.out.println("zadd:"+jedis.zadd("scores", 610.5, "xx"));
        jedis.zadd("scores", 630, "yy");

        //zcard:返回sortedset中元素數
        System.out.println("zcard:"+jedis.zcard("scores"));

        //zcount:返回指定分值(包括)的元素數
        System.out.println("zcount:"+jedis.zcount("scores",610,620));

        //zincrby:將指定值分數加分,返回加後的分數
        System.out.println("zincrby:"+jedis.zincrby("scores",10,"xx"));

        //zrange:返回指定座標的值
        System.out.println("zrange:"+jedis.zrange("scores",0,1));

        //zrangeByScore:返回指定分數範圍內的對象
        System.out.println("zrangeByScore:"+jedis.zrangeByScore("scores",600,700));

        //zrank:返回指定值的位置(分數低->高,0開始)
        System.out.println("zrank:"+jedis.zrank("scores","yy"));

        //zrevrank:返回指定值的位置(分數高->低,0開始)
        System.out.println("zrevrank:"+jedis.zrevrank("scores", "yy"));


        //zrem:刪除,其中還有zremrangeByRank\zremrangeByScore
        System.out.println("zrem:"+jedis.zrem("scores", "yy"));

        jedis.zadd("scores", 630, "yy");
        jedis.zadd("scores", 640, "zz");
        //zrevrange:獲取指定位置數據(分數從高->低)
        System.out.println(":"+jedis.zrevrange("scores",0,1));


        System.out.println("zrangeByScoreWithScores:"+jedis.zrangeByScoreWithScores("scores",600,700));

        //zscore:獲取指定分數
        System.out.println("zscore:"+jedis.zscore("scores", "xx"));
        jedis.zadd("scores_1", 630.5, "xx");
        jedis.zadd("scores_1",610.5,"bb");
        jedis.zadd("scores_1",622.5,"cc");

        //zunionstore:sortedset集合的並集並保存,如果集合中元素相同,則分數相加
        System.out.println("zunionstore:"+jedis.zunionstore("score_total","scores","scores_1"));


        ZParams zParams = new ZParams();
        zParams.aggregate(ZParams.Aggregate.MAX);//指定分數操作:+,最小,最大
        zParams.weightsByDouble(1,0.1);//分數中的乘法因子
        System.out.println("zunionstore:"+jedis.zunionstore("score_max",zParams,"scores","scores_1"));

        //zinterstore:集合元素取交集,相同元素值相加(默認)
        System.out.println("zinterstore:"+jedis.zinterstore("score_inter","scores","scores_1"));


        System.out.println("----------------redis-SortedSet-----------------\n");

    }
    public static void main(String[] args) {
        Jedis jedis = JEDIS_POOL.getResource();
        stringCmdTest(jedis);
        hashMapCmdTest(jedis);
        listCmdTest(jedis);
        setCmdTest(jedis);
        sortedSetTest(jedis);

        System.out.println("命令參考地址=>http://redisdoc.com/");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章