• Redis和Java的爱恨情仇之常见结构命令


    string

    mget和mset

    1. public class RedisDemoString {
    2. public static void main(String[] args) {
    3. JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
    4. try (Jedis jedis = jedisPool.getResource()) {
    5. test1(jedis);
    6. }
    7. }
    8. public static void test1(Jedis jedis) {
    9. jedis.flushAll();
    10. jedis.mset("key1", "value1", "key2", "value2");
    11. List values = jedis.mget("key1", "key2","key111");
    12. System.out.println(values);
    13. }
    14. }

    此处多个key得到的value的顺序都是和key匹配的,如果某个key在redis上不存在,对应的value使用null表示

    getrange和setrange

    1. private static void test2(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.set("key", "abcdefghijkl");
    4. String str1 = jedis.getrange("key",2,5);
    5. System.out.println(str1);
    6. jedis.setrange("key",2,"xyz");
    7. String str2 = jedis.get("key");
    8. System.out.println(str2);
    9. }

    append

    1. private static void test3(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.set("key", "bar");
    4. jedis.append("key", "baz");
    5. System.out.println(jedis.get("key"));
    6. }

    incr和decr

    1. private static void test4(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.set("foo", "bar");
    4. System.out.println(jedis.incr("key"));
    5. System.out.println(jedis.get("key"));
    6. }

    1. private static void test4(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.set("foo", "bar");
    4. System.out.println(jedis.incr("key"));
    5. System.out.println(jedis.get("key"));
    6. jedis.decr("key");
    7. System.out.println(jedis.get("key"));
    8. }

    List

    lpush和lrange

    1. private static void test1(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.lpush("key","111","222","333","444","555","666");
    4. System.out.println(jedis.lrange("key", 0, -1));
    5. }

    rpush,rpop,lpop

    1. private static void test2(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.rpush("key","111","222","333","444","555","666");
    4. System.out.println(jedis.lrange("key", 0, -1));
    5. }

    1. private static void test3(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.rpush("key","111","222","333","444","555","666");
    4. jedis.rpop("key");
    5. System.out.println(jedis.lrange("key", 0, -1));
    6. System.out.println(jedis.lpop("key"));
    7. }

    blpop,brpop

    1. private static void test4(Jedis jedis) {
    2. jedis.flushAll();
    3. List str1 = jedis.blpop(5,"key");
    4. System.out.println(str1);
    5. //返回的结果是一个二元组,一个是从哪个key对应的list中删除的,一个是删除的元素是什么
    6. jedis.lpush("key","111","222","333","444","555","666");
    7. }

    llen

    1. private static void test5(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.rpush("key","111","222","333","444","555","666");
    4. System.out.println(jedis.llen("key"));
    5. }

    set

    sadd,smembers

    1. public static void test1(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.sadd("key","111","222","333","444","555","666");
    4. System.out.println(jedis.smembers("key"));
    5. }

    sismember

    1. public static void test2(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.sadd("key","111","222","333","444","555","666");
    4. System.out.println(jedis.sismember("key", "111"));
    5. }

    scard

    1. public static void test3(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.sadd("key","111","222","333","444","555","666");
    4. System.out.println(jedis.scard("key"));
    5. }

    spop

    1. public static void test4(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.sadd("key","111","222","333","444","555","666");
    4. System.out.println(jedis.spop("key"));
    5. System.out.println(jedis.spop("key"));
    6. System.out.println(jedis.spop("key"));
    7. System.out.println(jedis.spop("key"));
    8. System.out.println(jedis.spop("key"));
    9. System.out.println(jedis.spop("key"));
    10. }

    sinter,sinterstore

    1. public static void test5(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.sadd("key","111","222","333","444","555","666");
    4. jedis.sadd("key1","111","222","444");
    5. System.out.println(jedis.sinter("key", "key1"));
    6. }

    1. static void test6(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.sadd("key","111","222","333","444","555","666");
    4. jedis.sadd("key1","111","222","444");
    5. System.out.println(jedis.sinterstore("key3", "key", "key1"));
    6. System.out.println(jedis.smembers("key3"));
    7. }

    Hash

    hset,hget

    1. public static void test1(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.hset("key", "f1", "111");
    4. Map fields = new HashMap<>();
    5. fields.put("f1", "111");
    6. fields.put("f2", "222");
    7. jedis.hmset("key", fields);
    8. System.out.println(jedis.hgetAll("key"));
    9. System.out.println(jedis.hdel("key", "f2"));
    10. System.out.println(jedis.hget("key","f100"));
    11. }

    hexists

    1. public static void test2(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.hset("key", "f1", "111");
    4. jedis.hset("key", "f2", "22");
    5. jedis.hset("key", "f3", "333");
    6. System.out.println(jedis.exists("key"));
    7. System.out.println(jedis.exists("key","f100"));
    8. }

    hdel

    1. public static void test3(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.hset("key", "f1", "111");
    4. jedis.hset("key", "f2", "22");
    5. jedis.hset("key", "f3", "333");
    6. System.out.println(jedis.hdel("key", "f1"));
    7. System.out.println(jedis.hdel("key", "f2"));
    8. System.out.println(jedis.hgetAll("key"));
    9. }

    hkeys,hvals

    1. public static void test4(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.hset("key", "f1", "111");
    4. jedis.hset("key", "f2", "22");
    5. jedis.hset("key", "f3", "333");
    6. System.out.println(jedis.hkeys("key"));
    7. System.out.println(jedis.hvals("key"));
    8. }

    hmget,hmset

    1. public static void test4(Jedis jedis) {
    2. jedis.flushAll();
    3. Map map = new HashMap<>();
    4. map.put("f1", "111");
    5. map.put("f2", "1222");
    6. map.put("f3", "113");
    7. jedis.hmset("key", map);
    8. List keys = jedis.hmget("key","f1","f2","f3");
    9. System.out.println(keys);
    10. }

    zset

    zadd,zrange

    1. public static void test1(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.zadd("key",11,"zahngsan");
    4. Map zset = new HashMap<>();
    5. zset.put("ke22y", 20.0);
    6. zset.put("k232342ey", 204.0);
    7. zset.put("ke42432y", 220.0);
    8. jedis.zadd("key",zset);
    9. System.out.println(jedis.zrange("key", 0, -1));
    10. System.out.println(jedis.zrangeWithScores("key", 0, -1));
    11. }

    1. public static void test1(Jedis jedis) {
    2. jedis.flushAll();
    3. jedis.zadd("key",11,"zahngsan");
    4. Map zset = new HashMap<>();
    5. zset.put("ke22y", 20.0);
    6. zset.put("k232342ey", 204.0);
    7. zset.put("ke42432y", 220.0);
    8. jedis.zadd("key",zset);
    9. List membersWithScore = jedis.zrangeWithScores("key", 0, -1);
    10. System.out.println(jedis.zrange("key", 0, -1));
    11. System.out.println(jedis.zrangeWithScores("key", 0, -1));
    12. String str1 = membersWithScore.get(0).getElement();
    13. Double db2 = membersWithScore.get(0).getScore();
    14. }

    zcard

    1. public static void test2(Jedis jedis) {
    2. System.out.println("zcard");
    3. jedis.flushAll();
    4. jedis.zadd("key", 10, "zhangsan");
    5. jedis.zadd("key", 20, "lisi");
    6. jedis.zadd("key", 30, "wangwu");
    7. long len = jedis.zcard("key");
    8. System.out.println("len: " + len);
    9. }

    zrem

    1. public static void test3(Jedis jedis) {
    2. System.out.println("zrem");
    3. jedis.flushAll();
    4. jedis.zadd("key", 10, "zhangsan");
    5. jedis.zadd("key", 20, "lisi");
    6. jedis.zadd("key", 30, "wangwu");
    7. long n = jedis.zrem("key", "zhangsan", "lisi");
    8. System.out.println("n: " + n);
    9. List result = jedis.zrangeWithScores("key", 0, -1);
    10. System.out.println("result: " + result);
    11. }

    zscore

    1. public static void test4(Jedis jedis) {
    2. System.out.println("zscore");
    3. jedis.flushAll();
    4. jedis.zadd("key", 10, "zhangsan");
    5. jedis.zadd("key", 20, "lisi");
    6. jedis.zadd("key", 30, "wangwu");
    7. Double score = jedis.zscore("key", "zhangsan2");
    8. System.out.println("score: " + score);
    9. }

    zrank

    1. public static void test5(Jedis jedis) {
    2. System.out.println("zrank");
    3. jedis.flushAll();
    4. jedis.zadd("key", 10, "zhangsan");
    5. jedis.zadd("key", 20, "lisi");
    6. jedis.zadd("key", 30, "wangwu");
    7. Long rank = jedis.zrank("key", "zhangsan2");
    8. System.out.println("rank: " + rank);
    9. }

    总结:

    Jedis这个库的本质就是针对Redis的各种命令进行了封装,调用某个方法,就相当于在Redis客户端敲下了对应的命令

  • 相关阅读:
    nodejs+vue+elementui网上书城 图书销售商城网站express
    【前端技巧】css篇
    kubernetes学习-概念3
    Windows网络与通信程序设计实验一:基于TCP的C/S通信仿真
    构建供应商全生命周期管理机制,SRM系统规范工程企业供应体系,提升管理水平
    mybatis-3.5.0使用插件拦截sql以及通用字段赋值
    解决Vue项目的runtime-only转为runtime-compiler
    JavaScript:日期对象
    Effective C++ 阅读笔记 06:继承与面向对象设计(上)
    5G用户常驻区域预判及网络支撑前置策略研究
  • 原文地址:https://blog.csdn.net/2301_79450337/article/details/141022487