• redis有序集合(sorted set)统计排名


    1.创建PVLog实体类
    import lombok.Data;
    
    import java.util.Date;
    
    @Data
    public class PVLog {
        private String appId;
        private String userId;
        private String deviceId;
        private String ip;
        private String userAgent;
        private String mobile;
        private String mobileType;
        private String requestURL;
        private String privince;
        private String city;
        private String area;
        private Date requestTime;
    }
    
    
    2.创建PVLogCount实体类
    import lombok.Data;
    
    import java.util.Date;
    
    @Data
    public class PVLogCount {
        private String requestURL;
        private Integer count;
    }
    
    

      3.创建UVModel实体类

    import lombok.Data;
    
    import java.io.Serializable;
    import java.util.Date;
    
    @Data
    public class UVModel implements Serializable {
    
        private static final long serialVersionUID = 3827513896393543190L;
        private String ip;
        private String browser;
        private String engine;
        private String engineVersion;
        private String os;
        private String platform;
        private Boolean isMobile;
    
    }

      4.编写测试方法

    import com.alibaba.fastjson.JSONObject;
    import com.stone.MybatisApplication;
    import com.stone.pvlog.entity.PVLog;
    import com.stone.pvlog.entity.PVLogCount;
    import com.stone.pvlog.entity.UVModel;
    import com.stone.utils.RedisUtil;
    import org.junit.Before;
    import org.junit.Ignore;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.util.CollectionUtils;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.Tuple;
    
    import java.time.Duration;
    import java.time.Instant;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Set;
    
    /**
     * @author fangyuanjie
     * @version 1.0.0
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = MybatisApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class MyRedisTest {
    
        @Autowired
        private RedisUtil redisUtil;
    
        private Jedis jedis;
    
    
        @Before
        public void init() {
            jedis = new Jedis("127.0.0.1", 6379);
        }
    
        @Test
        @Ignore
        public void test01() {
            jedis.set("test1", "hello");
            System.out.println(jedis.get("test1"));
        }
    
        /**
         * 新增一个pvlog日志
         */
        @Test
        public void test02() {
    //        String day = DateUtils.formatToString(new Date(), "yyyy-MM-dd");
            String day = "2018-08-30";
            String key = "500000" + "_pv_" + day;
            PVLog pvLog = new PVLog();
    
            pvLog.setAppId("500000");
            pvLog.setUserId("100");
            pvLog.setRequestURL("requestURL001");
            pvLog.setRequestTime(new Date());
            jedis.select(1);
            jedis.zincrby(key.getBytes(), 10, pvLog.getRequestURL().getBytes());
    
            pvLog.setAppId("500000");
            pvLog.setUserId("100");
            pvLog.setRequestURL("requestURL002");
            pvLog.setRequestTime(new Date());
    
            jedis.select(1);
            jedis.zincrby(key.getBytes(), 20, pvLog.getRequestURL().getBytes());
        }
    
        /**
         * 统计pvlog访问量
         */
        @Test
        public void test03() {
            Instant start = Instant.now();
            int total = 0;
    //        String day = DateUtils.formatToString(new Date(), "yyyy-MM-dd");
            String day = "2018-08-25";
            String key = "500000" + "_pv_" + day;
            jedis.select(0);
            Set<Tuple> tuples = jedis.zrevrangeWithScores(key, 0, Long.MAX_VALUE);
            List<PVLogCount> pvLogCounts = new ArrayList<>();
            if (!CollectionUtils.isEmpty(tuples)) {
                for (Tuple tuple : tuples) {
                    PVLogCount count = new PVLogCount();
                    count.setRequestURL(tuple.getElement());
                    count.setCount((int) tuple.getScore());
                    total = total + (int) tuple.getScore();
                    pvLogCounts.add(count);
                }
            }
            System.out.println(pvLogCounts);
            System.out.println("total=" + total);
            Instant end = Instant.now();
            System.out.println(Duration.between(start, end).toMillis());
        }
    
    
        //新增一个uvlog日志
        @Test
        public void test04() {
    //        String day = DateUtils.formatToString(new Date(), "yyyy-MM-dd");
            String day = "2018-08-25";
            String key = "500000" + "_uv_" + day;
            UVModel uvModel = new UVModel();
    //        uvModel.s("500000");
    //        uvModel.s("100");
    //        uvModel.setDeviceId("111");
            uvModel.setIp("127.0.0.1");
            jedis.zincrby(key.getBytes(), 10, JSONObject.toJSONString(uvModel).getBytes());
    
    //        uvModel.setAppId("500000");
    //        uvModel.setUserId("000");
    //        uvModel.setDeviceId("222");
            uvModel.setIp("127.0.0.2");
            jedis.select(1);
            jedis.zincrby(key.getBytes(), 80, JSONObject.toJSONString(uvModel).getBytes());
    
        }
    
        //查询UV
        @Test
        public void test05() {
            Instant start = Instant.now();
    //        String day = DateUtils.formatToString(new Date(), "yyyy-MM-dd");
            String day = "2018-08-25";
            String key = "500000" + "_uv_" + day;
            Set<Tuple> tuples = jedis.zrevrangeWithScores(key.getBytes(), 0, Long.MAX_VALUE);
            if (!CollectionUtils.isEmpty(tuples)) {
                for (Tuple tuple : tuples) {
                    String element = tuple.getElement();
                    int count = (int) tuple.getScore();
                    UVModel model = JSONObject.parseObject(element, UVModel.class);
                    if ("106.88.239.78".equals(model.getIp()) && "Webkit".equals(model.getEngine())) {
                        System.out.println("用户:" + model.getIp() + ",访问次数为:" + count);
                    }
                }
                System.out.println("访问用户数:" + tuples.size());
            }
            Instant end = Instant.now();
            System.out.println(Duration.between(start, end).toMillis());
    
        }
    
    }
     
  • 相关阅读:
    redis主从之过期key判断
    PHP之回调、匿名函数
    调试printf,fprintf (转)
    第一天,来报道
    hdu 4464 browsing history
    腾讯编程马拉松2012第一题
    今天面试悲剧了,看来面试前看考题是很关键的.
    JQuery访问WebService(可访问Java[Xfire])
    自己留备份,不解释.
    MYSQL UNION AND UNION ALL 的区别!
  • 原文地址:https://www.cnblogs.com/jpejie/p/9577330.html
Copyright © 2020-2023  润新知