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