Redis Java 基本操作

package yz20537.redis_test;

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

import redis.clients.jedis.Jedis;

public class App {

    static Jedis jedis = new Jedis("localhost");

    public static void main(String[] args) {
        // connect();
        // testKey();
        // testString();
        // testHash();
        // testList();
        // testSet();
        testSortedSet();

        jedis.close();
    }

    static void testKey() {
        // set key value --set key and value
        jedis.set("name", "wahaha");
        // del key -- delete key
        jedis.del("name");
        // exists key -- if exists
        jedis.set("name", "wahaha");
        System.out.println(jedis.exists("name"));
        // expire aaa 10 -- set the timeout 10s
        jedis.expire("name", 10);
        System.out.println(jedis.ttl("name"));
        // pexpire aaa 5000 -- set timeout 5000mm
        jedis.pexpire("name", 1000L);
        System.out.println(jedis.ttl("name"));
        // expireat aaa 1504771560 --set timeout at unixstamp
        jedis.expireAt("name", new Date().getTime() / 1000 + 10L);
        System.out.println(jedis.ttl("name"));
        // set always live
        jedis.persist("name");
        System.out.println(jedis.ttl("name"));
        // show all keys
        System.out.println(jedis.keys("*"));
    }

    static void testString() {
        jedis.flushAll();

        // set name zouyufei
        jedis.set("name", "wahaha");
        // get name
        System.out.println(jedis.get("name"));
        // mset name zouyufei age 20 (multi set)
        jedis.mset("name", "wahaha", "age", "20");
        // mget
        System.out.println(jedis.mget("name", "age"));
        // getrange name 1 3 (index from 0)
        System.out.println(jedis.getrange("name", 0, 2));
        // setrange name 7 aa (name = name from 0-7 + aa)
        jedis.setrange("name", 4, "!#");
        System.out.println(jedis.get("name"));
        // setex gender 10 male (set gender=male, and timeout=10s)
        jedis.setex("gender", 10, "male");
        System.out.println(jedis.get("gender") + "\t" + jedis.ttl("gender"));
        // setnx name zouyf (set name=zouyf if name doesn't exist)
        jedis.setnx("name", "abc");
        jedis.setnx("height", "180cm");
        System.out.println(jedis.mget("name", "height"));
        // msetnx name zouyufei age 20 (multi set when all keys don't exist)
        jedis.msetnx("food", "abc", "weight", "100kg");
        System.out.println(jedis.mget("food", "weight"));
        // strlen name (get name length)
        System.out.println(jedis.strlen("name"));
        // incr age (age ++)
        jedis.incr("age");
        System.out.println(jedis.get("age"));
        // incrby age 10 (age+=10)
        jedis.incrBy("age", 20);
        System.out.println(jedis.get("age"));
        // decr age (age--)
        jedis.decr("age");
        System.out.println(jedis.get("age"));
        // decrby age 10 (age -=10)
        jedis.decrBy("age", 20);
        System.out.println(jedis.get("age"));
        // append name dtc (name=name+dtc)
        jedis.append("food", "fruit");
        System.out.println(jedis.get("food"));
    }

    static void testHash() {
        jedis.flushAll();

        // hset mac cpu i7
        jedis.hset("hash", "cpu", "i7");
        // hmset mac cpu i5 memory 10G price 4999 (set multi)
        Map<String, String> keyvalues = new HashMap<String, String>();
        keyvalues.put("memory", "4GB");
        keyvalues.put("disk", "400GB");
        keyvalues.put("price", "400");
        jedis.hmset("hash", keyvalues);
        // hget mac cpu
        System.out.println(jedis.hget("hash", "cpu"));
        // hmget mac screen memory (get multi )
        System.out.println(jedis.hmget("hash", "cpu", "disk"));
        // hgetall mac
        System.out.println(jedis.hgetAll("hash"));
        // hdel mac address screen (delete address and screen from mac)
        jedis.hdel("hash", "cpu", "disk");
        // hexists mac name
        System.out.println(jedis.hexists("hash", "cpu"));
        // hincrby mac price 10 (mac's price +=10)
        jedis.hincrBy("hash", "price", 100);
        // hkeys mac (get all keys in mac)
        System.out.println(jedis.hkeys("hash"));
        // hlen mac (key count)
        System.out.println(jedis.hlen("hash"));
        // hsetnx mac cpu i8 (set value only key doesn't exist)
        jedis.hsetnx("hash", "cpu", "i8");
        // hvals mac (get all values)
        System.out.println(jedis.hvals("hash"));
    }

