Skip to content

Commit 506effc

Browse files
committed
Redis 설치, 세팅
1 parent 4f32880 commit 506effc

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dependencies {
3838
testImplementation 'org.springframework.boot:spring-boot-starter-test'
3939
testImplementation 'org.springframework.security:spring-security-test'
4040
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
41+
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
4142
}
4243

4344
tasks.named('test') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.sparta.binplay.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.redis.connection.RedisConnectionFactory;
7+
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
8+
import org.springframework.data.redis.core.RedisTemplate;
9+
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
10+
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
11+
import org.springframework.data.redis.serializer.StringRedisSerializer;
12+
13+
@Configuration
14+
@EnableRedisRepositories
15+
public class RedisConfig {
16+
@Value("${spring.redis.host}")
17+
private String redisHost;
18+
19+
@Value("${spring.redis.port}")
20+
private int redisPort;
21+
22+
@Bean
23+
public RedisConnectionFactory redisConnectionFactory() {
24+
return new LettuceConnectionFactory(redisHost, redisPort);
25+
}
26+
27+
@Bean
28+
public RedisTemplate<String, Object> redisTemplate() {
29+
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
30+
31+
redisTemplate.setConnectionFactory(redisConnectionFactory());
32+
33+
redisTemplate.setKeySerializer(new StringRedisSerializer());
34+
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
35+
36+
// Hash를 사용할 경우 Serializer
37+
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
38+
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
39+
40+
return redisTemplate;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.sparta.binplay.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.redis.core.RedisTemplate;
5+
import org.springframework.web.bind.annotation.*;
6+
7+
@RestController
8+
@RequestMapping("/redis")
9+
public class RedisTestController {
10+
11+
@Autowired
12+
private RedisTemplate<String, Object> redisTemplate;
13+
14+
@PostMapping("/set")
15+
public String setKeyValue(@RequestParam String key, @RequestParam String value) {
16+
redisTemplate.opsForValue().set(key, value);
17+
return "Key-Value pair set successfully";
18+
}
19+
20+
@GetMapping("/get")
21+
public String getValue(@RequestParam String key) {
22+
Object value = redisTemplate.opsForValue().get(key);
23+
return value != null ? value.toString() : "Key not found";
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.sparta.binplay.controller;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class Test {
8+
9+
@RequestMapping("/sample")
10+
public String greeting() {
11+
return "sample!!";
12+
}
13+
}

0 commit comments

Comments
 (0)