Skip to content

Commit

Permalink
hotfix: timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
doxxx93 committed Feb 21, 2024
1 parent 7e4c83e commit 01b7900
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
Expand Down Expand Up @@ -41,11 +40,13 @@ public LettuceConnectionFactory lettuceConnectionFactory() {
}

@Bean
public RedisTemplate<String, Object> redistemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
public RedisTemplate<String, String> redistemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper()));
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
public class Paint {

private Long id;
private Boolean isReply;
private Boolean isReply = false;
private Long authorId;
private String authorUsername;
private String authorNickname;
private String authorImagePath;
private String authorStatus;
private Paint quotePaint;
private Paint quotePaint = null;
private LocalDateTime createdAt;
private String text;
private List<HashtagRecord> hashtagRecords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
@RequiredArgsConstructor
public class FollowerPaintMapService {

private final RedisTemplate<String, Object> redistemplate;
private final RedisTemplate<String, String> redistemplate;

public void addPaintToFollowersTimeline(List<Long> followerIds, Long paintId) {
followerIds.forEach(userId -> {
final String key = RedisKeyUtil.constructKey(FOLLOWER_PAINT_TIMELINE_PREFIX.getKey(), userId);
redistemplate.opsForList()
.leftPush(key, paintId);
.leftPush(key, paintId.toString());
redistemplate.expire(key, Duration.ofDays(5));
});
}

public List<Long> getFollowingTimelinePaintIds(final Long userId) {
final String key = RedisKeyUtil.constructKey(FOLLOWER_PAINT_TIMELINE_PREFIX.getKey(), userId);
return Optional.ofNullable(redistemplate.opsForList().range(key, 0, -1))
return Optional.ofNullable(redistemplate.opsForList().range(key, 1, -1))
.orElse(List.of())
.stream()
.map(object -> Long.valueOf(object.toString()))
.map(Long::valueOf)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.palette.easeltimelineservice.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.palette.easeltimelineservice.persistence.domain.Paint;
import org.palette.easeltimelineservice.util.RedisKeyUtil;
Expand All @@ -16,27 +18,44 @@
@RequiredArgsConstructor
public class PaintCacheService {

private final RedisTemplate<String, Object> redistemplate;
private final RedisTemplate<String, String> redistemplate;
private final ObjectMapper objectMapper;

public void cachePaint(Long paintId, Paint paint) {
String key = RedisKeyUtil.constructKey(PAINT_PREFIX.getKey(), paintId);
redistemplate.opsForValue().set(key, paint);
String paintJson;
try {
paintJson = objectMapper.writeValueAsString(paint);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
redistemplate.opsForValue().set(key, paintJson);
redistemplate.opsForSet().add("paint_keys", paintId.toString());
}

public List<Paint> getPaints(final List<Long> paintIds) {
final List<String> keys = RedisKeyUtil.constructKeys(PAINT_PREFIX.getKey(), paintIds);
return Optional.ofNullable(redistemplate.opsForValue().multiGet(keys))
final List<String> value = redistemplate.opsForValue().multiGet(keys);
return Optional.ofNullable(value)
.orElseGet(Collections::emptyList)
.stream()
.map(Paint.class::cast)
.map(json -> {
try {
return objectMapper.readValue(json, Paint.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
})
.toList();
}

public List<Paint> getRandomPaints() {
return Optional.ofNullable(redistemplate.opsForSet().randomMembers(PAINT_PREFIX.getKey(), 200))
List<Long> paintIds = Optional.ofNullable(redistemplate.opsForSet().randomMembers("paint_keys", 200))
.orElseGet(Collections::emptyList)
.stream()
.map(Paint.class::cast)
.map(Long::valueOf)
.distinct()
.toList();
return getPaints(paintIds);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.palette.easeltimelineservice.service;

import java.util.Map;

public record PaintMetrics(Integer replyCount, Integer repaintCount, Integer likeCount) {

private static final Integer DEFAULT_COUNT = 0;
public static final PaintMetrics DEFAULT_METRICS = new PaintMetrics(DEFAULT_COUNT, DEFAULT_COUNT, DEFAULT_COUNT);

public static PaintMetrics of(Integer replyCount, Integer repaintCount, Integer likeCount) {
return new PaintMetrics(
Expand All @@ -11,4 +14,13 @@ public static PaintMetrics of(Integer replyCount, Integer repaintCount, Integer
likeCount == null ? DEFAULT_COUNT : likeCount
);
}


public Map<String, String> asMap() {
return Map.of(
"replyCount", String.valueOf(replyCount),
"repaintCount", String.valueOf(repaintCount),
"likeCount", String.valueOf(likeCount)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor
public class PaintMetricsService {
Expand Down Expand Up @@ -53,14 +55,21 @@ public PaintMetrics getPaintMetrics(final Long pid) {
final Integer replyCount = getPaintMetric(pid, REPLY_COUNT);
final Integer repaintCount = getPaintMetric(pid, REPAINT_COUNT);
final Integer likeCount = getPaintMetric(pid, LIKE_COUNT);

return PaintMetrics.of(replyCount, repaintCount, likeCount);
}

public Integer getPaintMetric(final Long pid, final String metric) {
return (Integer) redistemplate.opsForHash().get(
final String string = Optional.ofNullable(redistemplate.opsForHash().get(
RedisKeyUtil.constructKey(RedisKeyConstants.METRICS_PREFIX.getKey(), pid),
metric
)).orElse("0").toString();
return Integer.parseInt(string);
}

public void create(final Long id) {
redistemplate.opsForHash().putAll(
RedisKeyUtil.constructKey(RedisKeyConstants.METRICS_PREFIX.getKey(), id),
PaintMetrics.DEFAULT_METRICS.asMap()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class TimelineUsecase extends GSocialServiceGrpc.GSocialServiceImplBase {
public void handlePaintCreatedEvent(PaintCreatedEvent paintCreatedEvent) {
final GFollowerIdsResponse followerIds = gRPCSocialClient.getFollowerIds(paintCreatedEvent.authorId());
paintCacheService.cachePaint(paintCreatedEvent.id(), Paint.from(paintCreatedEvent));
paintMetricsService.create(paintCreatedEvent.id());
Optional.ofNullable(paintCreatedEvent.inReplyToPaintId())
.ifPresent(paintMetricsService::incrementReplyCount);
followerPaintMapService.addPaintToFollowersTimeline(followerIds.getFollowerIdsList(), paintCreatedEvent.id());
Expand Down

0 comments on commit 01b7900

Please sign in to comment.