add currently playing cache to spotify
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m28s

This commit is contained in:
Lee 2024-07-06 00:31:48 +01:00
parent 9395ae73b9
commit de1da2391d

@ -36,6 +36,13 @@ public class SpotifyService {
.expiration(30, TimeUnit.MINUTES)
.build();
/**
* A cache of the currently playing track for each user.
*/
private final Map<BatUser, CurrentlyPlaying> currentlyPlayingCache = ExpiringMap.builder()
.expiration(30, TimeUnit.SECONDS)
.build();
/**
* The client ID.
*/
@ -98,7 +105,12 @@ public class SpotifyService {
*/
@SneakyThrows
public CurrentlyPlaying getCurrentlyPlayingTrack(BatUser user) {
return getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute();
if (currentlyPlayingCache.containsKey(user)) {
return currentlyPlayingCache.get(user);
}
CurrentlyPlaying currentlyPlaying = getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute();
currentlyPlayingCache.put(user, currentlyPlaying);
return currentlyPlaying;
}
/**
@ -191,7 +203,7 @@ public class SpotifyService {
AuthorizationCodeCredentials credentials = api.authorizationCodeRefresh().build().execute();
profile.setAccessToken(credentials.getAccessToken());
profile.setExpiresAt(System.currentTimeMillis() + (credentials.getExpiresIn() * 1000));
log.info("Refreshed Spotify token for user {}", user.getName());
log.info("Refreshed Spotify token for user \"{}\"", user.getName());
} catch (SpotifyWebApiException ex) {
log.error("Failed to refresh Spotify token", ex);
}