    static void testList() {
        jedis.flushAll();

        // lpushx list 6 (if list doesn't exist, do nothing)
        jedis.lpushx("list", "a");
        System.out.println(jedis.lrange("list", 0, -1));
        // lpush list 1 (if list doesn't exist, create the list)
        jedis.lpush("list", "a", "b", "c", "d", "e", "f");
        System.out.println(jedis.lrange("list", 0, -1));
        // lindex list 2 (get the second element)
        System.out.println(jedis.lindex("list", 1));
        // llen list (get length)
        System.out.println(jedis.llen("list"));
        // lpop list (get first element from left - head)
        System.out.println(jedis.lpop("list"));
        System.out.println(jedis.lrange("list", 0, -1));
        // lrem list 2 5 (remove 5 twice from head)
        jedis.lrem("list", 1, "a");
        System.out.println(jedis.lrange("list", 0, -1));
        // lset list 0 100 (set the first element from left to 100)
        jedis.lset("list", 0, "100");
        System.out.println(jedis.lrange("list", 0, -1));
        // ltrim list 0 4 (remain from [0,4] elements)
        jedis.ltrim("list", 0, 2);
        System.out.println(jedis.lrange("list", 0, -1));
        // rpoplpush list list (pop first element from right and push to list
        // from left)
        jedis.rpoplpush("list", "list1");
        System.out.println(jedis.lrange("list1", 0, -1));
        // rpush list 2 3 4 (push the elements from right)
        jedis.rpush("list", "1", "2", "3");
        System.out.println(jedis.lrange("list", 0, -1));
        // rpushx list 6 (if list doesn't exist, do nothing)
        // rpop list (get first element from right - tail)
        jedis.rpop("list");
        System.out.println(jedis.lrange("list", 0, -1));

    }

    static void testSet() {
        // sadd set 1 2 3 4
        jedis.sadd("set", "1", "1", "2", "3", "4", "7", "8");
        // smembers set (show all)
        System.out.println(jedis.smembers("set"));
        // srem set 4 (remove)
        jedis.srem("set", "3");
        System.out.println(jedis.smembers("set"));
        // scard set (show size)
        System.out.println(jedis.scard("set"));
        // sdiff set set1 (get the difference between set and set1)
        jedis.sadd("set1", "1", "5");
        System.out.println(String.format("set:%s, set1:%s, diff:%s", jedis.smembers("set"), jedis.smembers("set1"), jedis.sdiff("set", "set1")));
        System.out.println(String.format("set:%s, set1:%s, diff:%s", jedis.smembers("set"), jedis.smembers("set1"), jedis.sdiff("set1", "set")));
        // sdiffstore diffset set set1
        jedis.sdiffstore("setdiff", "set1", "set");
        System.out.println(jedis.smembers("setdiff"));
        // sinter set set1 (交集)
        System.out.println(jedis.sinter("set", "set1"));
        // sinterstore interset set set1
        jedis.sinterstore("interset", "set", "set1");
        System.out.println(jedis.smembers("interset"));
        // sunion set set1 (并集)
        System.out.println(jedis.sunion("set", "set1"));
        // sunionstore unionset set set1
        jedis.sunionstore("unionset", "set", "set1");
        System.out.println(jedis.smembers("unionset"));
        // sismember set 1 (if 1 belongs to set)
        System.out.println(jedis.sismember("set", "3"));
        // smove set set1 1 (move 1 from set to set1)
        jedis.smove("set", "mset", "1");
        System.out.println(jedis.smembers("set"));
        System.out.println(jedis.smembers("mset"));
        // spop set 2 (drop 2 elements randomly from set)
        jedis.spop("set", 2);
        System.out.println(jedis.smembers("set"));
        // srandmember set 2 (get 2 elements randomly from set)
        System.out.println(jedis.srandmember("set", 1));
    }

