-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from fr35wo/feature/board
feat: Redis 연동 & 조회수 중복 증가 방지 기능 구현
- Loading branch information
Showing
5 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/example/copro/global/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.example.copro.global.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@EnableRedisRepositories | ||
public class RedisConfig { | ||
@Value("${spring.data.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int port; | ||
|
||
@Value("${spring.profiles.active}") | ||
private String namespace; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<String, String> redisTemplate() { | ||
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return redisTemplate; | ||
} | ||
|
||
public String getNamespace() { | ||
return namespace; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/example/copro/global/redis/application/RedisService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.example.copro.global.redis.application; | ||
|
||
import com.example.copro.global.config.RedisConfig; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
public class RedisService { | ||
private final RedisTemplate<String, String> redisTemplate; | ||
private final RedisConfig redisConfig; | ||
|
||
public RedisService(RedisTemplate<String, String> redisTemplate, RedisConfig redisConfig) { | ||
this.redisTemplate = redisTemplate; | ||
this.redisConfig = redisConfig; | ||
} | ||
|
||
public boolean checkAndAddViewByMember(Long memberId, Long boardId) { | ||
String namespace = redisConfig.getNamespace(); | ||
String redisKey = namespace + ":boardView:" + boardId; | ||
String redisMemberKey = namespace + ":memberView:" + memberId; | ||
|
||
List<String> memberViewList = getValuesList(redisMemberKey); | ||
|
||
if (!memberViewList.contains(redisKey)) { | ||
setValuesListWithExpire(redisMemberKey, redisKey, Duration.ofHours(24)); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public void setValuesListWithExpire(String key, String data, Duration duration) { | ||
redisTemplate.opsForList().rightPush(key, data); | ||
redisTemplate.expire(key, duration); | ||
} | ||
|
||
public List<String> getValuesList(String key) { | ||
Long len = redisTemplate.opsForList().size(key); | ||
return len == 0 ? new ArrayList<>() : redisTemplate.opsForList().range(key, 0, len-1); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters