Skip to content

Commit

Permalink
Merge pull request #134 from Lixuhuilll/del-rating-compatible
Browse files Browse the repository at this point in the history
移除评分系统中的兼容性代码,优化热度值刷入任务的内存占用
  • Loading branch information
dragove authored Sep 4, 2023
2 parents 053ddde + 7868a38 commit 717d2cf
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 355 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package plus.maa.backend.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import plus.maa.backend.repository.entity.Copilot;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

Expand All @@ -13,12 +16,14 @@

public interface CopilotRepository extends MongoRepository<Copilot, String> {

List<Copilot> findAllByDeleteIsFalse();
Page<Copilot> findAllByDeleteIsFalse(Pageable pageable);

Optional<Copilot> findFirstByOrderByCopilotIdDesc();

Optional<Copilot> findByCopilotIdAndDeleteIsFalse(Long copilotId);

List<Copilot> findByCopilotIdInAndDeleteIsFalse(Collection<Long> copilotIds);

Optional<Copilot> findByCopilotId(Long copilotId);

boolean existsCopilotsByCopilotId(Long copilotId);
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/plus/maa/backend/repository/RedisCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -46,6 +50,12 @@ public class RedisCache {
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.build();

/*
使用 lua 脚本插入数据,维持 ZSet 的相对大小(size <= 实际大小 <= size + 50)以及过期时间
实际大小这么设计是为了避免频繁的 ZREMRANGEBYRANK 操作
*/
private final RedisScript<Object> incZSetRedisScript = RedisScript.of(new ClassPathResource("redis-lua/incZSet.lua"));

public <T> void setData(final String key, T value) {
setCache(key, value, 0, TimeUnit.SECONDS);
}
Expand Down Expand Up @@ -103,6 +113,35 @@ public <T> void addSet(final String key, Collection<T> set, long timeout, TimeUn
}
}


/**
* ZSet 中元素的 score += incScore,如果元素不存在则插入 <br>
* 会维持 ZSet 的相对大小(size <= 实际大小 <= size + 50)以及过期时间 <br>
* 当大小超出 size + 50 时,会优先删除 score 最小的元素,直到大小等于 size
*
* @param key ZSet 的 key
* @param member ZSet 的 member
* @param incScore 增加的 score
* @param size ZSet 的相对大小
* @param timeout ZSet 的过期时间
*/

public void incZSet(final String key, String member, double incScore, long size, long timeout) {
redisTemplate.execute(incZSetRedisScript, List.of(key), member, Double.toString(incScore), Long.toString(size), Long.toString(timeout));
}

// 获取的元素是按照 score 从小到大排列的
@Nullable
public Set<String> getZSet(final String key, long start, long end) {
return redisTemplate.opsForZSet().range(key, start, end);
}

// 获取的元素是按照 score 从大到小排列的
@Nullable
public Set<String> getZSetReverse(final String key, long start, long end) {
return redisTemplate.opsForZSet().reverseRange(key, start, end);
}

public <T> boolean valueMemberInSet(final String key, T value) {
try {
String json = writeMapper.writeValueAsString(value);
Expand All @@ -117,18 +156,22 @@ public <T> boolean valueMemberInSet(final String key, T value) {
return false;
}

@Nullable
public <T> T getCache(final String key, Class<T> valueType) {
return getCache(key, valueType, null, expire, TimeUnit.SECONDS);
}

@Nullable
public <T> T getCache(final String key, Class<T> valueType, Supplier<T> onMiss) {
return getCache(key, valueType, onMiss, expire, TimeUnit.SECONDS);
}

@Nullable
public <T> T getCache(final String key, Class<T> valueType, Supplier<T> onMiss, long timeout) {
return getCache(key, valueType, onMiss, timeout, TimeUnit.SECONDS);
}

@Nullable
public <T> T getCache(final String key, Class<T> valueType, Supplier<T> onMiss, long timeout, TimeUnit timeUnit) {
T result;
try {
Expand Down Expand Up @@ -188,6 +231,7 @@ public <T> void updateCache(final String key, Class<T> valueType, T defaultValue
}
}

@Nullable
public String getCacheLevelCommit() {
return getCache("level:commit", String.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author LoMu
Expand All @@ -34,9 +33,6 @@ public class CommentsArea implements Serializable {
//评论内容
private String message;

//赞 踩
private List<CopilotRating.RatingUser> ratingUser;

private long likeCount;

private LocalDateTime uploadTime = LocalDateTime.now();
Expand Down

This file was deleted.

40 changes: 4 additions & 36 deletions src/main/java/plus/maa/backend/service/CommentsAreaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import plus.maa.backend.repository.CopilotRepository;
import plus.maa.backend.repository.RatingRepository;
import plus.maa.backend.repository.UserRepository;
import plus.maa.backend.repository.entity.*;
import plus.maa.backend.repository.entity.CommentsArea;
import plus.maa.backend.repository.entity.Copilot;
import plus.maa.backend.repository.entity.MaaUser;
import plus.maa.backend.repository.entity.Rating;
import plus.maa.backend.service.model.CommentNotification;
import plus.maa.backend.service.model.RatingType;

Expand Down Expand Up @@ -189,41 +192,6 @@ public void rates(String userId, CommentsRatingDTO commentsRatingDTO) {
String rating = commentsRatingDTO.getRating();

CommentsArea commentsArea = findCommentsById(commentsRatingDTO.getCommentId());
List<CopilotRating.RatingUser> ratingUserList = commentsArea.getRatingUser();

// 判断旧评分是否存在 如果存在则迁移评分
if (ratingUserList != null && !ratingUserList.isEmpty()) {
long likeCount = commentsArea.getLikeCount();
List<Rating> ratingList = new ArrayList<>();
for (CopilotRating.RatingUser ratingUser : ratingUserList) {

Rating newRating = new Rating()
.setType(Rating.KeyType.COMMENT)
.setKey(commentsArea.getId())
.setUserId(ratingUser.getUserId())
.setRating(RatingType.fromRatingType(ratingUser.getRating()))
.setRateTime(ratingUser.getRateTime());
ratingList.add(newRating);

if (Objects.equals(userId, ratingUser.getUserId())) {
if (Objects.equals(rating, ratingUser.getRating())) {
continue;
}
RatingType oldRatingType = newRating.getRating();
newRating.setRating(RatingType.fromRatingType(rating));
newRating.setRateTime(LocalDateTime.now());
likeCount += newRating.getRating() == RatingType.LIKE ? 1 :
(oldRatingType != RatingType.LIKE ? 0 : -1);
}
}
if (likeCount < 0) {
likeCount = 0;
}
commentsArea.setLikeCount(likeCount);
commentsArea.setRatingUser(null);
ratingRepository.insert(ratingList);
commentsAreaRepository.save(commentsArea);
} // 迁移用代码结束,如不再需要可完全删除该 if 判断

long change;
Optional<Rating> ratingOptional = ratingRepository.findByTypeAndKeyAndUserId(Rating.KeyType.COMMENT, commentsArea.getId(), userId);
Expand Down
Loading

0 comments on commit 717d2cf

Please sign in to comment.