Skip to content

Commit

Permalink
refactor: 重构活跃最近活跃实现(基于Redis缓存)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangrunkang committed Apr 5, 2023
1 parent 8b41bcd commit b3c90af
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import lombok.Builder;
import lombok.Data;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -69,7 +70,7 @@ public class MemberEnhance {

private String createDate;
private String createDateDiff;
private String lastLoginDate;
private String lastActiveTime;
/**
* 创建时间短 年-月-日
*/
Expand Down Expand Up @@ -128,12 +129,18 @@ public String getCreateDate() {
return createDate;
}

public String getLastLoginDate() {
Long lastLoginTime = member.getLastLoginTime();
if (Objects.nonNull(lastLoginTime) && lastLoginTime > 0) {
return CcDateUtil.timeStamp2Date(lastLoginTime);
public String getLastActiveTime() {
String latestActiveTimeValue = RedisUtil.get(CcRedis.Key.userActiveKey(member.getUserId()));
if (StringUtils.isEmpty(latestActiveTimeValue)) {
// 默认返回最后一次登录时间
return CcDateUtil.timeStamp2Date(member.getLastLoginTime());
}
return lastLoginDate;
Long latestActiveTime = Long.valueOf(latestActiveTimeValue);
if (Objects.nonNull(latestActiveTime) && latestActiveTime > 0) {
return CcDateUtil.timeStamp2Date(latestActiveTime);
}
// 默认返回最后一次登录时间
return CcDateUtil.timeStamp2Date(member.getLastLoginTime());
}

public String getCreateDateShort() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,6 @@ public static final class Session {


public static final class Time {
// 用户只要活跃一次,维持活跃状态的时间
public static final long MEMBER_ACTIVE_TIME = 60 * 60L;
// 文章只要编辑过,维持更新标识的时间
public static final long CONTENT_UPDATE_TIME = 24 * 60 * 60L;
// 最新的新文章 7天内都算
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static class Key {
* 文章发布间隔
*/
public static final String CREATE_CONTENT_TIME_OUT = "create_content_time_out";
public static final String REFRESH_KEY = "refresh_key";
public static final String ACTIVE_KEY = "member_active_key";

/**
* SiteMap
Expand All @@ -82,8 +82,8 @@ public static String createContentIntervalKey(String userId) {
* @param userId
* @return
*/
public static String refreshActiveMemberListKey(String userId) {
return REFRESH_KEY + userId;
public static String userActiveKey(String userId) {
return ACTIVE_KEY + userId;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import com.upupor.data.dao.entity.Tag;
import com.upupor.data.dao.entity.enhance.ContentEnhance;
import com.upupor.data.dao.entity.enhance.MemberEnhance;
import com.upupor.data.dao.entity.enhance.TagEnhance;
import com.upupor.data.dto.cache.CacheMemberDto;
import com.upupor.data.dto.page.CommonPageIndexDto;
Expand Down Expand Up @@ -146,6 +147,10 @@ public CommonPageIndexDto index(GetCommonReq getCommonReq) {
String activeUserListJson = RedisUtil.get(CcRedis.Key.ACTIVE_USER_LIST);
if (!StringUtils.isEmpty(activeUserListJson)) {
cacheMemberDto = JsonUtils.parse2Clazz(activeUserListJson, CacheMemberDto.class);
// 因为活跃用户列表是缓存的,会存在延后,所有这里重新根据Redis里面的最新数据覆盖
for (MemberEnhance memberEnhance : cacheMemberDto.getMemberEnhanceList()) {
memberEnhance.setLastActiveTime(CcRedis.Key.userActiveKey(memberEnhance.getMember().getUserId()));
}
}
return cacheMemberDto;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ public void login(MemberLoginEvent memberLoginEvent) throws ParseException {
// 登录送积分
loginIntegral(memberLoginEvent);
// 记录最新登录时间
recordLastLoginTime(memberLoginEvent);
recordTheLatestLoginTime(memberLoginEvent);
}

/**
* 记录最新登录时间
*
* @param memberLoginEvent
*/
private void recordLastLoginTime(MemberLoginEvent memberLoginEvent) {
private void recordTheLatestLoginTime(MemberLoginEvent memberLoginEvent) {
String userId = memberLoginEvent.getUserId();
if (StringUtils.isEmpty(userId)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.upupor.data.dao.mapper.DraftMapper;
import com.upupor.framework.CcConstant;
import com.upupor.framework.CcRedis;
import com.upupor.framework.utils.CcDateUtil;
import com.upupor.framework.utils.RedisUtil;
import com.upupor.service.base.MemberService;
import com.upupor.service.business.lucene.LuceneService;
Expand All @@ -57,8 +58,6 @@
import java.util.List;
import java.util.Objects;

import static com.upupor.framework.CcConstant.Time.MEMBER_ACTIVE_TIME;


/**
* Upupor 监听器
Expand All @@ -76,7 +75,7 @@ public class UpuporListener {
private final ContentExtendMapper contentExtendMapper;
private final DraftMapper draftMapper;
private final CommentMapper commentMapper;

private static final String ZIPPED = "zipped";
private static final String ZIP_KEY = "zip";

Expand All @@ -103,18 +102,14 @@ public void buriedPointData(BuriedPointDataEvent event) {
CcRedis.Operate.updateTokenExpireTime(userId);
// 延长活跃Key
CcRedis.Operate.memberActive(userId);
// 刷新最近登录的用户
refreshActiveMemberList(userId);
// 刷新用户活跃时间
refreshUserActiveTime(userId);
}
}

private void refreshActiveMemberList(String userId) {
String refreshKey = CcRedis.Key.refreshActiveMemberListKey(userId);
if (RedisUtil.exists(refreshKey)) {
return;
}
RedisUtil.set(refreshKey, refreshKey, MEMBER_ACTIVE_TIME);
taskService.refreshActiveMember();
private void refreshUserActiveTime(String userId) {
String userActiveKey = CcRedis.Key.userActiveKey(userId);
RedisUtil.set(userActiveKey, String.valueOf(CcDateUtil.getCurrentTime()));
}

@EventListener
Expand Down
2 changes: 1 addition & 1 deletion upupor-web/src/main/resources/templates/content/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ <h5 class="text-wrap m-0" th:text="${contentIndexDto.contentEnhance.content.titl
</a>
</p>
<div>
<p style="text-align: center" class="text-black-50 small m-0 p-1" th:if="!${#strings.isEmpty(contentIndexDto.contentEnhance.memberEnhance.lastLoginDate)}" th:inline="text">最近活跃: [[${contentIndexDto.contentEnhance.memberEnhance.lastLoginDate}]]</p>
<p style="text-align: center" class="text-black-50 small m-0 p-1" th:if="!${#strings.isEmpty(contentIndexDto.contentEnhance.memberEnhance.lastActiveTime)}" th:inline="text">最近活跃: [[${contentIndexDto.contentEnhance.memberEnhance.lastActiveTime}]]</p>
</div>
<!--关注按钮-->
<button th:if="${session?.cv_user_id != contentIndexDto.contentEnhance.content.userId} and !${contentIndexDto.currUserIsAttention}" class="btn rounded-3 bg-gradient btn-warning btn-sm w-100" th:onclick="attentionAuthor([[${contentIndexDto.contentEnhance.content.userId}]])" id="attention_btn">
Expand Down
4 changes: 2 additions & 2 deletions upupor-web/src/main/resources/templates/fragments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,8 @@ <h4 class="alert-heading">危险操作区域</h4>
<div class="mt-2"><span class="badge bg-gradient rounded-3 fw-normal bg-warning text-dark"
th:text="${memberIndexDto.memberEnhance.createDateShort}">42</span> 加入Upupor大家庭
</div>
<div class="mt-2" th:if="!${#strings.isEmpty(memberIndexDto.memberEnhance.lastLoginDate)}" th:inline="text">
最近活跃: <span class="badge bg-gradient rounded-3 fw-normal bg-warning text-dark">[[${memberIndexDto.memberEnhance.lastLoginDate}]]</span>
<div class="mt-2" th:if="!${#strings.isEmpty(memberIndexDto.memberEnhance.lastActiveTime)}" th:inline="text">
最近活跃: <span class="badge bg-gradient rounded-3 fw-normal bg-warning text-dark">[[${memberIndexDto.memberEnhance.lastActiveTime}]]</span>
</div>
<div class="mt-2 mb-1" th:inline="text">积分: <span
class="badge bg-gradient rounded-3 fw-normal bg-warning text-dark">[[${memberIndexDto.memberEnhance.totalIntegral}]]</span>
Expand Down
4 changes: 2 additions & 2 deletions upupor-web/src/main/resources/templates/fragments/member.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
th:text="${#strings.isEmpty(memberEnhance?.memberExtendEnhance?.memberExtend?.introduce)?'&nbsp;':memberEnhance?.memberExtendEnhance?.memberExtend?.introduce}">
</div>
</div>
<!-- <span class="text-black-50 small" th:if="!${#strings.isEmpty(memberEnhance?.lastLoginDate)}"-->
<!-- th:text="${memberEnhance?.lastLoginDate}+' 登录'">最近登录时间</span>&nbsp;&nbsp;&nbsp;-->
<!-- <span class="text-black-50 small" th:if="!${#strings.isEmpty(memberEnhance?.lastActiveTime)}"-->
<!-- th:text="${memberEnhance?.lastActiveTime}+' 登录'">最近登录时间</span>&nbsp;&nbsp;&nbsp;-->
<a class="btn rounded-3 bg-gradient btn-warning btn-sm"
th:href="'/profile/' + ${memberEnhance.member.userId}+'/message'" th:inline="text">
<img th:data-src="${ossStatic} + @{/icons/system/goutong.png}" class="lazyload cv-icon-item"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ <h5 class="mt-2">[[${radioIndexDto.radioEnhance.radio.radioIntro}]]</h5>
</p>
</div>
<div>
<p style="text-align: center" class="text-black-50 small m-0 p-1" th:if="!${#strings.isEmpty(radioIndexDto.radioEnhance.memberEnhance.lastLoginDate)}" th:inline="text">最近活跃: [[${radioIndexDto.radioEnhance.memberEnhance.lastLoginDate}]]</p>
<p style="text-align: center" class="text-black-50 small m-0 p-1" th:if="!${#strings.isEmpty(radioIndexDto.radioEnhance.memberEnhance.lastActiveTime)}" th:inline="text">最近活跃: [[${radioIndexDto.radioEnhance.memberEnhance.lastActiveTime}]]</p>
</div>
</div>
<!--访问者-->
Expand Down

0 comments on commit b3c90af

Please sign in to comment.