Commit 63ba2d25 authored by 刘基明's avatar 刘基明

topic锁

parent f0106844
......@@ -3,12 +3,14 @@ package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.biz.common.enums.clue.PageEnum;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.TimesCountEntity;
import com.tanpu.community.dao.entity.community.VisitLogEntity;
import com.tanpu.community.dao.mapper.community.VisitLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -29,6 +31,9 @@ public class VisitLogService {
@Resource
private VisitLogMapper visitLogMapper;
@Resource
private RedisCache redisCache;
// 获取用户7天内访问过的
public List<String> queryUserRecentVisited(String userId) {
Date endDate = new Date();
......@@ -59,13 +64,26 @@ public class VisitLogService {
@Transactional
public void insertOrUpdateDur(VisitLogEntity vs) {
if (visitLogMapper.selectByIdentAndRefId(vs.getIdent(), vs.getRefId(), vs.getRefType()) == null) {
visitLogMapper.insert(vs);
} else {
visitLogMapper.updateDurByIdent(vs.getDuration(), vs.getIdent());
// 分布式锁
for (;;) {
String key = getVisitLogRedisKey(vs);
if (redisCache.setIfAbsent(key, "1", 15)) {
if (visitLogMapper.selectByIdentAndRefId(vs.getIdent(), vs.getRefId(), vs.getRefType()) == null) {
visitLogMapper.insert(vs);
} else {
visitLogMapper.updateDurByIdent(vs.getDuration(), vs.getIdent());
}
// 如果执行超时,会删除别的实例的key
redisCache.evict(key);
return;
}
}
}
private String getVisitLogRedisKey(VisitLogEntity vs) {
return StringUtils.join("insertOrUpdateDur:", vs.getIdent().substring(0, 4), vs.getRefId(), vs.getRefType());
}
@Transactional
//TODO 临时埋点,接入新埋点后删除
public void addPageView(String userId, String targetId, PageEnum type) {
......
......@@ -60,6 +60,8 @@ spring:
enabled: false
zipkin:
enabled: false
redis:
database: 7
aliyun:
oss:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment