Skip to content

Commit

Permalink
feat: redis 동작 확인을 위한 테스트 api를 추가한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjo6300 committed Nov 22, 2023
1 parent 5f676f3 commit a2f1ed3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: cd-dev-docker

on:
push:
branches: [ "dev" ]
branches: [ "dev", "feat/#298" ]

jobs:
build:
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/com/clova/anifriends/global/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
package com.clova.anifriends.global.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
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.serializer.StringRedisSerializer;

@EnableCaching
@Configuration
public class RedisConfig {

@Value("${spring.redis.host}")
@Value("${spring.data.redis.host}")
private String host;

@Value("${spring.redis.port}")
@Value("${spring.data.redis.port}")
private int port;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}

// @Bean
// public RedisTemplate<String, Integer> redisTemplate() {
// RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
// redisTemplate.setKeySerializer(new StringRedisSerializer());
// redisTemplate.setValueSerializer(new StringRedisSerializer());
// redisTemplate.setConnectionFactory(redisConnectionFactory());
// return redisTemplate;
// }

@Bean
public RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
public RedisTemplate<String, Integer> redisTemplate() {
RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
}



2 changes: 1 addition & 1 deletion src/main/resources/backend-config
36 changes: 36 additions & 0 deletions src/test/java/com/clova/anifriends/global/RedisTestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.clova.anifriends.global;

import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class RedisTestController {

private final RedisTemplate<String, String> redisTemplate;

@PostMapping("/data")
public ResponseEntity<String> setRedisData(
@RequestBody(required = true) Map<String, String> map) throws Exception {

redisTemplate.opsForValue().set(map.get("key"), map.get("value"));

return new ResponseEntity<>("정상 등록", HttpStatus.CREATED);
}

@GetMapping("/data")
public ResponseEntity<String> getRedisData(
@RequestParam(required = true) String key) {

return new ResponseEntity<>(redisTemplate.opsForValue().get(key), HttpStatus.OK);

}
}

0 comments on commit a2f1ed3

Please sign in to comment.