Skip to content

Commit

Permalink
feat: store recently viewed product using list
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang1221 committed Sep 13, 2024
1 parent 0382749 commit a597225
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Slf4j
@Repository
public class RedisRepositoryImpl implements RedisRepository {

private static final String DEVICE_PREFIX = "device:";
private static final long MAX_RECENT_PRODUCTS = 12;
private static final long EXPIRATION_TIME = 10 * 24 * 60 * 60; // 10일

private final RedisTemplate<String, String> redisTemplate;

Expand All @@ -20,17 +24,22 @@ public RedisRepositoryImpl(RedisTemplate<String, String> redisTemplate) {
}

@Override
public void saveDeviceToken(String deviceToken, String productId) {
redisTemplate.opsForSet().add(DEVICE_PREFIX + deviceToken, productId);
public void saveRecentProduct(String deviceToken, String productId) {
String key = DEVICE_PREFIX + deviceToken;
redisTemplate.opsForList().leftPush(key, productId);
redisTemplate.opsForList().trim(key, 0, MAX_RECENT_PRODUCTS - 1); // 리스트 길이 제한
redisTemplate.expire(key, EXPIRATION_TIME, TimeUnit.SECONDS);

}

@Override
public Set<String> getRecentProducts(String deviceToken) {
return redisTemplate.opsForSet().members(DEVICE_PREFIX + deviceToken);
public List<String> getRecentProducts(String deviceToken) {
String key = DEVICE_PREFIX + deviceToken;
return redisTemplate.opsForList().range(key, 0, -1);
}

@Override
public void deleteDeviceToken(String deviceToken) {
public void deleteRecentProduct(String deviceToken) {
redisTemplate.delete(DEVICE_PREFIX + deviceToken);
}
}

0 comments on commit a597225

Please sign in to comment.