Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JuseungL committed Nov 26, 2024
1 parent 526db7d commit 184d560
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public String healthCheck() {
@GetMapping("/redis")
public String redisConnectionCheck() {
// Redis에서 'id1' 키로 값을 가져옵니다.
String value = redisUtil.getObjectByKey("id1", String.class);

// String value = redisUtil.getObjectByKey("id1", String.class);
String value = (String) redisUtil.get("id1");
System.out.println(value);
// Redis에서 가져온 값이 있을 경우 반환, 없으면 기본 메시지 반환
if (value != null) {
return value;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.drinkhere.infraredis.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@EnableCaching
@Configuration
public class RedisCacheConfig {
@Bean
public CacheManager oidcCacheManager(RedisConnectionFactory cf) {
RedisCacheConfiguration redisCacheConfiguration =
RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new StringRedisSerializer()))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new GenericJackson2JsonRedisSerializer()))
// TTL 하루로 설정
.entryTtl(Duration.ofDays(1L));

return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(cf)
.cacheDefaults(redisCacheConfiguration)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
package com.drinkhere.infraredis.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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
@EnableCaching
public class RedisConfig {

@Value("${spring.redis.host}")
private String host;
@Value("${spring.data.redis.host}")
private String redisHost;

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

@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
return new LettuceConnectionFactory(redisStandaloneConfiguration);
return new LettuceConnectionFactory(redisHost, redisPort);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
package com.drinkhere.infraredis.util;

import com.drinkhere.infraredis.config.JsonComponent;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
@Component
public class RedisUtil {
private final RedisTemplate<String, Object> redisTemplate;

final RedisTemplate<String, String> redisTemplate;
final JsonComponent jsonComponent;
public void saveAsValue(String key, Object val, Long time, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, val, time, timeUnit);
}

public <T> T getObjectByKey(String key, Class<T> clazz) {
ValueOperations<String, String> vop = redisTemplate.opsForValue();
public void appendToRecentlyViewedAnnouncement(String key, String newValue) {
long RECENT_VIEWED_ANNOUNCEMENT_LIMIT = 20;

String jsonString = vop.get(key);
if (StringUtils.isNotEmpty(jsonString)) {
return jsonComponent.jsonToObject(jsonString, clazz);
} else {
return null;
Object mostRecentlyViewedValue = redisTemplate.opsForList().index(key, 0);
if (Objects.equals(mostRecentlyViewedValue, newValue)) {
return;
}
if (Objects.equals(redisTemplate.opsForList().size(key), RECENT_VIEWED_ANNOUNCEMENT_LIMIT)) {
redisTemplate.opsForList().rightPop(key);
}
redisTemplate.opsForList().leftPush(key, newValue);
}

public boolean hasKey(String key) {
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
}

public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}

public List<Object> getList(String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}

public void setObjectByKey(String key, Object obj) {
ValueOperations<String, String> vop = redisTemplate.opsForValue();
vop.set(key, jsonComponent.objectToJson(obj));
redisTemplate.expire(key, 7, TimeUnit.DAYS);
public boolean delete(String key) {
return Boolean.TRUE.equals(redisTemplate.delete(key));
}
}

0 comments on commit 184d560

Please sign in to comment.