chore(backend): move avatar cache to cache manager

This commit is contained in:
MiniDigger | Martin 2023-01-08 17:58:23 +01:00
parent d45a16339f
commit cd8a1a3220
2 changed files with 28 additions and 5 deletions

View File

@ -2,11 +2,15 @@ package io.papermc.hangar.config;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.time.Duration;
import io.micrometer.core.instrument.Tag;
import jakarta.annotation.PostConstruct;
import org.springframework.boot.actuate.metrics.cache.CacheMetricsRegistrar;
import org.springframework.cache.Cache;
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 org.springframework.context.annotation.Lazy;
@Configuration
@EnableCaching
@ -34,10 +38,13 @@ public class CacheConfig {
public static final String INDEX_SITEMAP = "indexSitemap-cache";
public static final String GLOBAL_SITEMAP = "globalSitemap-cache";
public static final String USER_SITEMAP = "userSitemap-cache";
public static final String AVATARS = "avatars-cache";
private final CacheMetricsRegistrar cacheMetricsRegistrar;
private final CaffeineCacheManager cacheManager;
public CacheConfig() {
public CacheConfig(@Lazy final CacheMetricsRegistrar cacheMetricsRegistrar) {
this.cacheMetricsRegistrar = cacheMetricsRegistrar;
this.cacheManager = new CaffeineCacheManager();
}
@ -151,11 +158,25 @@ public class CacheConfig {
return this.createCache(USER_SITEMAP, Duration.ofHours(1), 20);
}
@Bean(AVATARS)
Cache avatarsCache() {
return this.createCache(AVATARS, Duration.ofMinutes(30), 200);
}
@PostConstruct
public void init() {
for (final String cacheName : this.cacheManager.getCacheNames()) {
final Cache cache = this.cacheManager.getCache(cacheName);
this.cacheMetricsRegistrar.bindCacheToRegistry(cache);
}
}
private Cache createCache(final String name, final Duration ttl, final long maxSize) {
final var caffineCache = Caffeine.newBuilder()
.expireAfterWrite(ttl)
.expireAfterAccess(ttl)
.maximumSize(maxSize)
.recordStats()
.build();
this.cacheManager.registerCustomCache(name, caffineCache);
return this.cacheManager.getCache(name);

View File

@ -1,8 +1,8 @@
package io.papermc.hangar.service.internal;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.papermc.hangar.HangarComponent;
import io.papermc.hangar.config.CacheConfig;
import io.papermc.hangar.db.dao.internal.table.UserDAO;
import io.papermc.hangar.exceptions.HangarApiException;
import io.papermc.hangar.model.api.User;
@ -11,9 +11,9 @@ import io.papermc.hangar.model.internal.user.HangarUser;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpEntity;
@ -36,12 +36,14 @@ public class AvatarService extends HangarComponent {
private final RestTemplate restTemplate;
private final UserDAO userDAO;
private final Cache<String, String> cache = Caffeine.newBuilder().expireAfterAccess(Duration.ofMinutes(30)).build();
private final Cache<String, String> cache;
@Autowired
public AvatarService(@Lazy final RestTemplate restTemplate, final UserDAO userDAO) {
public AvatarService(@Lazy final RestTemplate restTemplate, final UserDAO userDAO, @Qualifier(CacheConfig.AVATARS) final org.springframework.cache.Cache avatarsCache) {
this.restTemplate = restTemplate;
this.userDAO = userDAO;
//noinspection unchecked
this.cache = (Cache<String, String>) avatarsCache.getNativeCache();
}
/*