diff --git a/backend/build.gradle b/backend/build.gradle index 5a9b4a0fe..c5a1b2bb3 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -41,6 +41,9 @@ dependencies { implementation 'org.flywaydb:flyway-core:9.22.3' implementation 'org.flywaydb:flyway-mysql' + // Redis + implementation 'org.springframework.boot:spring-boot-starter-data-redis' + // Lombok compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' diff --git a/backend/docker-compose.local.yml b/backend/docker-compose.local.yml new file mode 100644 index 000000000..533541953 --- /dev/null +++ b/backend/docker-compose.local.yml @@ -0,0 +1,14 @@ +version: '3.8' + +services: + redis: + image: redis:latest + container_name: redis-local + ports: + - "6379:6379" + networks: + - redis_network + +networks: + redis_network: + driver: bridge diff --git a/backend/src/main/java/com/cruru/RedisTestController.java b/backend/src/main/java/com/cruru/RedisTestController.java new file mode 100644 index 000000000..664e2d774 --- /dev/null +++ b/backend/src/main/java/com/cruru/RedisTestController.java @@ -0,0 +1,38 @@ +package com.cruru; + +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.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/redis-test") +public class RedisTestController { + + private final RedisTemplate redisTemplate; + + public RedisTestController(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + // curl -X POST "http://localhost:8080/redis-test?key=testKey&value=testValue" + @PostMapping + public ResponseEntity setKey(@RequestParam String key, @RequestParam String value) { + redisTemplate.opsForValue().set(key, value); + return ResponseEntity.ok("key-value: " + key + " = " + value); + } + + // http://localhost:8080/redis-test?key=testKey + @GetMapping + public ResponseEntity getKey(@RequestParam String key) { + String value = redisTemplate.opsForValue().get(key); + if (value == null) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("key " + key + " not found"); + } + return ResponseEntity.ok("value for key " + key + " is: " + value); + } +} diff --git a/backend/src/main/java/com/cruru/config/RedisConfig.java b/backend/src/main/java/com/cruru/config/RedisConfig.java new file mode 100644 index 000000000..6a1dbf0dd --- /dev/null +++ b/backend/src/main/java/com/cruru/config/RedisConfig.java @@ -0,0 +1,36 @@ +package com.cruru.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +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; + +@Profile({"local", "test"}) +@Configuration +public class RedisConfig { + + @Value("${spring.data.redis.host}") + private String host; + + @Value("${spring.data.redis.port}") + private int port; + + @Bean + public RedisConnectionFactory redisConnectionFactory() { + return new LettuceConnectionFactory(host, port); + } + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(connectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new StringRedisSerializer()); + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } +} diff --git a/backend/src/main/resources/application.yml b/backend/src/main/resources/application.yml index 6752c8733..7e95fd73a 100644 --- a/backend/src/main/resources/application.yml +++ b/backend/src/main/resources/application.yml @@ -45,6 +45,10 @@ spring: file-size-threshold: 2KB max-file-size: 25MB max-request-size: 50MB + data: + redis: + port: 6379 + host: localhost security: jwt: diff --git a/backend/src/test/java/com/cruru/RedisTest.java b/backend/src/test/java/com/cruru/RedisTest.java new file mode 100644 index 000000000..038f9aa44 --- /dev/null +++ b/backend/src/test/java/com/cruru/RedisTest.java @@ -0,0 +1,56 @@ +package com.cruru; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.test.context.ActiveProfiles; + +@ActiveProfiles("test") +@SpringBootTest +class RedisTest { + + @Autowired + private RedisTemplate redisTemplate; + + @Test + public void shouldConnectToRedis() { + Boolean isAvailable = redisTemplate.getConnectionFactory() + .getConnection() + .ping() + .equalsIgnoreCase("PONG"); + assertThat(isAvailable).isTrue(); + } + + @Test + public void setAndGet() { + // given + String key = "testKey"; + String value = "testValue"; + + // when + redisTemplate.opsForValue().set(key, value); + String actual = redisTemplate.opsForValue().get(key); + + // then + assertThat(actual).isEqualTo(value); + redisTemplate.delete("testKey"); + } + + @Test + public void delete() { + // given + String key = "testDeleteKey"; + String value = "testValue"; + redisTemplate.opsForValue().set(key, value); + + // when + redisTemplate.delete(key); + String actual = redisTemplate.opsForValue().get(key); + + // then + assertThat(actual).isNull(); + } +} diff --git a/backend/src/test/resources/application.yml b/backend/src/test/resources/application.yml index 149549123..ec6de16fd 100644 --- a/backend/src/test/resources/application.yml +++ b/backend/src/test/resources/application.yml @@ -25,6 +25,10 @@ spring: open-in-view: false mail: host: smtp.gmail.com + data: + redis: + port: 6379 + host: localhost dataloader: enable: false