添加部分cache

This commit is contained in:
Himit_ZH 2021-09-07 21:20:26 +08:00
parent 85469ce411
commit 293fd74aee
8 changed files with 68 additions and 17 deletions

View File

@ -472,7 +472,7 @@ public class ContestController {
List<ContestRecord> contestRecordList = contestRecordService.list(wrapper);
// 进行排行榜计算以及排名分页
resultList = contestRecordService.getContestACMRank(isOpenSealRank,contest, contestRecordList, currentPage, limit);
resultList = contestRecordService.getContestACMRank(isOpenSealRank, contest, contestRecordList, currentPage, limit);
} else { //OI比赛以最后一次提交得分作为该题得分

View File

@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
@ -13,7 +14,7 @@ import java.util.List;
*/
@ApiModel(value="OI排行榜数据类OIRankVo", description="")
@Data
public class OIRankVo {
public class OIRankVo implements Serializable {
@ApiModelProperty(value = "用户id")
private String uid;

View File

@ -16,6 +16,7 @@ import top.hcode.hoj.service.ContestRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.hcode.hoj.utils.Constants;
import top.hcode.hoj.utils.RedisUtils;
import java.util.*;
import java.util.stream.Collectors;
@ -38,6 +39,9 @@ public class ContestRecordServiceImpl extends ServiceImpl<ContestRecordMapper, C
@Autowired
private UserInfoMapper userInfoMapper;
@Autowired
private RedisUtils redisUtils;
@Override
public IPage<ContestRecord> getACInfo(Integer currentPage, Integer limit, Integer status, Long cid, String contestCreatorId) {
@ -132,11 +136,29 @@ public class ContestRecordServiceImpl extends ServiceImpl<ContestRecordMapper, C
@Override
public IPage<OIContestRankVo> getContestOIRank(Long cid, String contestAuthor, Boolean isOpenSealRank, Date sealTime, Date startTime, Date endTime, int currentPage, int limit) {
public IPage<OIContestRankVo> getContestOIRank(Long cid, String contestAuthor, Boolean isOpenSealRank, Date sealTime,
Date startTime, Date endTime, int currentPage, int limit) {
// 获取每个用户每道题最近一次提交
List<ContestRecord> oiContestRecord = contestRecordMapper.getOIContestRecord(cid, contestAuthor, isOpenSealRank, sealTime, startTime, endTime);
if (!isOpenSealRank) {
// 超级管理员和比赛管理员选择强制刷新 或者 比赛结束
return getOIContestRank(cid, contestAuthor, false, sealTime, startTime, startTime, currentPage, limit);
} else {
String key = Constants.Contest.OI_CONTEST_RANK_CACHE.getName() + "_" + cid;
Page<OIContestRankVo> page = (Page<OIContestRankVo>) redisUtils.get(key);
if (page == null) {
page = getOIContestRank(cid, contestAuthor, true, sealTime, startTime, startTime, currentPage, limit);
redisUtils.set(key, page, 2 * 3600);
}
return page;
}
}
private Page<OIContestRankVo> getOIContestRank(Long cid, String contestAuthor, Boolean isOpenSealRank, Date sealTime,
Date startTime, Date endTime, int currentPage, int limit) {
List<ContestRecord> oiContestRecord = contestRecordMapper.getOIContestRecord(cid, contestAuthor, isOpenSealRank, sealTime, startTime, endTime);
// 计算排名
List<OIContestRankVo> orderResultList = calcOIRank(oiContestRecord);
@ -153,7 +175,6 @@ public class ContestRecordServiceImpl extends ServiceImpl<ContestRecordMapper, C
page.setCurrent(currentPage);
page.setTotal(count);
page.setRecords(pageList);
return page;
}
@ -256,7 +277,7 @@ public class ContestRecordServiceImpl extends ServiceImpl<ContestRecordMapper, C
int errorNumber = (int) problemSubmissionInfo.getOrDefault("errorNum", 0);
problemSubmissionInfo.put("errorNum", errorNumber + 1);
}else{
} else {
int errorNumber = (int) problemSubmissionInfo.getOrDefault("errorNum", 0);
problemSubmissionInfo.put("errorNum", errorNumber);

View File

@ -240,7 +240,7 @@ public class ScheduleServiceImpl implements ScheduleService {
@Retryable(value = Exception.class,
maxAttempts = 5,
backoff = @Backoff(delay = 500))
backoff = @Backoff(delay = 500,multiplier = 1.4))
public JSONObject getCFUserInfo(String url) throws Exception {
return JsoupUtils.getJsonFromConnection(JsoupUtils.getConnectionFromUrl(url, null, null));
}

View File

@ -10,6 +10,8 @@ import top.hcode.hoj.pojo.vo.UserHomeVo;
import top.hcode.hoj.service.UserRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.hcode.hoj.utils.Constants;
import top.hcode.hoj.utils.RedisUtils;
import java.util.List;
@ -27,15 +29,29 @@ public class UserRecordServiceImpl extends ServiceImpl<UserRecordMapper, UserRec
@Autowired
private UserRecordMapper userRecordMapper;
@Autowired
private RedisUtils redisUtils;
// 排行榜缓存时间 60s
private static final long cacheRankSecond = 60;
@Override
public Page<ACMRankVo> getACMRankList(int limit, int currentPage) {
//新建分页
Page<ACMRankVo> page = new Page<>(currentPage, limit);
String key = Constants.Account.ACM_RANK_CACHE.getCode() + "_" + limit + "_" + currentPage;
return page.setRecords(userRecordMapper.getACMRankList(page));
List<ACMRankVo> data = (List<ACMRankVo>) redisUtils.get(key);
if (data == null) {
data = userRecordMapper.getACMRankList(page);
redisUtils.set(key, data, cacheRankSecond);
}
return page.setRecords(data);
}
@Override
public List<ACMRankVo> getRecent7ACRank() {
return userRecordMapper.getRecent7ACRank();
@ -43,10 +59,18 @@ public class UserRecordServiceImpl extends ServiceImpl<UserRecordMapper, UserRec
@Override
public Page<OIRankVo> getOIRankList(int limit, int currentPage) {
//新建分页
Page<OIRankVo> page = new Page<>(currentPage, limit);
return page.setRecords(userRecordMapper.getOIRankList(page));
Page<OIRankVo> page = new Page<>(currentPage, limit);
String key = Constants.Account.OI_RANK_CACHE.getCode() + "_" + limit + "_" + currentPage;
List<OIRankVo> data = (List<OIRankVo>) redisUtils.get(key);
if (data == null) {
data = userRecordMapper.getOIRankList(page);
redisUtils.set(key, data, cacheRankSecond);
}
return page.setRecords(data);
}
@Override

View File

@ -101,7 +101,9 @@ public class Constants {
RECORD_NOT_AC_PENALTY(-1, "未AC通过算罚时"),
RECORD_NOT_AC_NOT_PENALTY(0, "未AC通过不罚时"),
RECORD_AC(1, "AC通过");
RECORD_AC(1, "AC通过"),
OI_CONTEST_RANK_CACHE(null,"oi_contest_rank_cache");
private final Integer code;
private final String name;
@ -130,7 +132,10 @@ public class Constants {
CODE_CHANGE_PASSWORD_LOCK("change-password-lock:"),
CODE_ACCOUNT_LOCK("account-lock:"),
CODE_CHANGE_EMAIL_FAIL("change-email-fail:"),
CODE_CHANGE_EMAIL_LOCK("change-email-lock:");
CODE_CHANGE_EMAIL_LOCK("change-email-lock:"),
ACM_RANK_CACHE("acm_rank_cache"),
OI_RANK_CACHE("oi_rank_cache");
private final String code;

View File

@ -224,7 +224,7 @@ export default {
}, 1000);
},
sendRegisterEmail() {
var emailReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
var emailReg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!emailReg.test(this.registerForm.email)) {
mMessage.error(this.$i18n.t('m.Email_Check_Format'));
return;

View File

@ -53,7 +53,7 @@ export const m = {
Code_Check_Required:'验证码不能为空',
Code_Check_Length:'请输入6位数字的验证码',
The_system_is_processing:'请稍等... 系统正在处理...',
Register_Send_Email_Msg:'验证码已发送至指定邮箱!如果长时间没收到邮件,请检查你的邮箱是否准确!',
Register_Send_Email_Msg:'发送成功!如果长时间没收到邮件,请检查你的邮箱是否准确!',
Thanks_for_registering: '感谢您的注册,您现在可以登录了',
// /components/oj/common/ResetPassword.vue 重置密码弹窗
@ -62,7 +62,7 @@ export const m = {
Reset_Password_Captcha: '请输入验证码',
Send_Password_Reset_Email: '发送密码重置邮件',
Waiting_Can_Resend_Email:'请稍等片刻,重新发送重置邮件...',
ResetPwd_Send_Email_Msg:'重置密码邮件已发送至指定邮箱!如果长时间没收到邮件,请检查你的邮箱是否准确!',
ResetPwd_Send_Email_Msg:'发送成功!如果长时间没收到邮件,请检查你的邮箱是否准确!',
Remember_Passowrd_To_Login:'咦,好像记得密码? 请尝试登录!',
Set_New_Password:'设置新密码',
Set_New_Password_Msg: '请输入新密码',