java redis 实现pv uv_同步Redis中PV&UV数据到MongoDB的定时任务

packagenet.baiqu.shop.data.core.job;importlombok.extern.slf4j.Slf4j;importnet.baiqu.shop.data.core.util.DateUtils;importnet.baiqu.shop.data.core.util.RedisUtil;importnet.baiqu.shop.data.dal.entity.document.MPv;importnet.baiqu.shop.data.dal.entity.document.MShopPvStatistic;importnet.baiqu.shop.data.dal.entity.document.MShopUvStatistic;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.mongodb.core.MongoTemplate;importorg.springframework.data.mongodb.core.query.Criteria;importorg.springframework.data.mongodb.core.query.Query;importorg.springframework.data.mongodb.core.query.Update;importorg.springframework.scheduling.annotation.EnableScheduling;importorg.springframework.scheduling.annotation.Scheduled;importorg.springframework.stereotype.Component;importorg.springframework.util.CollectionUtils;importredis.clients.jedis.Jedis;importredis.clients.jedis.Tuple;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importjava.util.Set;/*** 同步redis和mongodb中pv uv统计数据的定时任务*/@Component

@EnableScheduling

@Slf4jpublic classPvUvJob {/**权益商城appId*/

private static final String SHOP_APP_ID = "500000";/**redis片区*/

private static final int REDIS_INDEX = 0;/**权益商城PV统计collection*/

private static final String SHOP_PV_COLLECTION = "shop_pv_statistic";/**权益商城UV统计collection*/

private static final String SHOP_UV_COLLECTION = "shop_uv_statistic";

@AutowiredprivateMongoTemplate mongoTemplate;

@AutowiredprivateRedisUtil redisUtil;/*** 同步Redis中PV数据到MongoDB定时任务*/@Scheduled(fixedRate= 10 * 60 * 1000)public voidredisPvToMongodbTask() {

String day= DateUtils.formatToString(new Date(), "yyyy-MM-dd");

String key= SHOP_APP_ID + "_pv_" +day;

log.info("PvUvJob.redisPvToMongodbTask: 开始同步{}数据", key);//查询mongodb中是否有当天的pv统计数据,有则更新,无则插入

MShopPvStatistic shopPvStatistic =mongoTemplate.findOne(

Query.query(

Criteria.where("day").is(day)

), MShopPvStatistic.class);

List pvList = new ArrayList<>();//查询redis中的pv数据

long total = this.queryPvByRedis(key, pvList);if (shopPvStatistic == null) {//插入//封装pv统计数据

shopPvStatistic = newMShopPvStatistic();

shopPvStatistic.setAppId(SHOP_APP_ID);

shopPvStatistic.setDay(day);

shopPvStatistic.setPvList(pvList);

shopPvStatistic.setTotal(total);

shopPvStatistic.setCreateTime(newDate());

shopPvStatistic.setUpdateTime(newDate());//将pv统计数据存入mongo中

mongoTemplate.insert(shopPvStatistic, SHOP_PV_COLLECTION);

log.info("PvUvJob.redisPvToMongodbTask: 插入{}数据到mongo", key);

}else{//更新

mongoTemplate.updateFirst(

Query.query(new Criteria("day").is(day)

),

Update.update("total", total)

.set("pvList", pvList)

.set("updateTime", newDate()),

MShopPvStatistic.class);

log.info("PvUvJob.redisPvToMongodbTask: 更新{}数据到mongo", key);

}

log.info("PvUvJob.redisPvToMongodbTask: 完成同步{}数据", key);

}/*** 同步Redis中UV数据到MongoDB定时任务*/@Scheduled(fixedRate= 10 * 60 * 1000)public voidredisUvToMongodbTask() {

String day= DateUtils.formatToString(new Date(), "yyyy-MM-dd");

String key= SHOP_APP_ID + "_uv_" +day;

log.info("PvUvJob.redisUvToMongodbTask: 开始同步{}数据", key);//查询mongodb中是否有当天的uv统计数据,有则更新,无则插入

MShopUvStatistic shopUvStatistic =mongoTemplate.findOne(

Query.query(

Criteria.where("day").is(day)

), MShopUvStatistic.class);//查询redis中的uv数据

long total = this.queryUvByRedis(key);if (shopUvStatistic == null) {//插入//封装uv统计数据

shopUvStatistic = newMShopUvStatistic();

shopUvStatistic.setAppId(SHOP_APP_ID);

shopUvStatistic.setDay(day);

shopUvStatistic.setTotal(total);

shopUvStatistic.setCreateTime(newDate());

shopUvStatistic.setUpdateTime(newDate());//将pv统计数据存入mongo中

mongoTemplate.insert(shopUvStatistic, SHOP_UV_COLLECTION);

log.info("PvUvJob.redisUvToMongodbTask: 插入{}数据到mongo", key);

}else{//更新

mongoTemplate.updateFirst(

Query.query(new Criteria("day").is(day)

),

Update.update("total", total)

.set("updateTime", newDate()),

MShopUvStatistic.class);

log.info("PvUvJob.redisUvToMongodbTask: 更新{}数据到mongo", key);

}

log.info("PvUvJob.redisUvToMongodbTask: 完成同步{}数据", key);

}/*** 查询Redis中PV数据*/

private long queryPvByRedis(String key, ListpvList) {

Jedis jedis=redisUtil.getJedis();long total = 0;

jedis.select(REDIS_INDEX);

Set tuples = jedis.zrevrangeWithScores(key, 0, Long.MAX_VALUE);if (!CollectionUtils.isEmpty(tuples)) {for(Tuple tuple : tuples) {

MPv pv= newMPv();

pv.setRequestURL(tuple.getElement());

pv.setCount((long) tuple.getScore());

total= total + (long) tuple.getScore();

pvList.add(pv);

}

}returntotal;

}/*** 查询Redis中UV数据*/

private longqueryUvByRedis(String key) {

Jedis jedis=redisUtil.getJedis();

jedis.select(REDIS_INDEX);

Set tuples = jedis.zrevrangeWithScores(key, 0, Long.MAX_VALUE);returntuples.size();

}

}

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页