Skip to content

Commit

Permalink
Added cache for both @NotNull String legacyText, @NotNull Component m…
Browse files Browse the repository at this point in the history
…odernText
  • Loading branch information
alexdev03 committed Jun 25, 2024
1 parent a43be88 commit 2b79796
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public class TextHolderProviderImpl extends TextHolderProvider {
.expireAfterWrite(Duration.of(5, ChronoUnit.MINUTES))
.build();

private static final Cache<Pair, TextHolder> pairCache = CacheBuilder.newBuilder()
.maximumSize(5000)
.expireAfterWrite(Duration.of(5, ChronoUnit.MINUTES))
.build();

public TextHolderProviderImpl() {
super();
}
Expand Down Expand Up @@ -72,7 +77,31 @@ public TextHolder of(@NotNull Component modernText) {

@Override
public TextHolder of(@NotNull String legacyText, @NotNull Component modernText) {
return new TextHolderImpl(legacyText, modernText); // TODO cache
try {
return pairCache.get(Pair.of(legacyText, modernText), () -> new TextHolderImpl(legacyText, modernText));
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

private record Pair(String legacy, Component modern) {

public static Pair of(String legacy, Component modern) {
return new Pair(legacy, modern);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Pair pair = (Pair) obj;
return legacy.equals(pair.legacy) && modern.equals(pair.modern);
}

@Override
public int hashCode() {
return 31 * legacy.hashCode() + modern.hashCode();
}
}

}

0 comments on commit 2b79796

Please sign in to comment.