38 lines
1.3 KiB
Java
38 lines
1.3 KiB
Java
package com.backend.hls.proxy.config;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import org.springframework.cache.CacheManager;
|
|
import org.springframework.cache.annotation.EnableCaching;
|
|
import org.springframework.cache.caffeine.CaffeineCacheManager;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import com.backend.hls.proxy.model.SimpleResponse;
|
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
|
|
|
@Configuration
|
|
@EnableCaching
|
|
public class CacheConfig {
|
|
|
|
@Bean
|
|
public CacheManager cacheManager() {
|
|
CaffeineCacheManager cacheManager = new CaffeineCacheManager("hlsPlaylistContent", "playlistSegmentContent");
|
|
cacheManager.setCaffeine(Caffeine.newBuilder()
|
|
.expireAfterAccess(1, TimeUnit.HOURS)
|
|
.weigher((Object key, Object value) -> {
|
|
if (value instanceof byte[] valueBytes) {
|
|
return valueBytes.length;
|
|
}
|
|
if (value instanceof SimpleResponse<?> response && response.getBody() instanceof byte[] body) {
|
|
return body.length;
|
|
}
|
|
return 0;
|
|
})
|
|
.maximumWeight(500 * 1024 * 1024)
|
|
.recordStats());
|
|
return cacheManager;
|
|
}
|
|
|
|
}
|