#26 pharmacy: add redis

This commit is contained in:
haerong22
2023-01-05 07:35:38 +09:00
parent a04491b148
commit 246c364ea5
3 changed files with 97 additions and 0 deletions

View File

@@ -28,6 +28,9 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.retry:spring-retry'
// redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'io.seruco.encoding:base62:0.1.3'
// handlebars

View File

@@ -0,0 +1,34 @@
package com.example.road.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.serializer.StringRedisSerializer;
@Configuration
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.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}

View File

@@ -0,0 +1,60 @@
package com.example.road.pharmacy.cache
import com.example.road.AbstractIntegrationContainerBaseTest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
class RedisTemplateTest extends AbstractIntegrationContainerBaseTest {
@Autowired
private RedisTemplate redisTemplate;
def "RedisTemplate String operations"() {
given:
def valueOps = redisTemplate.opsForValue()
def key = "key"
def value = "hello"
when:
valueOps.set(key, value)
then:
def result = valueOps.get(key)
result == value
}
def "RedisTemplate set operations"() {
given:
def setOps = redisTemplate.opsForSet()
def key = "setKey"
when:
setOps.add(key, "h", "e", "l", "l", "o")
then:
def size = setOps.size(key)
size == 4
}
def "RedisTemplate hash operations"() {
given:
def hashOps = redisTemplate.opsForHash()
def key = "hashKey"
def subKey = "subKey"
def value = "value"
when:
hashOps.put(key, subKey, value)
then:
def result = hashOps.get(key, subKey)
result == value
def entries = hashOps.entries(key)
entries.keySet().contains(subKey)
entries.values().contains(value)
def size = hashOps.size(key)
size == entries.size()
}
}