11package net .javadiscord .javabot .api .routes .user_profile ;
22
3+ import com .github .benmanes .caffeine .cache .Caffeine ;
34import net .dv8tion .jda .api .entities .Guild ;
45import net .dv8tion .jda .api .entities .User ;
56import net .javadiscord .javabot .Bot ;
67import net .javadiscord .javabot .api .response .ApiResponseBuilder ;
78import net .javadiscord .javabot .api .response .ApiResponses ;
9+ import net .javadiscord .javabot .api .routes .CaffeineCache ;
810import net .javadiscord .javabot .api .routes .JDAEntity ;
911import net .javadiscord .javabot .api .routes .user_profile .model .HelpAccountData ;
1012import net .javadiscord .javabot .api .routes .user_profile .model .UserProfileData ;
1719import net .javadiscord .javabot .systems .user_preferences .model .Preference ;
1820import net .javadiscord .javabot .systems .user_preferences .model .UserPreference ;
1921import net .javadiscord .javabot .util .ExceptionLogger ;
22+ import net .javadiscord .javabot .util .Pair ;
2023import org .springframework .http .HttpStatus ;
2124import org .springframework .http .MediaType ;
2225import org .springframework .http .ResponseEntity ;
2932import java .time .LocalDateTime ;
3033import java .util .Arrays ;
3134import java .util .List ;
35+ import java .util .concurrent .TimeUnit ;
3236
3337/**
3438 * Handles all GET-Requests on the {guild_id}/{user_id} route.
3539 */
3640@ RestController
37- public class UserProfileController implements JDAEntity {
41+ public class UserProfileController extends CaffeineCache <Pair <Long , Long >, UserProfileData > implements JDAEntity {
42+
43+ /**
44+ * The constructor of this class which initializes the {@link Caffeine} cache.
45+ */
46+ public UserProfileController () {
47+ super (Caffeine .newBuilder ()
48+ .expireAfterWrite (10 , TimeUnit .SECONDS )
49+ .build ()
50+ );
51+ }
3852
3953 /**
4054 * Serves a single users' profile in a specified guild.
@@ -60,28 +74,33 @@ public ResponseEntity<String> getUserProfile(
6074 return new ResponseEntity <>(ApiResponses .INVALID_USER_IN_REQUEST , HttpStatus .BAD_REQUEST );
6175 }
6276 try (Connection con = Bot .getDataSource ().getConnection ()) {
63- UserProfileData data = new UserProfileData ();
64- data .setUserId (user .getIdLong ());
65- data .setUserName (user .getName ());
66- data .setDiscriminator (user .getDiscriminator ());
67- data .setEffectiveAvatarUrl (user .getEffectiveAvatarUrl ());
68- // Question of the Week Account
69- QOTWPointsService qotwService = new QOTWPointsService (Bot .getDataSource ());
70- QOTWAccount qotwAccount = qotwService .getOrCreateAccount (user .getIdLong ());
71- data .setQotwAccount (qotwAccount );
72- // Help Account
73- HelpExperienceService helpService = new HelpExperienceService (Bot .getDataSource ());
74- HelpAccount helpAccount = helpService .getOrCreateAccount (user .getIdLong ());
75- data .setHelpAccount (HelpAccountData .of (helpAccount , guild ));
76- // User Preferences
77- UserPreferenceService preferenceService = new UserPreferenceService (Bot .getDataSource ());
78- List <UserPreference > preferences = Arrays .stream (Preference .values ()).map (p -> preferenceService .getOrCreate (user .getIdLong (), p )).toList ();
79- data .setPreferences (preferences );
80- // User Warns
81- WarnRepository warnRepository = new WarnRepository (con );
82- LocalDateTime cutoff = LocalDateTime .now ().minusDays (Bot .getConfig ().get (guild ).getModerationConfig ().getWarnTimeoutDays ());
83- data .setWarns (warnRepository .getWarnsByUserId (user .getIdLong (), cutoff ));
84-
77+ // Check Cache
78+ UserProfileData data = getCache ().getIfPresent (new Pair <>(guild .getIdLong (), user .getIdLong ()));
79+ if (data == null ) {
80+ data = new UserProfileData ();
81+ data .setUserId (user .getIdLong ());
82+ data .setUserName (user .getName ());
83+ data .setDiscriminator (user .getDiscriminator ());
84+ data .setEffectiveAvatarUrl (user .getEffectiveAvatarUrl ());
85+ // Question of the Week Account
86+ QOTWPointsService qotwService = new QOTWPointsService (Bot .getDataSource ());
87+ QOTWAccount qotwAccount = qotwService .getOrCreateAccount (user .getIdLong ());
88+ data .setQotwAccount (qotwAccount );
89+ // Help Account
90+ HelpExperienceService helpService = new HelpExperienceService (Bot .getDataSource ());
91+ HelpAccount helpAccount = helpService .getOrCreateAccount (user .getIdLong ());
92+ data .setHelpAccount (HelpAccountData .of (helpAccount , guild ));
93+ // User Preferences
94+ UserPreferenceService preferenceService = new UserPreferenceService (Bot .getDataSource ());
95+ List <UserPreference > preferences = Arrays .stream (Preference .values ()).map (p -> preferenceService .getOrCreate (user .getIdLong (), p )).toList ();
96+ data .setPreferences (preferences );
97+ // User Warns
98+ WarnRepository warnRepository = new WarnRepository (con );
99+ LocalDateTime cutoff = LocalDateTime .now ().minusDays (Bot .getConfig ().get (guild ).getModerationConfig ().getWarnTimeoutDays ());
100+ data .setWarns (warnRepository .getWarnsByUserId (user .getIdLong (), cutoff ));
101+ // Insert into cache
102+ getCache ().put (new Pair <>(guild .getIdLong (), user .getIdLong ()), data );
103+ }
85104 return new ResponseEntity <>(new ApiResponseBuilder ().add ("profile" , data ).build (), HttpStatus .OK );
86105 } catch (SQLException e ) {
87106 ExceptionLogger .capture (e , getClass ().getSimpleName ());
0 commit comments