Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.github.shortcube.skywars.mysql;
- import com.github.shortcube.skywars.SkyWars;
- import com.google.common.cache.CacheBuilder;
- import com.google.common.cache.CacheLoader;
- import com.google.common.cache.LoadingCache;
- import java.sql.ResultSet;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.TimeUnit;
- public class CachedStatsLoader {
- private final LoadingCache< String, Integer > pointsCache = CacheBuilder
- .newBuilder()
- .expireAfterAccess( 3, TimeUnit.SECONDS )
- .build(new CacheLoader<String, Integer>( ) {
- @Override
- public Integer load(String string) throws Exception {
- Integer result = -1;
- ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
- string +
- "'");
- if ((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("points")) == null)) {
- result = resultSet.getInt("points");
- }
- resultSet.close();
- return result;
- }
- });
- private final LoadingCache< String, Integer > deathsCache = CacheBuilder
- .newBuilder()
- .expireAfterAccess( 5, TimeUnit.MINUTES )
- .build(new CacheLoader<String, Integer>( ) {
- @Override
- public Integer load(String string) throws Exception {
- Integer result = -1;
- ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
- string +
- "'");
- if((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("DEATHS")) == null)) {
- result = resultSet.getInt("DEATHS");
- }
- resultSet.close();
- return result;
- }
- });
- private final LoadingCache< String, Integer > killsCache = CacheBuilder
- .newBuilder()
- .expireAfterAccess( 5, TimeUnit.MINUTES )
- .build(new CacheLoader<String, Integer>( ) {
- @Override
- public Integer load(String string) throws Exception {
- Integer result = -1;
- ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
- string +
- "'");
- if ((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("KILLS")) == null)) {
- result = resultSet.getInt("KILLS");
- }
- resultSet.close();
- return result;
- }
- });
- private final LoadingCache< String, Integer > winsCache = CacheBuilder
- .newBuilder()
- .expireAfterAccess( 5, TimeUnit.MINUTES )
- .build(new CacheLoader<String, Integer>( ) {
- @Override
- public Integer load(String string) throws Exception {
- Integer result = -1;
- ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
- string +
- "'");
- if ((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("WINS")) == null)) {
- result = resultSet.getInt("WINS");
- }
- resultSet.close();
- return result;
- }
- });
- private final LoadingCache< String, Integer > gamesCache = CacheBuilder
- .newBuilder()
- .expireAfterAccess( 5, TimeUnit.MINUTES )
- .build(new CacheLoader<String, Integer>( ) {
- @Override
- public Integer load(String string) throws Exception {
- Integer result = -1;
- ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
- string +
- "'");
- if ((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("GAMES")) == null)) {
- result = resultSet.getInt("GAMES");
- }
- resultSet.close();
- return result;
- }
- });
- public Integer getPoints( String UUID ) {
- try {
- return this.pointsCache.get( UUID );
- } catch (ExecutionException e) {
- e.printStackTrace();
- return -1;
- }
- }
- public Integer getDeaths ( String UUID ) {
- try {
- return this.deathsCache.get( UUID );
- } catch (ExecutionException e) {
- e.printStackTrace();
- return -1;
- }
- }
- public Integer getKills( String UUID ) {
- try {
- return this.killsCache.get( UUID );
- } catch (ExecutionException e) {
- e.printStackTrace();
- return -1;
- }
- }
- public Integer getWins( String UUID ) {
- try {
- return this.winsCache.get( UUID );
- } catch (ExecutionException e) {
- e.printStackTrace();
- return -1;
- }
- }
- public Integer getGames( String UUID ) {
- try {
- return this.gamesCache.get( UUID );
- } catch (ExecutionException e) {
- e.printStackTrace();
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement