Redis工具类

package com.tangzhe;

import com.gaiaworks.Application;
import com.gaiaworks.job.entity.pv.PVLog;
import com.gaiaworks.job.util.DateUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
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.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyRedisTest {

    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 = "2018-08-30";
        String key = "500000" + "_pv_" + day;
        PVLog pvLog = new PVLog();

        pvLog.setAppId("500000");
        pvLog.setUserId("100");
        pvLog.setRequestURL("/shop/test01");
        pvLog.setRequestTime(new Date());
        jedis.select(1);
        jedis.zincrby(key.getBytes(), 10, pvLog.getRequestURL().getBytes());

        pvLog.setAppId("500000");
        pvLog.setUserId("100");
        pvLog.setRequestURL("/shop/test02");
        pvLog.setRequestTime(new Date());

        jedis.select(1);
        jedis.zincrby(key.getBytes(), 20, pvLog.getRequestURL().getBytes());
    }

    @Test
    public void test03() {
        int total = 0;
        String day = "2018-08-30";
        String key = "500000" + "_pv_" + day;
        jedis.select(1);
        Set<Tuple> tuples = jedis.zrevrangeWithScores(key, 0, 100);
        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);
    }


    /**
     * 新增一个uvlog日志
     */
    @Test
    public void test04() {
        String day = DateUtils.formatToString(new Date(), "yyyy-MM-dd");
        String key = "500000" + "_uv_" + day;
        PVLog pvLog = new PVLog();

        pvLog.setAppId("500000");
        pvLog.setUserId("100");
        pvLog.setRequestURL("/shop/test01");
        pvLog.setRequestTime(new Date());
        jedis.select(1);
        jedis.zincrby(key.getBytes(), 10, pvLog.getRequestURL().getBytes());

        pvLog.setAppId("500000");
        pvLog.setUserId("100");
        pvLog.setRequestURL("/shop/test02");
        pvLog.setRequestTime(new Date());

        jedis.select(1);
        jedis.zincrby(key.getBytes(), 20, pvLog.getRequestURL().getBytes());
    }

}

 

posted on 2018-09-03 09:33  动物管理猿  阅读(147)  评论(0编辑  收藏  举报

导航