    static void testSortedSet() {
        jedis.flushAll();
        // zadd zet 1 a 2 b 3 c (add a,b,c with weight 1 2 3)
        jedis.zadd("zet", 90, "cpu");
        jedis.zadd("zet", 91, "memory");
        jedis.zadd("zet", 92, "disk");
        jedis.zadd("zet", 93, "screen");
        System.out.println(jedis.zrange("zet", 0, -1));
        // zrem zet a
        jedis.zrem("zet", "disk");
        System.out.println(jedis.zrange("zet", 0, -1));
        // zrank zet a (show the index of a order by score asc- from 0)
        System.out.println(jedis.zrank("zet", "disk"));
        // zrevrank zet2 a (show the index of a order by score desc)
        System.out.println(jedis.zrevrank("zet", "cpu"));
        // zscore zet1 a(show the score of a)
        System.out.println(jedis.zscore("zet", "cpu"));

        // zcard zet (size)
        System.out.println(jedis.zcard("zet"));
        // zcount zet 1 2 (get the count whose score between 1 and 2)
        System.out.println(jedis.zcount("zet", 90, 92));
        // zincrby zet 10 a (add 10 to the weight of a)
        jedis.zincrby("zet", 100, "cpu");
        System.out.println(jedis.zscore("zet", "cpu"));
        // zinterstore interzet 2 zet zet1 (交集)
        jedis.zadd("zet1", 93, "screen");
        jedis.zadd("zet1", 99, "mouse");
        jedis.zinterstore("interZet", "zet", "zet1");
        System.out.println(jedis.zrange("interZet", 0, -1));
        // zunionstore unionzet 2 zet zet1(并集)
        jedis.zunionstore("unionZet", "zet", "zet1");
        System.out.println(jedis.zrange("unionZet", 0, -1));
        // zremrangebyscore zet 0 4 (remove elements whose score between 0 and
        // 4)
        jedis.zremrangeByScore("zet", 0, 100);
        // zrevrange zet1 0 1(get the last 2 elements)
        System.out.println(jedis.zrevrange("zet", 0, 2));
        System.out.println(jedis.zrange("zet", 0, -1));

        // 當(dāng)有序集合的所有成員都具有相同的分值時贝咙, 有序集合的元素會根據(jù)成員的字典序(lexicographical ordering)來進(jìn)行排序平斩,
        // 而這個命令則可以返回給定的有序集合鍵 key 中慰毅, 值介于 min 和 max 之間的成員惠呼。
        // 如果有序集合里面的成員帶有不同的分值, 那么命令返回的結(jié)果是未指定的(unspecified)。
        // zrangebylex zet1 [a [c (show the elements whose score between a and
        // c's score)]
        System.out.println(jedis.zrangeByLex("zet", "[cpu", "[disk"));
        // zremrangebylex zet [a [b (remove elements whose score between a and
        // b's)
        System.out.println("before remove by lex:" + jedis.zrange("zet", 0, -1));
        jedis.zremrangeByLex("zet", "[cpu", "[disk");
        System.out.println("remove by lex:" + jedis.zrange("zet", 0, -1));
        // zlexcount zet1 [a [b (get the count which weight between a and b
        // weight )
        System.out.println(jedis.zlexcount("zet", "[cpu", "[disk"));

    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子杈曲,更是在濱河造成了極大的恐慌,老刑警劉巖胸懈,帶你破解...
    沈念sama閱讀 206,482評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件担扑,死亡現(xiàn)場離奇詭異,居然都是意外死亡趣钱,警方通過查閱死者的電腦和手機(jī)魁亦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,377評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來羔挡,“玉大人洁奈,你說我怎么就攤上這事〗首疲” “怎么了利术?”我有些...
    開封第一講書人閱讀 152,762評論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長低矮。 經(jīng)常有香客問我印叁,道長,這世上最難降的妖魔是什么军掂? 我笑而不...
    開封第一講書人閱讀 55,273評論 1 279
  • 正文 為了忘掉前任轮蜕,我火速辦了婚禮,結(jié)果婚禮上蝗锥,老公的妹妹穿的比我還像新娘跃洛。我一直安慰自己,他們只是感情好终议,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,289評論 5 373
  • 文/花漫 我一把揭開白布汇竭。 她就那樣靜靜地躺著葱蝗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪细燎。 梳的紋絲不亂的頭發(fā)上两曼,一...
    開封第一講書人閱讀 49,046評論 1 285
  • 那天,我揣著相機(jī)與錄音玻驻,去河邊找鬼悼凑。 笑死,一個胖子當(dāng)著我的面吹牛璧瞬,可吹牛的內(nèi)容都是我干的佛析。 我是一名探鬼主播,決...
    沈念sama閱讀 38,351評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼彪蓬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了捺萌?” 一聲冷哼從身側(cè)響起档冬,我...
    開封第一講書人閱讀 36,988評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎桃纯,沒想到半個月后酷誓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,476評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡态坦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,948評論 2 324
  • 正文 我和宋清朗相戀三年盐数,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伞梯。...
    茶點(diǎn)故事閱讀 38,064評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡玫氢,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谜诫,到底是詐尸還是另有隱情漾峡,我是刑警寧澤,帶...
    沈念sama閱讀 33,712評論 4 323
  • 正文 年R本政府宣布喻旷,位于F島的核電站生逸,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏且预。R本人自食惡果不足惜槽袄,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,261評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望锋谐。 院中可真熱鬧遍尺,春花似錦、人聲如沸涮拗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,264評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至歧蕉,卻和暖如春灾部,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背惯退。 一陣腳步聲響...
    開封第一講書人閱讀 31,486評論 1 262
  • 我被黑心中介騙來泰國打工赌髓, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人催跪。 一個月前我還...
    沈念sama閱讀 45,511評論 2 354
  • 正文 我出身青樓锁蠕,卻偏偏與公主長得像,于是被迫代替她去往敵國和親懊蒸。 傳聞我的和親對象是個殘疾皇子荣倾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,802評論 2 345

推薦閱讀更多精彩內(nèi)容