-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…m-caching Refactor/#133 trending program caching
- Loading branch information
Showing
4 changed files
with
168 additions
and
81 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
38 changes: 38 additions & 0 deletions
38
src/main/java/tavebalak/OTTify/common/config/CacheConfig.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 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
19
src/main/java/tavebalak/OTTify/common/constant/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,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; | ||
} |
Oops, something went wrong.