Skip to content

Commit

Permalink
Merge pull request #134 from dionisos198/refactor/#133-trendingProgra…
Browse files Browse the repository at this point in the history
…m-caching

Refactor/#133 trending program caching
  • Loading branch information
dionisos198 authored Feb 6, 2024
2 parents f372c1e + 653accf commit d1fbf18
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 81 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ dependencies {
implementation 'com.querydsl:querydsl-jpa'
implementation 'com.querydsl:querydsl-apt'

//cache
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'


}

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/tavebalak/OTTify/common/config/CacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tavebalak.OTTify.common.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
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 tavebalak.OTTify.common.constant.CacheType;

@Configuration
@EnableCaching
public class CacheConfig {

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


}
19 changes: 19 additions & 0 deletions src/main/java/tavebalak/OTTify/common/constant/CacheType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tavebalak.OTTify.common.constant;

import lombok.Getter;

@Getter
public enum CacheType {

PROGRAM_TRENDING("programTrending", 60 * 60, 100);

CacheType(String cacheName, int expiredAfterWrite, int maximumSize) {
this.cacheName = cacheName;
this.expiredAfterWrite = expiredAfterWrite;
this.maximumSize = maximumSize;
}

private String cacheName;
private int expiredAfterWrite;
private int maximumSize;
}
Loading

0 comments on commit d1fbf18

Please sign in to comment.