mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-23 00:21:43 +01:00
Made GeolocationCache use Caffeine
This commit is contained in:
parent
d2b5415094
commit
6df205074d
@ -25,6 +25,8 @@ import com.djrapitops.plan.settings.locale.lang.PluginLang;
|
|||||||
import com.djrapitops.plan.storage.file.PlanFiles;
|
import com.djrapitops.plan.storage.file.PlanFiles;
|
||||||
import com.djrapitops.plugin.logging.L;
|
import com.djrapitops.plugin.logging.L;
|
||||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||||
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
import com.maxmind.geoip2.DatabaseReader;
|
import com.maxmind.geoip2.DatabaseReader;
|
||||||
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
||||||
import com.maxmind.geoip2.model.CountryResponse;
|
import com.maxmind.geoip2.model.CountryResponse;
|
||||||
@ -43,8 +45,7 @@ import java.nio.channels.Channels;
|
|||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.nio.channels.ReadableByteChannel;
|
import java.nio.channels.ReadableByteChannel;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.HashMap;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,7 +62,7 @@ public class GeolocationCache implements SubSystem {
|
|||||||
private final PlanFiles files;
|
private final PlanFiles files;
|
||||||
private final PlanConfig config;
|
private final PlanConfig config;
|
||||||
private final PluginLogger logger;
|
private final PluginLogger logger;
|
||||||
private final Map<String, String> cached;
|
private final Cache<String, String> cache;
|
||||||
|
|
||||||
private File geolocationDB;
|
private File geolocationDB;
|
||||||
|
|
||||||
@ -77,7 +78,9 @@ public class GeolocationCache implements SubSystem {
|
|||||||
this.config = config;
|
this.config = config;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
|
||||||
this.cached = new HashMap<>();
|
this.cache = Caffeine.newBuilder()
|
||||||
|
.expireAfterAccess(1, TimeUnit.MINUTES)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -109,16 +112,7 @@ public class GeolocationCache implements SubSystem {
|
|||||||
* @see #getUnCachedCountry(String)
|
* @see #getUnCachedCountry(String)
|
||||||
*/
|
*/
|
||||||
public String getCountry(String ipAddress) {
|
public String getCountry(String ipAddress) {
|
||||||
String country = getCachedCountry(ipAddress);
|
return cache.get(ipAddress, this::getUnCachedCountry);
|
||||||
|
|
||||||
if (country != null) {
|
|
||||||
return country;
|
|
||||||
} else {
|
|
||||||
country = getUnCachedCountry(ipAddress);
|
|
||||||
cached.put(ipAddress, country);
|
|
||||||
|
|
||||||
return country;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -128,7 +122,7 @@ public class GeolocationCache implements SubSystem {
|
|||||||
* @return The cached country, {@code null} if the country is not cached
|
* @return The cached country, {@code null} if the country is not cached
|
||||||
*/
|
*/
|
||||||
private String getCachedCountry(String ipAddress) {
|
private String getCachedCountry(String ipAddress) {
|
||||||
return cached.get(ipAddress);
|
return cache.getIfPresent(ipAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -200,18 +194,19 @@ public class GeolocationCache implements SubSystem {
|
|||||||
* @return true if the IP Address is cached
|
* @return true if the IP Address is cached
|
||||||
*/
|
*/
|
||||||
boolean isCached(String ipAddress) {
|
boolean isCached(String ipAddress) {
|
||||||
return cached.containsKey(ipAddress);
|
return cache.getIfPresent(ipAddress) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void disable() {
|
public void disable() {
|
||||||
cached.clear();
|
clearCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears the cache
|
* Clears the cache
|
||||||
*/
|
*/
|
||||||
public void clearCache() {
|
public void clearCache() {
|
||||||
cached.clear();
|
cache.invalidateAll();
|
||||||
|
cache.cleanUp();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user