Skip to content

Commit

Permalink
fix: properly evict latest release stuff, store for longer, make it m…
Browse files Browse the repository at this point in the history
…atch ignoring case
  • Loading branch information
MiniDigger committed Dec 19, 2024
1 parent 2ffbab9 commit 7b9f4e5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Cache versionDependenciesCache() {

@Bean(LATEST_VERSION)
Cache latestVersionCache() {
return this.createCache(LATEST_VERSION, Duration.ofMinutes(30), 2000);
return this.createCache(LATEST_VERSION, Duration.ofHours(6), 2000);
}

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void clearAuthorsCache() {
// Clears a cache
}

@Cacheable(CacheConfig.AUTHORS)
@Cacheable(value = CacheConfig.AUTHORS, keyGenerator = "ignoringCaseCacheKeyGenerator")
@Transactional(readOnly = true)
public PaginatedResult<User> getAuthors(final String query, final RequestPagination pagination) {
final boolean hasQuery = !StringUtils.isBlank(query);
Expand All @@ -132,7 +132,7 @@ public void clearStaffCache() {
// Clears a cache
}

@Cacheable(CacheConfig.STAFF)
@Cacheable(value = CacheConfig.STAFF, keyGenerator = "ignoringCaseCacheKeyGenerator")
@Transactional(readOnly = true)
public PaginatedResult<User> getStaff(final String query, final RequestPagination pagination) {
final boolean hasQuery = !StringUtils.isBlank(query);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.papermc.hangar.service.api;

import io.papermc.hangar.HangarComponent;
import io.papermc.hangar.config.CacheConfig;
import io.papermc.hangar.db.dao.v1.VersionsApiDAO;
import io.papermc.hangar.exceptions.HangarApiException;
import io.papermc.hangar.model.api.PaginatedResult;
Expand Down Expand Up @@ -30,6 +31,7 @@
import java.util.stream.Stream;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -193,12 +195,20 @@ public Map<String, VersionStats> getVersionStats(final String slug, final String
return this.versionsApiDAO.getVersionStats(slug, versionString, fromDate, toDate);
}

@Cacheable("latestVersion")
@CacheEvict(value = CacheConfig.LATEST_VERSION, keyGenerator = "ignoringCaseCacheKeyGenerator")
public void evictLatestRelease(final String slug) {
}

@Cacheable(value = CacheConfig.LATEST_VERSION, keyGenerator = "ignoringCaseCacheKeyGenerator")
public @Nullable String latestVersion(final String slug) {
return this.latestVersion(slug, this.config.channels.nameDefault());
}

@Cacheable("latestVersion")
@CacheEvict(value = CacheConfig.LATEST_VERSION, keyGenerator = "ignoringCaseCacheKeyGenerator")
public void evictLatest(final String slug, final String channel) {
}

@Cacheable(value = CacheConfig.LATEST_VERSION, keyGenerator = "ignoringCaseCacheKeyGenerator")
public @Nullable String latestVersion(final String slug, final String channel) {
final boolean canSeeHidden = this.getGlobalPermissions().has(Permission.SeeHidden);
final String version = this.versionsApiDAO.getLatestVersion(slug, channel, canSeeHidden, this.getHangarUserId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.papermc.hangar.components.webhook.model.event.ProjectPublishedEvent;
import io.papermc.hangar.components.webhook.model.event.VersionPublishedEvent;
import io.papermc.hangar.components.webhook.service.WebhookService;
import io.papermc.hangar.config.CacheConfig;
import io.papermc.hangar.controller.extras.pagination.filters.versions.VersionChannelFilter;
import io.papermc.hangar.controller.extras.pagination.filters.versions.VersionPlatformFilter;
import io.papermc.hangar.db.dao.internal.table.versions.ProjectVersionsDAO;
Expand Down Expand Up @@ -35,6 +36,7 @@
import io.papermc.hangar.model.internal.versions.PendingVersionFile;
import io.papermc.hangar.service.ValidationService;
import io.papermc.hangar.service.api.UsersApiService;
import io.papermc.hangar.service.api.VersionsApiService;
import io.papermc.hangar.service.internal.PlatformService;
import io.papermc.hangar.service.internal.file.FileService;
import io.papermc.hangar.service.internal.file.S3FileService;
Expand All @@ -45,6 +47,7 @@
import io.papermc.hangar.service.internal.versions.plugindata.PluginDataService;
import io.papermc.hangar.service.internal.versions.plugindata.PluginFileWithData;
import io.papermc.hangar.service.internal.visibility.ProjectVisibilityService;
import io.papermc.hangar.util.CacheWrapper;
import io.papermc.hangar.util.CryptoUtils;
import io.papermc.hangar.util.StringUtils;
import java.io.IOException;
Expand All @@ -65,6 +68,10 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.SimpleKey;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -92,9 +99,10 @@ public class VersionFactory extends HangarComponent {
private final ReviewService reviewService;
private final WebhookService webhookService;
private final AvatarService avatarService;
private final VersionsApiService versionApiService;

@Autowired
public VersionFactory(final ProjectVersionPlatformDependenciesDAO projectVersionPlatformDependencyDAO, final ProjectVersionDependenciesDAO projectVersionDependencyDAO, final ProjectVersionsDAO projectVersionDAO, final ProjectFiles projectFiles, final PluginDataService pluginDataService, final ChannelService channelService, final ProjectVisibilityService projectVisibilityService, final ProjectService projectService, final NotificationService notificationService, final PlatformService platformService, final UsersApiService usersApiService, final ValidationService validationService, final ProjectVersionDownloadsDAO downloadsDAO, final VersionsApiDAO versionsApiDAO, final FileService fileService, final JarScanningService jarScanningService, final ReviewService reviewService, final WebhookService webhookService, final AvatarService avatarService) {
public VersionFactory(final ProjectVersionPlatformDependenciesDAO projectVersionPlatformDependencyDAO, final ProjectVersionDependenciesDAO projectVersionDependencyDAO, final ProjectVersionsDAO projectVersionDAO, final ProjectFiles projectFiles, final PluginDataService pluginDataService, final ChannelService channelService, final ProjectVisibilityService projectVisibilityService, final ProjectService projectService, final NotificationService notificationService, final PlatformService platformService, final UsersApiService usersApiService, final ValidationService validationService, final ProjectVersionDownloadsDAO downloadsDAO, final VersionsApiDAO versionsApiDAO, final FileService fileService, final JarScanningService jarScanningService, final ReviewService reviewService, final WebhookService webhookService, final AvatarService avatarService, final @Lazy VersionsApiService versionApiService) {
this.projectVersionPlatformDependenciesDAO = projectVersionPlatformDependencyDAO;
this.projectVersionDependenciesDAO = projectVersionDependencyDAO;
this.projectVersionsDAO = projectVersionDAO;
Expand All @@ -114,6 +122,7 @@ public VersionFactory(final ProjectVersionPlatformDependenciesDAO projectVersion
this.reviewService = reviewService;
this.webhookService = webhookService;
this.avatarService = avatarService;
this.versionApiService = versionApiService;
}

@Transactional
Expand Down Expand Up @@ -335,6 +344,8 @@ public void publishPendingVersion(final long projectId, final PendingVersion pen
// cache purging
this.projectService.refreshHomeProjects();
this.usersApiService.clearAuthorsCache();
this.versionApiService.evictLatestRelease(projectTable.getSlug());
this.versionApiService.evictLatest(projectTable.getSlug(), projectChannelTable.getName());

final List<Platform> platformsToScan = new ArrayList<>();
boolean hasExternalLink = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.papermc.hangar.util;

import java.lang.reflect.Method;
import java.util.Arrays;
import org.jetbrains.annotations.NotNull;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKey;
import org.springframework.stereotype.Component;

@Component
public class IgnoringCaseCacheKeyGenerator implements KeyGenerator {

@Override
public @NotNull Object generate(final @NotNull Object target, final @NotNull Method method, final Object... params) {
if (params.length == 0) {
return SimpleKey.EMPTY;
}
if (params.length == 1) {
Object param = params[0];
if (param instanceof String s) {
return s.toLowerCase();
} else if (param != null && !param.getClass().isArray()) {
return param;
}
}

Object[] elements = Arrays.copyOf(params, params.length);
for (int i = 0; i < elements.length; i++) {
if (elements[i] instanceof String s) {
elements[i] = s.toLowerCase();
}
}
return new SimpleKey(elements);
}
}

0 comments on commit 7b9f4e5

Please sign in to comment.