Skip to content

Commit

Permalink
Redis 설치, 세팅
Browse files Browse the repository at this point in the history
  • Loading branch information
chobeebee committed Jun 23, 2024
1 parent 4f32880 commit 506effc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
}

tasks.named('test') {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/sparta/binplay/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.sparta.binplay.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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableRedisRepositories
public class RedisConfig {
@Value("${spring.redis.host}")
private String redisHost;

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

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(redisHost, redisPort);
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

redisTemplate.setConnectionFactory(redisConnectionFactory());

redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

// Hash를 사용할 경우 Serializer
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());

return redisTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sparta.binplay.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/redis")
public class RedisTestController {

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@PostMapping("/set")
public String setKeyValue(@RequestParam String key, @RequestParam String value) {
redisTemplate.opsForValue().set(key, value);
return "Key-Value pair set successfully";
}

@GetMapping("/get")
public String getValue(@RequestParam String key) {
Object value = redisTemplate.opsForValue().get(key);
return value != null ? value.toString() : "Key not found";
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/sparta/binplay/controller/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sparta.binplay.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test {

@RequestMapping("/sample")
public String greeting() {
return "sample!!";
}
}

0 comments on commit 506effc

Please sign in to comment.