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 8e09d4e
Showing 4 changed files with 51 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
@@ -2,7 +2,7 @@ name: cd-dev-docker

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

jobs:
build:
24 changes: 11 additions & 13 deletions src/main/java/com/clova/anifriends/global/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
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.GenericToStringSerializer;
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 GenericToStringSerializer<>(Integer.class));
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
2 changes: 1 addition & 1 deletion src/main/resources/backend-config
38 changes: 38 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,38 @@
package com.clova.anifriends.global;

import java.util.Map;
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
public class RedisTestController {

private final RedisTemplate<String, Integer> redisTemplate;

public RedisTestController(RedisTemplate<String, Integer> redisTemplate) {
this.redisTemplate = redisTemplate;
}

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

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

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

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

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

}
}

0 comments on commit 8e09d4e

Please sign in to comment.