Advertisement
Guest User

Untitled

a guest
Mar 10th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. package com.github.shortcube.skywars.mysql;
  2.  
  3. import com.github.shortcube.skywars.SkyWars;
  4. import com.google.common.cache.CacheBuilder;
  5. import com.google.common.cache.CacheLoader;
  6. import com.google.common.cache.LoadingCache;
  7.  
  8. import java.sql.ResultSet;
  9. import java.util.concurrent.ExecutionException;
  10. import java.util.concurrent.TimeUnit;
  11.  
  12. public class CachedStatsLoader {
  13.  
  14. private final LoadingCache< String, Integer > pointsCache = CacheBuilder
  15. .newBuilder()
  16. .expireAfterAccess( 3, TimeUnit.SECONDS )
  17. .build(new CacheLoader<String, Integer>( ) {
  18. @Override
  19. public Integer load(String string) throws Exception {
  20. Integer result = -1;
  21. ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
  22. string +
  23. "'");
  24.  
  25. if ((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("points")) == null)) {
  26. result = resultSet.getInt("points");
  27. }
  28. resultSet.close();
  29.  
  30. return result;
  31. }
  32. });
  33.  
  34. private final LoadingCache< String, Integer > deathsCache = CacheBuilder
  35. .newBuilder()
  36. .expireAfterAccess( 5, TimeUnit.MINUTES )
  37. .build(new CacheLoader<String, Integer>( ) {
  38. @Override
  39. public Integer load(String string) throws Exception {
  40. Integer result = -1;
  41. ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
  42. string +
  43. "'");
  44.  
  45. if((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("DEATHS")) == null)) {
  46. result = resultSet.getInt("DEATHS");
  47. }
  48. resultSet.close();
  49.  
  50. return result;
  51. }
  52. });
  53.  
  54. private final LoadingCache< String, Integer > killsCache = CacheBuilder
  55. .newBuilder()
  56. .expireAfterAccess( 5, TimeUnit.MINUTES )
  57. .build(new CacheLoader<String, Integer>( ) {
  58. @Override
  59. public Integer load(String string) throws Exception {
  60. Integer result = -1;
  61. ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
  62. string +
  63. "'");
  64.  
  65. if ((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("KILLS")) == null)) {
  66. result = resultSet.getInt("KILLS");
  67. }
  68. resultSet.close();
  69.  
  70. return result;
  71. }
  72. });
  73.  
  74. private final LoadingCache< String, Integer > winsCache = CacheBuilder
  75. .newBuilder()
  76. .expireAfterAccess( 5, TimeUnit.MINUTES )
  77. .build(new CacheLoader<String, Integer>( ) {
  78. @Override
  79. public Integer load(String string) throws Exception {
  80. Integer result = -1;
  81. ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
  82. string +
  83. "'");
  84.  
  85. if ((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("WINS")) == null)) {
  86. result = resultSet.getInt("WINS");
  87. }
  88. resultSet.close();
  89.  
  90. return result;
  91. }
  92. });
  93.  
  94. private final LoadingCache< String, Integer > gamesCache = CacheBuilder
  95. .newBuilder()
  96. .expireAfterAccess( 5, TimeUnit.MINUTES )
  97. .build(new CacheLoader<String, Integer>( ) {
  98. @Override
  99. public Integer load(String string) throws Exception {
  100. Integer result = -1;
  101. ResultSet resultSet = SkyWars.mysql.query("SELECT * FROM stats WHERE UUID= '" +
  102. string +
  103. "'");
  104.  
  105. if ((!resultSet.next()) || (Integer.valueOf(resultSet.getInt("GAMES")) == null)) {
  106. result = resultSet.getInt("GAMES");
  107. }
  108. resultSet.close();
  109.  
  110. return result;
  111. }
  112. });
  113.  
  114. public Integer getPoints( String UUID ) {
  115. try {
  116. return this.pointsCache.get( UUID );
  117. } catch (ExecutionException e) {
  118. e.printStackTrace();
  119. return -1;
  120. }
  121. }
  122.  
  123. public Integer getDeaths ( String UUID ) {
  124. try {
  125. return this.deathsCache.get( UUID );
  126. } catch (ExecutionException e) {
  127. e.printStackTrace();
  128. return -1;
  129. }
  130. }
  131.  
  132. public Integer getKills( String UUID ) {
  133. try {
  134. return this.killsCache.get( UUID );
  135. } catch (ExecutionException e) {
  136. e.printStackTrace();
  137. return -1;
  138. }
  139. }
  140.  
  141. public Integer getWins( String UUID ) {
  142. try {
  143. return this.winsCache.get( UUID );
  144. } catch (ExecutionException e) {
  145. e.printStackTrace();
  146. return -1;
  147. }
  148. }
  149.  
  150. public Integer getGames( String UUID ) {
  151. try {
  152. return this.gamesCache.get( UUID );
  153. } catch (ExecutionException e) {
  154. e.printStackTrace();
  155. return -1;
  156. }
  157. }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement