zoukankan      html  css  js  c++  java
  • Java连接Redis常用操作

    1.去重

    package Data;
    
    import redis.clients.jedis.Jedis;
    
    public class TestRedisUniq {
        public static Jedis jd = new Jedis("localhost", 6379);
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            jd.sadd("uniqset", "aa");
            jd.sadd("uniqset", "a1");
            jd.sadd("uniqset", "aa");
            jd.sadd("uniqset", "a2");
            jd.sadd("uniqset", "aa");
            jd.sadd("uniqset", "a2");
            System.out.println(jd.smembers("uniqset"));
        }
    }

    运行结果:

     2.获取前n名数据

    package Data;
    
    import redis.clients.jedis.Jedis;
    
    public class TestRedisTopN {
        public static Jedis jd = new Jedis("localhost", 6379);
    
        public static void main(String[] args) {
            jd.zadd("topn", 1, "m1");
            jd.zadd("topn", 2, "m2");
            jd.zadd("topn", 11, "m3");
            jd.zadd("topn", 12, "m4");
            jd.zadd("topn", 14, "m5");
            jd.zadd("topn", 15, "m6");
            jd.zadd("topn", 151, "m1");
            jd.zadd("topn", 66, "m2");
            jd.zadd("topn", 3, "m7");
            //获取前5名
            System.out.println(jd.zrevrange("topn", 0, 4));
        }
    }

    运行结果:

     3.获取后n条数据

    import redis.clients.jedis.Jedis;
    
    public class TestRedisLastN {
        public static Jedis jd = new Jedis("localhost", 6379);
    
        public static void main(String[] args) {
            jd.del("nlist");
            lpush("nlist", "a1");
            lpush("nlist", "a2");
            lpush("nlist", "a3");
            lpush("nlist", "a4");
            lpush("nlist", "a5");
            System.out.println(queryTop100("nlist"));
        }
    
        public static void lpush(String key, String value) {
            jd.lpush(key, value);
            jd.ltrim(key, 0, 2);
        }
    
        public static List<String> queryTop100(String key) {
            return jd.lrange(key, 0, 2);
        }
    }

    运行结果:

     4.实现队列

    package Data;
    
    import redis.clients.jedis.Jedis;
    
    public class TestRedisQueue {
        public static Jedis jd = new Jedis("localhost", 6379);
    
        public static void main(String[] args) {
            jd.del("queueList");
            jd.rpush("queueList", "1");
            jd.rpush("queueList", "2");
            jd.lpop("queueList");
            System.out.println(jd.lrange("queueList", 0, -1));
        }
    }
  • 相关阅读:
    jQuery里使用setinterval
    关于java以及JavaScript或者更多的语言中Data类的问题
    重置input checked
    利用CSS变量实现悬浮效果
    vue 可编辑表格组件
    js 可拉伸表格
    vue 自定义拖拽指令
    vue 表格导出excel
    vue 侧边导航栏递归显示
    RUP(Rational Unified Process)统一软件过程概述
  • 原文地址:https://www.cnblogs.com/wanyuan/p/11867792.html
Copyright © 2011-2022 走看看