-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
infra/infra-redis/src/main/java/com/drinkhere/infraredis/config/JsonComponent.java
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
infra/infra-redis/src/main/java/com/drinkhere/infraredis/config/RedisCacheConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
17 changes: 5 additions & 12 deletions
17
infra/infra-redis/src/main/java/com/drinkhere/infraredis/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 29 additions & 16 deletions
45
infra/infra-redis/src/main/java/com/drinkhere/infraredis/util/RedisUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |