Skip to content

Commit

Permalink
caching
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Nov 23, 2024
1 parent ee37e9c commit eef3316
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 165 deletions.
7 changes: 3 additions & 4 deletions src/main/java/de/oliver/fancynpcs/FancyNpcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
import de.oliver.fancynpcs.listeners.*;
import de.oliver.fancynpcs.skins.SkinManagerImpl;
import de.oliver.fancynpcs.skins.SkinUtils;
import de.oliver.fancynpcs.skins.cache.SkinCacheYaml;
import de.oliver.fancynpcs.skins.cache.SkinCacheFile;
import de.oliver.fancynpcs.skins.cache.SkinCacheMemory;
import de.oliver.fancynpcs.tracker.TurnToPlayerTracker;
import de.oliver.fancynpcs.tracker.VisibilityTracker;
import de.oliver.fancynpcs.v1_19_4.Npc_1_19_4;
Expand Down Expand Up @@ -86,7 +87,6 @@ public class FancyNpcs extends JavaPlugin implements FancyNpcsPlugin {
private NpcManagerImpl npcManager;
private AttributeManagerImpl attributeManager;
private SkinManager skinManager;
private SkinCacheYaml skinCache;
private ActionManagerImpl actionManager;
private VisibilityTracker visibilityTracker;
private boolean usingPlotSquared;
Expand Down Expand Up @@ -193,8 +193,7 @@ public void onEnable() {
actionManager.registerAction(new ExecuteRandomActionAction());
actionManager.registerAction(new BlockUntilDoneAction());

skinCache = new SkinCacheYaml();
skinManager = new SkinManagerImpl(skinCache);
skinManager = new SkinManagerImpl(new SkinCacheFile(), new SkinCacheMemory());

textConfig = new TextConfig("#E33239", "#AD1D23", "#81E366", "#E3CA66", "#E36666", "");
translator = new Translator(textConfig);
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/de/oliver/fancynpcs/skins/SkinManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public class SkinManagerImpl implements SkinManager {

private final ScheduledExecutorService executor;
private final MineSkinClient client;
private final SkinCache cache;

public SkinManagerImpl(SkinCache cache) {
private final SkinCache fileCache;
private final SkinCache memCache;

public SkinManagerImpl(SkinCache fileCache, SkinCache memCache) {
this.executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder()
.setNameFormat("FancyNpcs-Skins")
.build());
Expand All @@ -47,7 +49,8 @@ public SkinManagerImpl(SkinCache cache) {
.jobCheckScheduler(executor)
.build();

this.cache = cache;
this.fileCache = fileCache;
this.memCache = memCache;
}

@Override
Expand Down Expand Up @@ -180,4 +183,12 @@ private SkinInfo executeRequest(GenerateRequest req) {

return skinResp.join();
}

public SkinCache getFileCache() {
return fileCache;
}

public SkinCache getMemCache() {
return memCache;
}
}
6 changes: 5 additions & 1 deletion src/main/java/de/oliver/fancynpcs/skins/cache/SkinCache.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package de.oliver.fancynpcs.skins.cache;

import de.oliver.fancynpcs.api.skins.SkinData;

public interface SkinCache {

long CACHE_TIME = 1000 * 60 * 60 * 24 * 7; // 1 week

SkinCacheData getSkin(String identifier);

void addSkin(SkinCacheData skin);
void addSkin(SkinData skin);

void removeSkin(String identifier);

Expand Down
18 changes: 0 additions & 18 deletions src/main/java/de/oliver/fancynpcs/skins/cache/SkinCacheFake.java

This file was deleted.

55 changes: 55 additions & 0 deletions src/main/java/de/oliver/fancynpcs/skins/cache/SkinCacheFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.oliver.fancynpcs.skins.cache;

import de.oliver.fancylib.jdb.JDB;
import de.oliver.fancynpcs.FancyNpcs;
import de.oliver.fancynpcs.api.skins.SkinData;

import java.io.IOException;

public class SkinCacheFile implements SkinCache {

private final JDB storage;

public SkinCacheFile() {
this.storage = new JDB("plugins/FancyNpcs/.skinCache");
}

@Override
public SkinCacheData getSkin(String identifier) {
SkinCacheData skinCacheData = null;
try {
skinCacheData = this.storage.get("skins/" + identifier, SkinCacheData.class);
} catch (IOException e) {
FancyNpcs.getInstance().getFancyLogger().error("Failed to load skin cache");
FancyNpcs.getInstance().getFancyLogger().error(e);
}

if (skinCacheData == null) {
return null;
}

if (skinCacheData.isExpired()) {
this.storage.delete("skins/" + identifier);
return null;
}

return skinCacheData;
}

@Override
public void addSkin(SkinData skin) {
SkinCacheData skinCacheData = new SkinCacheData(skin, System.currentTimeMillis(), CACHE_TIME);

try {
this.storage.set("skins/" + skin.identifier(), skinCacheData);
} catch (IOException e) {
FancyNpcs.getInstance().getFancyLogger().error("Failed to save skin cache");
FancyNpcs.getInstance().getFancyLogger().error(e);
}
}

@Override
public void removeSkin(String identifier) {
this.storage.delete("skins/" + identifier);
}
}
42 changes: 42 additions & 0 deletions src/main/java/de/oliver/fancynpcs/skins/cache/SkinCacheMemory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.oliver.fancynpcs.skins.cache;


import de.oliver.fancynpcs.api.skins.SkinData;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SkinCacheMemory implements SkinCache {

private final Map<String, SkinCacheData> cache;

public SkinCacheMemory() {
this.cache = new ConcurrentHashMap<>();
}

@Override
public SkinCacheData getSkin(String identifier) {
if(!cache.containsKey(identifier)) {
return null;
}

SkinCacheData skinCacheData = cache.get(identifier);
if(skinCacheData.isExpired()) {
cache.remove(identifier);
return null;
}

return skinCacheData;
}

@Override
public void addSkin(SkinData skin) {
SkinCacheData skinCacheData = new SkinCacheData(skin, System.currentTimeMillis(), CACHE_TIME);
cache.put(skin.identifier(),skinCacheData);
}

@Override
public void removeSkin(String identifier) {
cache.remove(identifier);
}
}
137 changes: 0 additions & 137 deletions src/main/java/de/oliver/fancynpcs/skins/cache/SkinCacheYaml.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import de.oliver.fancynpcs.FancyNpcs;
import de.oliver.fancynpcs.api.skins.SkinData;
import de.oliver.fancynpcs.skins.SkinManagerImpl;
import de.oliver.fancynpcs.skins.cache.SkinCacheFake;
import de.oliver.fancynpcs.skins.cache.SkinCacheMemory;
import de.oliver.fancynpcs.tests.annotations.FNBeforeEach;
import de.oliver.fancynpcs.tests.annotations.FNTest;
import org.bukkit.entity.Player;
Expand All @@ -19,7 +19,7 @@ public class SkinManagerTest {
@FNBeforeEach
public void setUp(Player player) {

skinFetcher = new SkinManagerImpl(new SkinCacheFake());
skinFetcher = new SkinManagerImpl(new SkinCacheMemory(), new SkinCacheMemory());
}

@FNTest(name = "Test fetch skin by UUID")
Expand Down

0 comments on commit eef3316

Please sign in to comment.