Skip to content

Commit

Permalink
add: added bitmap cache
Browse files Browse the repository at this point in the history
  • Loading branch information
thguss committed Oct 8, 2024
1 parent 043d87e commit fe6eccb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.smeem.application.domain.badge.Badge;
import com.smeem.application.domain.trainingtime.DayType;
import com.smeem.application.domain.trainingtime.TrainingTime;
import com.smeem.application.domain.visit.Visit;
import com.smeem.application.port.input.MemberUseCase;
import com.smeem.application.port.input.dto.request.member.UpdateMemberHasPushAlarmRequest;
import com.smeem.application.port.input.dto.request.member.UpdateMemberRequest;
Expand All @@ -13,6 +12,7 @@
import com.smeem.application.port.input.dto.response.member.UpdateMemberResponse;
import com.smeem.application.port.input.dto.response.member.UsernameDuplicatedResponse;
import com.smeem.application.port.input.dto.response.plan.RetrieveMemberPlanResponse;
import com.smeem.application.port.output.cache.CachePort;
import com.smeem.application.port.output.persistence.*;
import com.smeem.common.logger.HookLogger;
import com.smeem.common.logger.LoggingMessage;
Expand All @@ -21,6 +21,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

@Service
Expand All @@ -31,10 +33,10 @@ public class MemberService implements MemberUseCase {
private final TrainingTimePort trainingTimePort;
private final BadgePort badgePort;
private final DiaryPort diaryPort;
private final VisitPort visitPort;
private final GoalPort goalPort;
private final PlanPort planPort;
private final HookLogger hookLogger;
private final CachePort cachePort;

@Transactional
public UpdateMemberResponse updateMember(long memberId, UpdateMemberRequest request) {
Expand Down Expand Up @@ -90,11 +92,15 @@ public RetrievePerformanceResponse retrieveMemberPerformance(long memberId) {

@Transactional
public void visit(long memberId) {
val foundMember = memberPort.findById(memberId);
if (!visitPort.isExistByMemberAndToday(foundMember.getId())) {
Member foundMember = memberPort.findById(memberId);
String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String key = "visit:" + today;

if (!cachePort.getBit(key, foundMember.getId())) {
System.out.println("test");
foundMember.visit();
memberPort.update(foundMember);
visitPort.visit(new Visit(foundMember.getId()));
cachePort.setBit(key, foundMember.getId(), true);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.smeem.application.port.output.cache;

public interface CachePort {
void setBit(String key, long offset, boolean value);
boolean getBit(String key, long offset);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.smeem.output.cache.redis.adapter;

import com.smeem.application.port.output.cache.CachePort;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@RequiredArgsConstructor
public class CacheAdapter implements CachePort {
private final RedisTemplate<String, String> redisTemplate;

@Override
public void setBit(String key, long offset, boolean value) {
redisTemplate.opsForValue().setBit(key, offset, value);
}

@Override
public boolean getBit(String key, long offset) {
return Boolean.TRUE.equals(redisTemplate.opsForValue().getBit(key, offset));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class MemberAdapter implements MemberPort {
private final DeletedDiaryRepository deletedDiaryRepository;
private final DiaryRepository diaryRepository;
private final TrainingTimeRepository trainingTimeRepository;
private final VisitRepository visitRepository;
private final WithdrawRepository withdrawRepository;

@Override
Expand Down Expand Up @@ -55,7 +54,6 @@ public void deleteById(long id) {
deletedDiaryRepository.deleteByMemberId(id);
diaryRepository.deleteByMemberId(id);
trainingTimeRepository.deleteByMemberId(id);
visitRepository.deleteByMemberId(id);
memberRepository.deleteById(id);
}

Expand Down

0 comments on commit fe6eccb

Please sign in to comment.