-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup Cache & Apply Cache to Cluster, Table (#27)
* Setup Cache * Add Cache to Retrieve Cluster & Table
- Loading branch information
Showing
9 changed files
with
236 additions
and
61 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: 43 additions & 0 deletions
43
cadio-core/src/main/java/kr/hakdang/cadio/core/support/cache/CacheType.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,43 @@ | ||
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 { | ||
|
||
// TODO: 만료시간은 5분으로 가져가돼, N분마다 api를 쏴서 클러스터에 대한 정보를 갱신하는 것도 좋을듯 | ||
CLUSTER_LIST(CacheTypeNames.CLUSTER_LIST, Duration.ofMinutes(5)), | ||
CLUSTER(CacheTypeNames.CLUSTER, Duration.ofMinutes(5)), | ||
TABLE_LIST(CacheTypeNames.TABLE_LIST, Duration.ofMinutes(5)), | ||
TABLE(CacheTypeNames.TABLE, Duration.ofMinutes(5)), | ||
; | ||
|
||
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_LIST = "clusterList"; | ||
public static final String CLUSTER = "cluster"; | ||
public static final String TABLE_LIST = "tableList"; | ||
public static final String TABLE = "table"; | ||
|
||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
cadio-core/src/main/java/kr/hakdang/cadio/core/support/cache/LocalCacheConfig.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,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; | ||
} | ||
|
||
} |
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
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 |
---|---|---|
|
@@ -60,5 +60,4 @@ public ApiResponse<ClusterNode> nodeDetail( | |
} | ||
} | ||
|
||
|
||
} |
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
58 changes: 58 additions & 0 deletions
58
...-web/src/main/java/kr/hakdang/cadio/web/route/cluster/keyspace/ClusterKeyspaceReader.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,58 @@ | ||
package kr.hakdang.cadio.web.route.cluster.keyspace; | ||
|
||
import com.datastax.oss.driver.api.core.CqlSession; | ||
import kr.hakdang.cadio.core.domain.cluster.TempClusterConnector; | ||
import kr.hakdang.cadio.core.domain.cluster.keyspace.ClusterKeyspaceCommander; | ||
import kr.hakdang.cadio.core.domain.cluster.keyspace.ClusterKeyspaceDescribeArgs; | ||
import kr.hakdang.cadio.core.domain.cluster.keyspace.ClusterKeyspaceListResult; | ||
import kr.hakdang.cadio.core.support.cache.CacheType; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cache.annotation.CacheEvict; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* ClusterKeyspaceReader | ||
* | ||
* @author seungh0 | ||
* @since 2024-07-04 | ||
*/ | ||
@Slf4j | ||
@Service | ||
public class ClusterKeyspaceReader { | ||
|
||
private final TempClusterConnector tempClusterConnector; | ||
private final ClusterKeyspaceCommander clusterKeyspaceCommander; | ||
|
||
public ClusterKeyspaceReader( | ||
TempClusterConnector tempClusterConnector, | ||
ClusterKeyspaceCommander clusterKeyspaceCommander | ||
) { | ||
this.tempClusterConnector = tempClusterConnector; | ||
this.clusterKeyspaceCommander = clusterKeyspaceCommander; | ||
} | ||
|
||
@Cacheable(value = CacheType.CacheTypeNames.CLUSTER_LIST) | ||
public ClusterKeyspaceListResult listKeyspace(String clusterId) { | ||
try (CqlSession session = tempClusterConnector.makeSession(clusterId)) { | ||
return clusterKeyspaceCommander.keyspaceList(session); | ||
} | ||
} | ||
|
||
@CacheEvict(value = CacheType.CacheTypeNames.CLUSTER_LIST) | ||
public void refreshKeyspaceCache(String clusterId) { | ||
log.info("ClusterList ({}) is evicted", clusterId); | ||
} | ||
|
||
@Cacheable(value = CacheType.CacheTypeNames.CLUSTER_LIST) | ||
public String getKeyspace(String clusterId, String keyspace) { | ||
try (CqlSession session = tempClusterConnector.makeSession(clusterId)) { | ||
return clusterKeyspaceCommander.describe(session, ClusterKeyspaceDescribeArgs.builder() | ||
.keyspace(keyspace) | ||
.withChildren(false) | ||
.pretty(true) | ||
.build()); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.