Cache maker info with Caffeine
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -37,6 +37,7 @@
|
|||||||
<jdbi-version>3.37.1</jdbi-version> <!-- Convenience wrapper around JDBC -->
|
<jdbi-version>3.37.1</jdbi-version> <!-- Convenience wrapper around JDBC -->
|
||||||
<junit.version>5.9.2</junit.version> <!-- Unit test -->
|
<junit.version>5.9.2</junit.version> <!-- Unit test -->
|
||||||
<mockito.version>5.1.1</mockito.version> <!-- Unit test -->
|
<mockito.version>5.1.1</mockito.version> <!-- Unit test -->
|
||||||
|
<caffeine.version>3.1.4</caffeine.version> <!-- Caching -->
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -50,6 +51,11 @@
|
|||||||
<artifactId>jcip-annotations</artifactId>
|
<artifactId>jcip-annotations</artifactId>
|
||||||
<version>${jcip-annotations.version}</version>
|
<version>${jcip-annotations.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||||
|
<artifactId>caffeine</artifactId>
|
||||||
|
<version>${caffeine.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Database -->
|
<!-- Database -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -1,50 +1,30 @@
|
|||||||
package database.maker;
|
package database.maker;
|
||||||
|
|
||||||
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
import net.jcip.annotations.ThreadSafe;
|
import net.jcip.annotations.ThreadSafe;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
public class MakerInfoProvider {
|
public class MakerInfoProvider {
|
||||||
private final MakerDao makerDao;
|
private final MakerDao makerDao;
|
||||||
private final Map<Integer, MakerReagent> reagentCache = new ConcurrentHashMap<>();
|
private final Cache<Integer, Optional<MakerReagent>> reagentCache = Caffeine.newBuilder().build();
|
||||||
private final Map<Integer, MakerRecipe> recipeCache = new ConcurrentHashMap<>();
|
private final Cache<Integer, Optional<MakerRecipe>> recipeCache = Caffeine.newBuilder().build();
|
||||||
|
|
||||||
public MakerInfoProvider(MakerDao makerDao) {
|
public MakerInfoProvider(MakerDao makerDao) {
|
||||||
if (makerDao == null) {
|
if (makerDao == null) {
|
||||||
throw new IllegalArgumentException("MakerDao is null");
|
throw new IllegalArgumentException("MakerDao must not be null");
|
||||||
}
|
}
|
||||||
this.makerDao = makerDao;
|
this.makerDao = makerDao;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<MakerReagent> getMakerReagent(int itemId) {
|
public Optional<MakerReagent> getMakerReagent(int itemId) {
|
||||||
final MakerReagent cachedReagent = reagentCache.get(itemId);
|
return reagentCache.get(itemId, makerDao::getReagent);
|
||||||
if (cachedReagent != null) {
|
|
||||||
return Optional.of(cachedReagent);
|
|
||||||
}
|
|
||||||
|
|
||||||
final Optional<MakerReagent> reagentFromDb = makerDao.getReagent(itemId);
|
|
||||||
if (reagentFromDb.isEmpty()) {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
reagentCache.put(itemId, reagentFromDb.get());
|
|
||||||
return reagentFromDb;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<MakerRecipe> getMakerRecipe(int itemId) {
|
public Optional<MakerRecipe> getMakerRecipe(int itemId) {
|
||||||
final MakerRecipe cachedRecipe = recipeCache.get(itemId);
|
return recipeCache.get(itemId, makerDao::getRecipe);
|
||||||
if (cachedRecipe != null) {
|
|
||||||
return Optional.of(cachedRecipe);
|
|
||||||
}
|
|
||||||
|
|
||||||
final Optional<MakerRecipe> recipeFromDb = makerDao.getRecipe(itemId);
|
|
||||||
if (recipeFromDb.isEmpty()) {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
recipeCache.put(itemId, recipeFromDb.get());
|
|
||||||
return recipeFromDb;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Integer> getStimulant(int itemId) {
|
public Optional<Integer> getStimulant(int itemId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user