Redis Java 基本操作

2017-09-29  本文已影响0人  一颗北上广的心
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));

        // 当有序集合的所有成员都具有相同的分值时, 有序集合的元素会根据成员的字典序(lexicographical ordering)来进行排序,
        // 而这个命令则可以返回给定的有序集合键 key 中, 值介于 min 和 max 之间的成员。
        // 如果有序集合里面的成员带有不同的分值, 那么命令返回的结果是未指定的(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"));

    }
}

上一篇下一篇

猜你喜欢

热点阅读