Skip to content

Commit

Permalink
Setup Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
seungh0 committed Jul 4, 2024
1 parent 7118f71 commit 9e06f12
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cadio-core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
dependencies {
// Spring Boot
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-json")

//TODO ClusterFactory 만들어지면 변경
api("com.datastax.oss:java-driver-core:${datastaxJavaDriverVersion}")
Expand All @@ -11,12 +10,18 @@ dependencies {
// implementation("com.datastax.oss:java-driver-query-builder:${datastaxJavaDriverVersion}")
// implementation("com.datastax.oss:java-driver-mapper-runtime:${datastaxJavaDriverVersion}")

api("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.1")
// Json
implementation("org.springframework.boot:spring-boot-starter-json")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.1")

// CommonsLang3
api("org.apache.commons:commons-lang3:3.13.0")
api("com.google.guava:guava:33.0.0-jre")
api("org.apache.commons:commons-collections4:4.4")

// Cache
implementation("org.springframework.boot:spring-boot-starter-cache")
implementation("com.github.ben-manes.caffeine:caffeine:3.1.8")
}

bootJar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package kr.hakdang.cadio.core.support.cache;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.Duration;

/**
* CacheType
*
* @author seungh0
* @since 2024-07-04
*/
@Getter
public enum CacheType {

CLUSTER(CacheTypeNames.CLUSTER, Duration.ofMinutes(1)),
;

private final String key;
private final Duration duration;

CacheType(String key, Duration duration) {
this.key = key;
this.duration = duration;
}

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class CacheTypeNames {

public static final String CLUSTER = "cluster";

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kr.hakdang.cadio.core.support.cache;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* LocalCacheConfig
*
* @author seungh0
* @since 2024-07-04
*/
@EnableCaching
@Configuration
public class LocalCacheConfig {

@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<CaffeineCache> caches = Arrays.stream(CacheType.values())
.map(cache -> new CaffeineCache(cache.getKey(), Caffeine.newBuilder()
.expireAfterWrite(cache.getDuration())
.build()
))
.collect(Collectors.toList());
cacheManager.setCaches(caches);
return cacheManager;
}

}

0 comments on commit 9e06f12

Please sign in to comment.