728x90
아래 내용은 https://github.com/beaver84/setting-test 에서 실제 소스를 확인할 수 있습니다.
- CacheRepository - RedisRepository의 인터페이스(객체 지향의 DIP를 지키기 위한)
import java.time.Duration;
import java.util.Map;
public interface CacheRepository {
String getValue(String key);
void setValue(String key, String value, int minutes);
void setValue(String key, String value);
void setValue(String key, String value, Duration duration);
void setValue(byte[] key, byte[] value);
void setValue(byte[] key, byte[] value, Duration duration);
String getValue(byte[] key);
void remove(byte[] key);
void remove(String key);
Long setValueHashes(String key, String subKey, String value);
Long setValueHashes(String key, String subKey, String value, Duration duration);
String getValueHashes(String key, String subKey);
Map<String, String> getAllValueHashes(String key);
Long removeHashes(String key, String subKey);
Long countHashes(String key);
}
- RedisRepository - RedisTemplate를 주입받아서 필요한 메서드만 구현
import com.example.settingtest.repository.cache.CacheRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
@Repository
public class RedisRepository implements CacheRepository {
private RedisTemplate redisTemplate;
@Autowired
public RedisRepository(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public String getValue(String key) {
Object data = redisTemplate.opsForValue().get(key);
if (Objects.nonNull(data)) {
return data.toString();
} else {
return null;
}
}
@Override
public void setValue(String key, String value, int minutes) {
redisTemplate.opsForValue().set(key, value, Duration.ofMinutes(minutes));
}
@Override
public void setValue(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
@Override
public void setValue(String key, String value, Duration duration) {
redisTemplate.opsForValue().set(key, value, duration);
}
@Override
public void setValue(byte[] key, byte[] value) {
setValue(new String(key), new String(value));
}
@Override
public void setValue(byte[] key, byte[] value, Duration duration) {
setValue(new String(key), new String(value), duration);
}
@Override
public String getValue(byte[] key) {
return getValue(new String(key));
}
@Override
public void remove(byte[] key) {
remove(new String(key));
}
@Override
public void remove(String key) {
redisTemplate.delete(key);
}
@Override
public Long setValueHashes(String key, String subKey, String value) {
redisTemplate.opsForHash().put(key, subKey, value);
return 1L;
}
@Override
public Long setValueHashes(String key, String subKey, String value, Duration duration) {
redisTemplate.opsForHash().put(key, subKey, value);
redisTemplate.expire(key, duration);
return 1L;
}
@Override
public String getValueHashes(String key, String subKey) {
Object hashValue = redisTemplate.opsForHash().get(key, subKey);
if(Objects.nonNull(hashValue)) {
return hashValue.toString();
} else {
return null;
}
}
@Override
public Map<String, String> getAllValueHashes(String key) {
Map<String, String> result = redisTemplate.opsForHash().entries(key);
return result;
}
@Override
public Long removeHashes(String key, String subKey) {
redisTemplate.opsForHash().delete(key, subKey);
return 1L;
}
@Override
public Long countHashes(String key) {
return getAllValueHashes(key).keySet().stream().count();
}
}
728x90
- test 실행
//redis test
private final CacheRepository cacheRepository;
cacheRepository.setValue("User1", "경태", LOGIN_SESSION_EXTEND_MINUTES);
cacheRepository.setValue("User2", "민철", LOGIN_SESSION_EXTEND_MINUTES);
cacheRepository.setValue("User3", "종민", LOGIN_SESSION_EXTEND_MINUTES);
728x90
'Spring > 초기 setting' 카테고리의 다른 글
11) Logback 설정 (0) | 2023.04.20 |
---|---|
9) 비밀번호 암/복호화(Encrypt/Decrypt) 설정 (0) | 2023.04.20 |
8) API Response(@ControllerAdvice) 설정 (0) | 2023.04.20 |
mapStruct 소개 및 활용 (0) | 2023.04.10 |
6) 스프링 시큐리티(spring-security) 적용 (0) | 2023.04.05 |