1
0
mirror of https://github.com/Suiranoil/SkinRestorer.git synced 2026-01-16 04:42:12 +00:00

make cache duration for providers configurable

This commit is contained in:
2024-09-03 20:46:24 +03:00
parent 8baad37227
commit 75889944e9
5 changed files with 49 additions and 7 deletions

View File

@@ -76,6 +76,10 @@ public final class SkinRestorer {
SkinRestorer.config = Config.load(SkinRestorer.getConfigDir());
Translation.reloadTranslations();
WebUtils.recreateHttpClient();
MojangSkinProvider.createCache();
ElyBySkinProvider.createCache();
MineskinSkinProvider.createCache();
}
public static String assetPath(String name) {

View File

@@ -25,6 +25,10 @@ public final class Config {
private long requestTimeout = 10;
private long mojangCacheDuration = 60;
private long elybyCacheDuration = 60;
private long mineskinCacheDuration = 300;
public String getLanguage() {
return this.language;
}
@@ -49,6 +53,18 @@ public final class Config {
return this.requestTimeout;
}
public long getMojangCacheDuration() {
return this.mojangCacheDuration;
}
public long getElybyCacheDuration() {
return this.elybyCacheDuration;
}
public long getMineskinCacheDuration() {
return this.mineskinCacheDuration;
}
public static Config load(Path path) {
var configFile = path.resolve(Config.CONFIG_FILENAME);
@@ -90,5 +106,14 @@ public final class Config {
if (this.requestTimeout <= 0)
this.requestTimeout = 10;
if (this.mojangCacheDuration < 0)
this.mojangCacheDuration = 60;
if (this.elybyCacheDuration < 0)
this.elybyCacheDuration = 60;
if (this.mineskinCacheDuration < 0)
this.mineskinCacheDuration = 300;
}
}

View File

@@ -6,6 +6,7 @@ import com.google.common.cache.LoadingCache;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.yggdrasil.response.MinecraftProfilePropertiesResponse;
import net.lionarius.skinrestorer.SkinRestorer;
import net.lionarius.skinrestorer.skin.SkinVariant;
import net.lionarius.skinrestorer.util.JsonUtils;
import net.lionarius.skinrestorer.util.PlayerUtils;
@@ -28,7 +29,7 @@ public final class ElyBySkinProvider implements SkinProvider {
private static final URI API_URI;
private static final LoadingCache<String, Optional<Property>> SKIN_CACHE;
private static LoadingCache<String, Optional<Property>> SKIN_CACHE;
static {
try {
@@ -37,8 +38,12 @@ public final class ElyBySkinProvider implements SkinProvider {
throw new IllegalArgumentException(e);
}
}
public static void createCache() {
SKIN_CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(60, TimeUnit.SECONDS)
.expireAfterWrite(SkinRestorer.getConfig().getElybyCacheDuration(), TimeUnit.SECONDS)
.build(new CacheLoader<>() {
@Override
public @NotNull Optional<Property> load(@NotNull String key) throws Exception {
@@ -64,6 +69,7 @@ public final class ElyBySkinProvider implements SkinProvider {
throw new IllegalArgumentException("invalid username");
var usernameLowerCase = username.toLowerCase(Locale.ROOT);
return Result.success(SKIN_CACHE.get(usernameLowerCase));
} catch (Exception e) {
return Result.error(e);

View File

@@ -6,6 +6,7 @@ import com.google.common.cache.LoadingCache;
import com.google.gson.JsonObject;
import com.mojang.authlib.properties.Property;
import it.unimi.dsi.fastutil.Pair;
import net.lionarius.skinrestorer.SkinRestorer;
import net.lionarius.skinrestorer.skin.SkinVariant;
import net.lionarius.skinrestorer.util.JsonUtils;
import net.lionarius.skinrestorer.util.PlayerUtils;
@@ -26,7 +27,7 @@ public final class MineskinSkinProvider implements SkinProvider {
private static final URI API_URI;
private static final LoadingCache<Pair<URI, SkinVariant>, Optional<Property>> SKIN_CACHE;
private static LoadingCache<Pair<URI, SkinVariant>, Optional<Property>> SKIN_CACHE;
static {
try {
@@ -34,9 +35,11 @@ public final class MineskinSkinProvider implements SkinProvider {
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}
public static void createCache() {
SKIN_CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(300, TimeUnit.SECONDS)
.expireAfterWrite(SkinRestorer.getConfig().getMineskinCacheDuration(), TimeUnit.SECONDS)
.build(new CacheLoader<>() {
@Override
public @NotNull Optional<Property> load(@NotNull Pair<URI, SkinVariant> key) throws Exception {

View File

@@ -35,7 +35,7 @@ public final class MojangSkinProvider implements SkinProvider {
public static final String PROFILE_CACHE_FILENAME = "mojang_profile_cache.json";
private static final GameProfileCache PROFILE_CACHE;
private static final LoadingCache<UUID, Optional<Property>> SKIN_CACHE;
private static LoadingCache<UUID, Optional<Property>> SKIN_CACHE;
static {
try {
@@ -56,8 +56,12 @@ public final class MojangSkinProvider implements SkinProvider {
}
}, SkinRestorer.getConfigDir().resolve(PROFILE_CACHE_FILENAME).toFile());
}
public static void createCache() {
SKIN_CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(60, TimeUnit.SECONDS)
.expireAfterWrite(SkinRestorer.getConfig().getMojangCacheDuration(), TimeUnit.SECONDS)
.build(new CacheLoader<>() {
@Override
public @NotNull Optional<Property> load(@NotNull UUID key) throws Exception {