1919import java .sql .SQLException ;
2020import java .sql .Timestamp ;
2121import java .time .Duration ;
22+ import java .time .Instant ;
2223import java .util .Date ;
2324import java .util .Optional ;
2425import java .util .UUID ;
2526import java .util .logging .Level ;
27+ import java .util .logging .Logger ;
2628
2729/**
2830 * Represents a player profile cached in the database.
@@ -40,6 +42,12 @@ public record CachedProfile(@NotNull UUID uuid, @NotNull String name, @NotNull D
4042 .connectTimeout (Duration .ofSeconds (15 ))
4143 .build ();
4244
45+ private static final @ NotNull Logger logger = Logger .getLogger ("ProfileCache" );
46+
47+ static {
48+ logger .setParent (SMPCore .getInstance ().getLogger ());
49+ }
50+
4351 /**
4452 * Gets a cached profile by UUID.
4553 *
@@ -58,7 +66,7 @@ public record CachedProfile(@NotNull UUID uuid, @NotNull String name, @NotNull D
5866 return Optional .<@ NotNull CachedProfile >ofNullable (CachedProfile .from (rs ));
5967 }
6068 catch (final SQLException e ) {
61- SMPCore . getInstance (). getLogger (). log (Level .SEVERE , "could not get cached profile for UUID " + uuid , e );
69+ logger . log (Level .SEVERE , "could not get profile for UUID " + uuid , e );
6270 return Optional .empty ();
6371 }
6472 }
@@ -78,6 +86,8 @@ public record CachedProfile(@NotNull UUID uuid, @NotNull String name, @NotNull D
7886 * @param uuid UUID of the player to fetch.
7987 */
8088 private static @ Nullable CachedProfile fetch (final @ NotNull UUID uuid ) {
89+ logger .fine ("Miss for UUID " + uuid );
90+
8191 final PluginMeta meta = SMPCore .getInstance ().getPluginMeta ();
8292
8393 final HttpRequest req = HttpRequest .newBuilder ()
@@ -91,8 +101,8 @@ public record CachedProfile(@NotNull UUID uuid, @NotNull String name, @NotNull D
91101 .send (req , HttpResponse .BodyHandlers .ofString ());
92102
93103 if (res .statusCode () >= 400 || res .statusCode () == 204 || res .statusCode () < 200 || res .body () == null ) {
94- SMPCore . getInstance (). getLogger () .log (Level .SEVERE , "got HTTP status " + res .statusCode ()
95- + " fetching profile for UUID " + uuid + ". Body is" + (res .body () == null ? "" : " not" )
104+ logger .log (Level .SEVERE , "got HTTP status " + res .statusCode ()
105+ + " for UUID " + uuid + ". Body is" + (res .body () == null ? "" : " not" )
96106 + " null." );
97107 return null ;
98108 }
@@ -103,7 +113,7 @@ public record CachedProfile(@NotNull UUID uuid, @NotNull String name, @NotNull D
103113 .save ();
104114 }
105115 catch (final IOException | InterruptedException e ) {
106- SMPCore . getInstance (). getLogger () .log (Level .SEVERE , "could not fetch profile for UUID " + uuid , e );
116+ logger .log (Level .SEVERE , "could not fetch profile for UUID " + uuid , e );
107117 return null ;
108118 }
109119 }
@@ -147,7 +157,7 @@ public record CachedProfile(@NotNull UUID uuid, @NotNull String name, @NotNull D
147157 * @param name Username of the player to look up.
148158 */
149159 public static @ Nullable OfflinePlayer getOffline (final @ NotNull String name ) {
150- final @ Nullable OfflinePlayer player = Bukkit .getOfflinePlayerIfCached (name );
160+ final @ Nullable OfflinePlayer player = Bukkit .getOfflinePlayerIfCached (name );
151161 if (player != null )
152162 return player ;
153163
@@ -167,7 +177,7 @@ public record CachedProfile(@NotNull UUID uuid, @NotNull String name, @NotNull D
167177 return Bukkit .getOfflinePlayer (profile .uuid ());
168178 }
169179 catch (final SQLException e ) {
170- SMPCore . getInstance (). getLogger () .log (Level .SEVERE , "could not get cached profile for name " + name , e );
180+ logger .log (Level .SEVERE , "could not get cached profile for name " + name , e );
171181 return null ;
172182 }
173183 }
@@ -179,29 +189,33 @@ public record CachedProfile(@NotNull UUID uuid, @NotNull String name, @NotNull D
179189 */
180190 public static @ NotNull OfflinePlayer get (final @ NotNull String name ) {
181191 final OfflinePlayer player = Optional .<@ NotNull OfflinePlayer >ofNullable (getOffline (name ))
182- .orElseGet (() -> Bukkit .getOfflinePlayer (name ));
192+ .orElseGet (() -> {
193+ logger .fine ("Miss for name " + name );
194+ return Bukkit .getOfflinePlayer (name );
195+ });
183196
184197 final @ Nullable CachedProfile profile = CachedProfile .from (player );
185198 if (profile != null )
186199 return Bukkit .getOfflinePlayer (profile .uuid ());
187200
188201 // unlikely
202+ logger .warning ("Unlikely cache miss for name " + name );
189203 return Bukkit .getOfflinePlayer (name );
190204 }
191205
192206 /**
193207 * Deletes stale cached profiles.
194208 */
195209 public static void cleanUp () {
196- SMPCore . getInstance (). getLogger (). info ("Cleaning up stale cached profiles..." );
210+ logger . fine ("Cleaning up stale cached profiles..." );
197211 try (final @ NotNull PreparedStatement stmt = SMPCore .getInstance ().conn .prepareStatement (
198212 "DELETE FROM `cached_profiles` WHERE `fetched` < ?"
199213 )) {
200214 stmt .setTimestamp (1 , new Timestamp (staleWhileRevalidateThreshold ().getTime ()));
201215 stmt .execute ();
202216 }
203217 catch (final SQLException e ) {
204- SMPCore . getInstance (). getLogger () .log (Level .SEVERE , "could not clean up cached profiles" , e );
218+ logger .log (Level .SEVERE , "could not clean up cached profiles" , e );
205219 }
206220 }
207221
@@ -222,7 +236,7 @@ public static void cleanUp() {
222236 return get (player ).name ();
223237 }
224238 catch (IllegalStateException e ) {
225- SMPCore . getInstance (). getLogger (). warning ( "Failed to fetch" );
239+ logger . log ( Level . SEVERE , "Failed to fetch" , e );
226240 return player .getUniqueId ().toString ();
227241 }
228242 }
@@ -243,9 +257,14 @@ public static void cleanUp() {
243257 );
244258
245259 if (profile .stale ()) {
260+ logger .fine ("Profile cache is stale for " + profile .uuid + " (" + Duration .between (profile .fetched .toInstant (), Instant .now ()) + " old). Revalidation scheduled." );
246261 SMPCore .runAsync (() -> CachedProfile .fetch (profile .uuid ()));
247262
248- return profile .staleWhileRevalidate () ? profile : null ;
263+ if (profile .staleWhileRevalidate ()) {
264+ logger .fine ("Serving stale cache for " + profile .uuid + " while revalidating." );
265+ return profile ;
266+ }
267+ return null ;
249268 }
250269
251270 return profile ;
@@ -277,7 +296,7 @@ private boolean staleWhileRevalidate() {
277296 stmt .setTimestamp (3 , new Timestamp (fetched ().getTime ()));
278297 stmt .executeUpdate ();
279298 } catch (final SQLException e ) {
280- SMPCore . getInstance (). getLogger () .log (Level .SEVERE , "could not save profile for UUID " + uuid , e );
299+ logger .log (Level .SEVERE , "could not save profile for UUID " + uuid , e );
281300 }
282301 });
283302
0 commit comments