Skip to content

Commit acb3eb4

Browse files
committed
change klaxon, expireTime visibility, replace cache runnable with a lazy approach
1 parent 89075bb commit acb3eb4

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

.idea/misc.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/kotlin/com/adamratzman/spotify/SpotifyAPI.kt

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ import com.adamratzman.spotify.models.serialization.getSavedTrackConverter
3232
import com.adamratzman.spotify.models.serialization.toObject
3333
import com.beust.klaxon.Klaxon
3434
import java.util.concurrent.Executors
35-
import java.util.concurrent.ScheduledFuture
36-
import java.util.concurrent.TimeUnit
35+
import java.util.concurrent.ScheduledExecutorService
3736

3837
internal val base = "https://api.spotify.com/v1"
3938

@@ -67,20 +66,16 @@ abstract class SpotifyAPI internal constructor(
6766
var retryWhenRateLimited: Boolean,
6867
enableLogger: Boolean
6968
) {
70-
private var refreshFuture: ScheduledFuture<*>? = null
71-
7269
var useCache = useCache
7370
set(value) {
74-
if (!useCache && value) refreshFuture = startCacheRefreshRunnable()
75-
else if (useCache && !value) refreshFuture?.cancel(false)
76-
7771
if (!value) clearCache()
7872

7973
field = value
8074
}
8175

82-
internal var expireTime = System.currentTimeMillis() + token.expiresIn * 1000
83-
internal val executor = Executors.newScheduledThreadPool(0)
76+
val expireTime: Long get() = System.currentTimeMillis() + token.expiresIn * 1000
77+
78+
val executor: ScheduledExecutorService = Executors.newScheduledThreadPool(0)
8479

8580
abstract val search: SearchAPI
8681
abstract val albums: AlbumAPI
@@ -91,9 +86,9 @@ abstract class SpotifyAPI internal constructor(
9186
abstract val tracks: TracksAPI
9287
abstract val following: FollowingAPI
9388

94-
internal val logger = SpotifyLogger(enableLogger)
89+
val logger = SpotifyLogger(enableLogger)
9590

96-
abstract val klaxon: Klaxon
91+
internal abstract val klaxon: Klaxon
9792

9893
/**
9994
* If the method used to create the [token] supports token refresh and
@@ -119,12 +114,6 @@ abstract class SpotifyAPI internal constructor(
119114
*/
120115
abstract fun getApiBuilderDsl(): SpotifyApiBuilderDsl
121116

122-
init {
123-
if (useCache) refreshFuture = startCacheRefreshRunnable()
124-
}
125-
126-
private fun startCacheRefreshRunnable() = executor.scheduleAtFixedRate(::clearCache, 10, 10, TimeUnit.MINUTES)
127-
128117
internal fun clearAllCaches(vararg endpoints: SpotifyEndpoint) {
129118
endpoints.forEach { it.cache.clear() }
130119
}
@@ -194,7 +183,6 @@ class SpotifyAppAPI internal constructor(
194183
val currentToken = this.token
195184

196185
token = getCredentialedToken(clientId, clientSecret, this)
197-
expireTime = System.currentTimeMillis() + token.expiresIn * 1000
198186

199187
return currentToken
200188
}
@@ -304,7 +292,7 @@ class SpotifyClientAPI internal constructor(
304292
* Stop all automatic functions like refreshToken or clearCache and shut down the scheduled
305293
* executor
306294
* */
307-
fun cancelAutomatics() = executor.shutdown()
295+
fun shutdown() = executor.shutdown()
308296

309297
override fun refreshToken(): Token {
310298
val currentToken = this.token

src/main/kotlin/com/adamratzman/spotify/http/Endpoints.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ abstract class SpotifyEndpoint(val api: SpotifyAPI) {
4848
): String {
4949
if (api is SpotifyAppAPI && System.currentTimeMillis() >= api.expireTime) api.refreshToken()
5050

51-
val spotifyRequest = SpotifyRequest(url, method, body)
51+
val spotifyRequest = SpotifyRequest(url, method, body, api)
5252
val cacheState = if (api.useCache) cache[spotifyRequest] else null
5353

5454
if (cacheState?.isStillValid() == true) return cacheState.data
@@ -142,25 +142,33 @@ internal class EndpointBuilder(private val path: String) {
142142
internal class SpotifyCache {
143143
private val cachedRequests = hashMapOf<SpotifyRequest, CacheState>()
144144

145-
internal operator fun get(spotifyRequest: SpotifyRequest): CacheState? {
146-
return cachedRequests[spotifyRequest]
145+
internal operator fun get(request: SpotifyRequest): CacheState? {
146+
checkCache(request)
147+
return cachedRequests[request]
147148
}
148149

149150
internal operator fun set(request: SpotifyRequest, state: CacheState) {
150-
cachedRequests[request] = state
151+
checkCache(request)
152+
if (request.api.useCache) cachedRequests[request] = state
151153
}
152154

153-
internal operator fun minusAssign(spotifyRequest: SpotifyRequest) {
154-
cachedRequests.remove(spotifyRequest)
155+
internal operator fun minusAssign(request: SpotifyRequest) {
156+
checkCache(request)
157+
cachedRequests.remove(request)
155158
}
156159

157160
fun clear() = cachedRequests.clear()
161+
162+
private fun checkCache(request: SpotifyRequest) {
163+
if (!request.api.useCache) clear()
164+
}
158165
}
159166

160167
internal data class SpotifyRequest(
161168
val url: String,
162169
val method: HttpRequestMethod,
163-
val body: String?
170+
val body: String?,
171+
val api: SpotifyAPI
164172
)
165173

166174
internal data class CacheState(val data: String, val eTag: String?, val expireBy: Long = 0) {

0 commit comments

Comments
 (0)