More control of CaffeineCacheManager, PreprocessService as interface

This commit is contained in:
2025-12-24 23:17:21 +05:00
parent a89b00d2b5
commit 0230cae852
6 changed files with 120 additions and 66 deletions

View File

@ -1,18 +1,37 @@
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() {
return new CaffeineCacheManager("hlsPlaylistContent", "playlistSegmentContent");
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;
}
}