Skip to content

Commit

Permalink
add: added visit log for db
Browse files Browse the repository at this point in the history
  • Loading branch information
thguss committed Oct 17, 2024
1 parent 3616c1e commit 4a3682e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class MemberService implements MemberUseCase {
private final PlanPort planPort;
private final HookLogger hookLogger;
private final CachePort cachePort;
private final VisitPort visitPort;

@Transactional
public UpdateMemberResponse updateMember(long memberId, UpdateMemberRequest request) {
Expand Down Expand Up @@ -90,16 +91,18 @@ public RetrievePerformanceResponse retrieveMemberPerformance(long memberId) {
badgePort.countByMember(memberId));
}

//TODO: redis 죽으면?
@Transactional
public void visit(long memberId) {
Member foundMember = memberPort.findById(memberId);
String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String key = "visit:" + today;
LocalDate today = LocalDate.now();
String key = "visit:" + today.format(DateTimeFormatter.ofPattern("yyyyMMdd"));

if (!cachePort.getBit(key, foundMember.getId())) {
foundMember.visit();
memberPort.update(foundMember);
cachePort.setBit(key, foundMember.getId(), true);
visitPort.update(today, cachePort.getBitmap(key));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public interface CachePort {
void setBit(String key, long offset, boolean value);
boolean getBit(String key, long offset);
String getBitmap(String key);

Optional<Integer> getInt(String key);
void setInt(String key, int value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.smeem.application.port.output.persistence;

import java.time.LocalDate;

public interface VisitPort {
void update(LocalDate date, String bitmap);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public boolean getBit(String key, long offset) {
return Boolean.TRUE.equals(redisTemplate.opsForValue().getBit(key, offset));
}

@Override
public String getBitmap(String key) {
return redisTemplate.opsForValue().get(key);
}

@Override
public Optional<Integer> getInt(String key) {
return Optional.ofNullable(integerRedisTemplate.opsForValue().get(key));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.smeem.persistence.postgresql.adapter;

import com.smeem.application.port.output.persistence.VisitPort;
import com.smeem.persistence.postgresql.persistence.entity.VisitEntity;
import com.smeem.persistence.postgresql.persistence.repository.member.VisitRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
@RequiredArgsConstructor
public class VisitAdapter implements VisitPort {
private final VisitRepository visitRepository;

@Override
public void update(LocalDate date, String bitmap) {
VisitEntity visit = visitRepository.findByDate(date)
.orElseGet(() -> visitRepository.save(new VisitEntity(date, bitmap)));
visit.update(bitmap);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.smeem.persistence.postgresql.persistence.entity;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class VisitEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, unique = true)
private LocalDate date;

@Column(columnDefinition = "TEXT", nullable = false)
private String log;

public VisitEntity(LocalDate date, String log) {
this.date = date;
this.log = log;
}

public void update(String log) {
this.log = log;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.smeem.persistence.postgresql.persistence.repository.member;

import com.smeem.persistence.postgresql.persistence.entity.VisitEntity;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDate;
import java.util.Optional;

public interface VisitRepository extends JpaRepository<VisitEntity, Long> {
Optional<VisitEntity> findByDate(LocalDate date);
}

0 comments on commit 4a3682e

Please sign in to comment.