Skip to content

Commit 0e377be

Browse files
committed
change cache limit to per-endpoint, 200 req
1 parent 04268f6 commit 0e377be

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SpotifyApiBuilder(
2323
var tokenString: String? = null,
2424
var token: Token? = null,
2525
var useCache: Boolean = true,
26-
var cacheLimit: Int? = 1000,
26+
var cacheLimit: Int? = 200,
2727
var automaticRefresh: Boolean = true,
2828
var retryWhenRateLimited: Boolean = false,
2929
var enableLogger: Boolean = false
@@ -215,7 +215,7 @@ class SpotifyApiBuilderDsl {
215215
private var credentials: SpotifyCredentials = SpotifyCredentials(null, null, null)
216216
private var authentication = SpotifyUserAuthorizationBuilder()
217217
var useCache: Boolean = true
218-
var cacheLimit: Int? = 1000
218+
var cacheLimit: Int? = 200
219219
var automaticRefresh: Boolean = true
220220
var retryWhenRateLimited: Boolean = false
221221
var enableLogger: Boolean = false

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ abstract class SpotifyEndpoint(val api: SpotifyAPI) {
3333
}
3434

3535
internal fun delete(
36-
url: String,
37-
body: String? = null,
38-
contentType: String? = null
36+
url: String,
37+
body: String? = null,
38+
contentType: String? = null
3939
): String {
4040
return execute(url, body, HttpRequestMethod.DELETE, contentType = contentType)
4141
}
4242

4343
private fun execute(
44-
url: String,
45-
body: String? = null,
46-
method: HttpRequestMethod = HttpRequestMethod.GET,
47-
retry202: Boolean = true,
48-
contentType: String? = null
44+
url: String,
45+
body: String? = null,
46+
method: HttpRequestMethod = HttpRequestMethod.GET,
47+
retry202: Boolean = true,
48+
contentType: String? = null
4949
): String {
5050
if (api is SpotifyAppAPI && System.currentTimeMillis() >= api.expireTime) api.refreshToken()
5151

@@ -73,10 +73,10 @@ abstract class SpotifyEndpoint(val api: SpotifyAPI) {
7373
}
7474

7575
private fun handleResponse(
76-
document: HttpResponse,
77-
cacheState: CacheState?,
78-
spotifyRequest: SpotifyRequest,
79-
retry202: Boolean
76+
document: HttpResponse,
77+
cacheState: CacheState?,
78+
spotifyRequest: SpotifyRequest,
79+
retry202: Boolean
8080
): String? {
8181
val statusCode = document.responseCode
8282

@@ -109,10 +109,10 @@ abstract class SpotifyEndpoint(val api: SpotifyAPI) {
109109
}
110110

111111
private fun createConnection(
112-
url: String,
113-
body: String? = null,
114-
method: HttpRequestMethod = HttpRequestMethod.GET,
115-
contentType: String? = null
112+
url: String,
113+
body: String? = null,
114+
method: HttpRequestMethod = HttpRequestMethod.GET,
115+
contentType: String? = null
116116
) = HttpConnection(
117117
url,
118118
method,
@@ -169,27 +169,26 @@ class SpotifyCache {
169169
cachedRequests.entries.removeIf { !it.value.isStillValid() }
170170

171171
val cacheLimit = request.api.cacheLimit
172-
val cacheUse = request.api.endpoints.sumBy { it.cache.cachedRequests.size }
172+
val cacheUse = cachedRequests.size
173173

174174
if (cacheLimit != null && cacheUse > cacheLimit) {
175175
val amountRemoveFromEach = ceil((cacheUse - cacheLimit).toDouble() / request.api.endpoints.size).toInt()
176176

177-
request.api.endpoints.forEach { endpoint ->
178-
val entries = endpoint.cache.cachedRequests.entries
179-
val toRemove = entries.sortedBy { it.value.expireBy }.take(amountRemoveFromEach)
177+
val entries = cachedRequests.entries
180178

181-
if (toRemove.isNotEmpty()) entries.removeAll(toRemove)
182-
}
179+
val toRemove = entries.sortedBy { it.value.expireBy }.take(amountRemoveFromEach)
180+
181+
if (toRemove.isNotEmpty()) entries.removeAll(toRemove)
183182
}
184183
}
185184
}
186185
}
187186

188187
data class SpotifyRequest(
189-
val url: String,
190-
val method: HttpRequestMethod,
191-
val body: String?,
192-
val api: SpotifyAPI
188+
val url: String,
189+
val method: HttpRequestMethod,
190+
val body: String?,
191+
val api: SpotifyAPI
193192
)
194193

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

0 commit comments

Comments
 (0)